Wednesday, March 28, 2012

Programmatically Adding Controls and Events

Hi, I am trying to add several link buttons to a page programmatically with the following code:

int i;
int i;
for (i = 0; i < 60; i++)
{
LinkButton lnk = new LinkButton();
lnk.ID = "region" + i;
lnk.CssClass = "mystyle";
lnk.Click += new System.EventHandler(lnk_Click);

addContent.Controls.Add(lnk);

}


which adds them perfectly, however I also want to add an event handler to them all (the same event handler) which I had hoped to achieve by adding the ' lnk.Click += new System.EventHandler(lnk_Click);'
however when the link buttons are now clicked the page freezes, any suggestions?

All that is assigned on the onclick event is a write out to a label within an ajax.net update panel.

Any help would be appreciated, cheers Jon

I set this up in my dev environment and wa sunabel to duplictae it. Can you post more of your code... the ASP and the code behind to give us a better idea of whats going on.

I got the code working - for some reason the fact that the linkbuttons were placed inside a updatepanel and that the 'childrenastriggers' set to true seems to cause the slow down.

I think i know the problem but i am not sure how to solve it!

basically my onload function generates around 1500 linkbuttons in tiny squares within an update panel, each assigned a unique ID.

onclick of each button it posts its unique number to a textbox (temporarily so i can see if it is doing something) it takes around 60 seconds to do this when the 'childrenastriggers' is set to true.

When this is not set to true it performs it instantly, which is fine, however what I am trying to achieve that on click of the linkbutton it puts the number in the box, and changes the css of the linkbutton, updates so the user can see it has changed.

However the update panel is refreshing the generation of 1500 link buttons - which is causing the slow down.

Without the update panel refreshing, and if i cause a full postback the change is instant.

Any ideas on how to get around this but still achieve something similar.


Try and place your button generation code in the Page_Init event.


worked a treat, thank you!

No comments:

Post a Comment