create a LinkButton at "runntime"

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
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 said:
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 said:
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
 
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 said:
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 said:
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 said:
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
 
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
 
Back
Top