c# - Dynamically added ASP.Net Button could not call Click or CommandArgument event -
i adding x number of buttons asp.net web application. code doing so:
int = 0; foreach(var foo in bar){ button b = new button(); b.id = "button" + i.tostring(); b.commandname = "var_value"; b.commandargument = foo; b.command += execute_command; //add panel p p.controls.add(b); i++; } private void execute_command(object sender, commandeventargs e){ //do stuff }
the execute_command method never called. buttons display fine, , when debug have command name , correct command argument assigned them. i'm not sure i'm doing wrong.
buttons created dynamically, they not in control tree. result, when trigger click event, not fire command event.
in order fix it, need reload dynamically created buttons in page_init or page_load event on every postback same ids.
for example,
protected void page_init(object sender, eventargs e) { int = 0; foreach(var foo in bar){ button b = new button(); b.id = "button" + i.tostring(); b.commandname = "var_value"; .commandargument = foo; b.command += execute_command; //add panel p p.controls.add(b); i++; } }
Comments
Post a Comment