Saturday, March 24, 2012

triggers for programmatically added controls

Hi!

I almos get crazy in here :-)

I have a Button and an UpdatePanel with a table in it. When the Button is pressed, the UpdatePanel fills up with MANY programmatically auto generated LinkButtons:

example:

void mybutton_Click(object sender, EventArgs e)

 {
LinkButton mylinkbutton =new LinkButton();
mylinkbutton.Text ="hallo";
mylinkbutton.ID ="mylinkbutton";
mylinkbutton.Click +=new EventHandler(mylinkbutton_Click);
mycell.Controls.Add(mylinkbutton);
}

where mycell is part of the Updatepanel and mybutton is a AsyncPostBackTrigger of the UpdatePanel.

The problem is the EventHandler of the several link buttons (mylinkbutton_Click). The Event doesnt get fired.

I already tried tons of solutions like

ScriptManager.RegisterAsyncPostBackControl(mylinkbutton);

within the mybutton_Click. Didnt work...

This page (http://ajax.asp.net/docs/tutorials/UsingUpdatePanelControls.aspx) tells me that I have to place the .RegisterAsynPostBackControl in the Page_Load() function, but how should I do this at runtime?

Any ideas? need help!

TIA

J

Try this:

ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(mylinkbutton)

Thanks a lot, but I already tried that!

I use a usercontrol and therefore call the postbackcontrol like that:

ScriptManager.GetCurrent(Parent.Page).RegisterAsyncPostBackControl(mylinkbutton);

But it still doesnt work :(

i do it like that:

void mybutton_Click(object sender, EventArgs e) { LinkButton mylinkbutton =new LinkButton(); mylinkbutton.Text ="hallo"; mylinkbutton.ID ="mylinkbutton"; mylinkbutton.Click +=new EventHandler(mylinkbutton_Click); mycell.Controls.Add(mylinkbutton); ScriptManager mymanager = ScriptManager.GetCurrent(Parent.Page); mymanager.RegisterAsyncPostBackControl(mylinkbutton); }

Microsoft says that I should call RegisterAsyncPostBackControl() in Page_Load() but I cannot write it there because I dont know my LinkButtons at design time! :-(

any ideas?

thx Joe


Hi,

like every dynamic controls, your LinkButtons must be recreated on every postback (either synchronous or asynchronous).

When you click one of the dynamic LinkButtons, you fire an asynchronous postback but the corresponding control doesn't exist on the server side, because you've created it in a handler (mybutton_Click) that now doesn't get called.

No comments:

Post a Comment