473,385 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

create a LinkButton at "runntime"

I wish to create some LinkButtons in DotNet. Because I will do it in
dependence of the entries in a database, I can not add them with the mouse
to the form as usually.
I can create them in the Page_Load event but then I can not see them.
I do not know the reason or how to do it correctly.
Do anywhere know something about?
For any help, I thanks a lot
Greetings Patrick Marti
Jul 21 '05 #1
5 1474
Hi Patrick

by "LinkButtons" I assume you are refering to LinkLabels, but anyways it
should work just the same for all controls.... so...
There are a few ways to go about it. If you know from the get go, how many
LinkLabels you will have dynamically created after runtime, you can declare
those controls in your code like this:

private LinkLabel[] _myLinkLabel = new LinkLabel[100]; // declaring 100
link labels

.... then in your code somewhere... depending when you need to create the
labels... say on some other Button you have in your form of which the user
needs to press...

private void SomeButton_Clicked( Object sender, System.EvenHandler e )
{
_myLinkLabel[ _labelCounter ] = new LinkLabel();
_myLinkLabel[ _labelCounter ].Name = "New Label";
_myLinkLabel[ _labelCounter ].Text = "Some text";
..
..
..
// some more code here
..
..
..
}

and that should be it..... UNLESS!!! you have something like a Panel on your
form.... of which will obscure any object under it, if it is not added to
the panel....

so to add a control to a panel...like the link label, you need

SomePanel.Controls.Add( _myLinkLabel[ _labelCounter ] );

then you would probably need to re locate your control so that it is where
you want it to be on your form.

Also... I would recomend using ArrayLists if you don't know how many labels
you will need after runtime. That way you don't really need declare your
label because an ArrayList takes any kind of object... and to access it
later on...to modify it you can either use the Controls property from you
form or the Panel if it belongs to a panel.

hope that helps

"Patrick Marti" <Pa**********@discussions.microsoft.com> wrote in message
news:FA**********************************@microsof t.com...
I wish to create some LinkButtons in DotNet. Because I will do it in
dependence of the entries in a database, I can not add them with the mouse
to the form as usually.
I can create them in the Page_Load event but then I can not see them.
I do not know the reason or how to do it correctly.
Do anywhere know something about?
For any help, I thanks a lot
Greetings Patrick Marti

Jul 21 '05 #2
Hi Felipe
Thanks so much for this huge and really interesting answer!!!
Unfortunately I can try out this at least when I'm at home in the evening.
Anyway, maybe one problem it will exists even longer. I think my object
will not be shown after creating it, but maybe it is because I do it in the
Page_Load event?
I will do some tests more this evening, but it is good to know that it should
work in general.
Again many thanks for your super help
Greetings Patrick

"Felipe" wrote:
Hi Patrick

by "LinkButtons" I assume you are refering to LinkLabels, but anyways it
should work just the same for all controls.... so...
There are a few ways to go about it. If you know from the get go, how many
LinkLabels you will have dynamically created after runtime, you can declare
those controls in your code like this:

private LinkLabel[] _myLinkLabel = new LinkLabel[100]; // declaring 100
link labels

.... then in your code somewhere... depending when you need to create the
labels... say on some other Button you have in your form of which the user
needs to press...

private void SomeButton_Clicked( Object sender, System.EvenHandler e )
{
_myLinkLabel[ _labelCounter ] = new LinkLabel();
_myLinkLabel[ _labelCounter ].Name = "New Label";
_myLinkLabel[ _labelCounter ].Text = "Some text";
..
..
..
// some more code here
..
..
..
}

and that should be it..... UNLESS!!! you have something like a Panel on your
form.... of which will obscure any object under it, if it is not added to
the panel....

so to add a control to a panel...like the link label, you need

SomePanel.Controls.Add( _myLinkLabel[ _labelCounter ] );

then you would probably need to re locate your control so that it is where
you want it to be on your form.

Also... I would recomend using ArrayLists if you don't know how many labels
you will need after runtime. That way you don't really need declare your
label because an ArrayList takes any kind of object... and to access it
later on...to modify it you can either use the Controls property from you
form or the Panel if it belongs to a panel.

hope that helps

"Patrick Marti" <Pa**********@discussions.microsoft.com> wrote in message
news:FA**********************************@microsof t.com...
I wish to create some LinkButtons in DotNet. Because I will do it in
dependence of the entries in a database, I can not add them with the mouse
to the form as usually.
I can create them in the Page_Load event but then I can not see them.
I do not know the reason or how to do it correctly.
Do anywhere know something about?
For any help, I thanks a lot
Greetings Patrick Marti


Jul 21 '05 #3
Hey Patrick,

It should work even if you do it in Page_Load as long as you remember to add
the controls to the Controls collection of either the form or any other
control already on the form. However, you may run into some issues regarding
viewstate when adding the controls in Page_Load. Because of this the OnInit
override in your form may be a better place to do it.

Regards, Jakob.

"Patrick Marti" wrote:
Hi Felipe
Thanks so much for this huge and really interesting answer!!!
Unfortunately I can try out this at least when I'm at home in the evening.
Anyway, maybe one problem it will exists even longer. I think my object
will not be shown after creating it, but maybe it is because I do it in the
Page_Load event?
I will do some tests more this evening, but it is good to know that it should
work in general.
Again many thanks for your super help
Greetings Patrick

"Felipe" wrote:
Hi Patrick

by "LinkButtons" I assume you are refering to LinkLabels, but anyways it
should work just the same for all controls.... so...
There are a few ways to go about it. If you know from the get go, how many
LinkLabels you will have dynamically created after runtime, you can declare
those controls in your code like this:

private LinkLabel[] _myLinkLabel = new LinkLabel[100]; // declaring 100
link labels

.... then in your code somewhere... depending when you need to create the
labels... say on some other Button you have in your form of which the user
needs to press...

private void SomeButton_Clicked( Object sender, System.EvenHandler e )
{
_myLinkLabel[ _labelCounter ] = new LinkLabel();
_myLinkLabel[ _labelCounter ].Name = "New Label";
_myLinkLabel[ _labelCounter ].Text = "Some text";
..
..
..
// some more code here
..
..
..
}

and that should be it..... UNLESS!!! you have something like a Panel on your
form.... of which will obscure any object under it, if it is not added to
the panel....

so to add a control to a panel...like the link label, you need

SomePanel.Controls.Add( _myLinkLabel[ _labelCounter ] );

then you would probably need to re locate your control so that it is where
you want it to be on your form.

Also... I would recomend using ArrayLists if you don't know how many labels
you will need after runtime. That way you don't really need declare your
label because an ArrayList takes any kind of object... and to access it
later on...to modify it you can either use the Controls property from you
form or the Panel if it belongs to a panel.

hope that helps

"Patrick Marti" <Pa**********@discussions.microsoft.com> wrote in message
news:FA**********************************@microsof t.com...
I wish to create some LinkButtons in DotNet. Because I will do it in
dependence of the entries in a database, I can not add them with the mouse
to the form as usually.
I can create them in the Page_Load event but then I can not see them.
I do not know the reason or how to do it correctly.
Do anywhere know something about?
For any help, I thanks a lot
Greetings Patrick Marti


Jul 21 '05 #4
I found a good article yesterday that shows how to do something similar and
also explains the viewstate considerations:

http://www.codeproject.com/aspnet/retainingstate.asp

HTH

"Patrick Marti" wrote:
I wish to create some LinkButtons in DotNet. Because I will do it in
dependence of the entries in a database, I can not add them with the mouse
to the form as usually.
I can create them in the Page_Load event but then I can not see them.
I do not know the reason or how to do it correctly.
Do anywhere know something about?
For any help, I thanks a lot
Greetings Patrick Marti

Jul 21 '05 #5
So good!!!
Thanks everybody for help in this.
I could not test it until now, but I'm shure it will work.
Have a good time
Greetings Patrick
"rufus" wrote:
I found a good article yesterday that shows how to do something similar and
also explains the viewstate considerations:

http://www.codeproject.com/aspnet/retainingstate.asp

HTH

"Patrick Marti" wrote:
I wish to create some LinkButtons in DotNet. Because I will do it in
dependence of the entries in a database, I can not add them with the mouse
to the form as usually.
I can create them in the Page_Load event but then I can not see them.
I do not know the reason or how to do it correctly.
Do anywhere know something about?
For any help, I thanks a lot
Greetings Patrick Marti

Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Patrick Marti | last post by:
I wish to create some LinkButtons in DotNet. Because I will do it in dependence of the entries in a database, I can not add them with the mouse to the form as usually. I can create them in the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.