Dynamically Created Controls

  • Thread starter Thread starter accyboy1981
  • Start date Start date
A

accyboy1981

Hi,

I have created a method that creates a link button dynamically and
places it within a place holder. The code I have creates the button,
but when it is clicked the button method is not fired. Below is the
code the creates the button:

LinkButton lb1 = new LinkButton();
lb1.ID = "lb1" + day.ToString( "ddMMMyyyy" );
lb1.Text = day.ToString( "dd" );
lb1.SkinID = "linkButtonSkin3";
lb1.CommandArgument = day.ToString( "ddMMMyyyy" );
lb1.Click += new EventHandler( EditDay );

A unique ID is given for every button.

I am unsing Visual Studio 2005 and Asp.net 2.0.

Any help on this would be much appreciated.

Thanks in advance.

Simon
 
I have created a method that creates a link button dynamically and
places it within a place holder. The code I have creates the button,
but when it is clicked the button method is not fired. Below is the
code the creates the button:

LinkButton lb1 = new LinkButton();
lb1.ID = "lb1" + day.ToString( "ddMMMyyyy" );
lb1.Text = day.ToString( "dd" );
lb1.SkinID = "linkButtonSkin3";
lb1.CommandArgument = day.ToString( "ddMMMyyyy" );
lb1.Click += new EventHandler( EditDay );

A unique ID is given for every button.

I am unsing Visual Studio 2005 and Asp.net 2.0.

Any help on this would be much appreciated.

In which Page method are you creating the control? I'm betting you're not
creating it in Page_Init...
 
Hi,

Thanks for the response.

I've tried creating the buttons in the Page_Init, but the problem with
this is that I dont know the name as it will be dynamically choosen.

Basically what I am doing is creating a calendar for a year, and when a
user clicks a specific date if post-backs and runs a different method.
When it doen this the calendar is not drawn so this button will not
appear on the page. I am trying to pass the date that was clicked in
the commandargument so all I need is one method for the 365 days a user
can click. The problem I am finding is that because the button is not
drawn once it has been clicked it cannot be found.

Is there any way around this? Is it possible to have dynamic controls
this way on the same page?

Any help would be appreciated

Thanks

Simon
 
Back
Top