c# - Dynamically created linkbuttons' common event not firing -


whenever dropdownlist selectedindexchanged, adding linkbuttons ul-li list in codebehind. each linkbuttons assigned ids , common click event. problem code in click event not executed or maybe event not triggered. code below: [edit] tried suggested in other posts (dynamically created list of link buttons, link buttons not posting back)

protected void ddl_selectedindexchanged(object sender, eventargs e) {    populate(); }  protected override void oninit(eventargs e) {    base.oninit(e);    populate(); }  void populate() {    htmlgenericcontrol ullist = new htmlgenericcontrol("ul");    panel.controls.add(ullist);     foreach (datarow dr in drc)     {       htmlgenericcontrol lilist = new htmlgenericcontrol("li");       ullist.controls.add(lilist);        var lnk = new linkbutton();       lnk.id = dr["col1"].tostring();       lnk.text = dr["col1"].tostring();       lnk.click += clicked;       lilist.controls.add(lnk);    } }                private void clicked(object sender, eventargs e) {    var btn = (linkbutton)sender;    label1.text = btn.id.tostring(); } 

im missing something. please.

here issue viewstate. when selected index of dropdownlist changes there postback takes place , previous state lost, @ point have maintain state of controls.

now in code state of control lost , click event not fire. solution maintain state of controls.

the below working example, can paste , try.

this page load.

protected void page_load(object sender, eventargs e)         {             (var = 0; < linkbuttonnumber; i++)                 addlinkbutton(i);         } 

similarly have maintain state of added control this.

private int linkbuttonnumber         {                         {                 var number = viewstate["linkbuttonnumber"];                 return (number == null) ? 0 : (int)number;             }             set             {                 viewstate["linkbuttonnumber"] = value;             }         } 

the below selectedindexchanged event dropdownlist

protected void example_selectedindexchanged(object sender, eventargs e)         {             addlinkbutton(linkbuttonnumber);             linkbuttonnumber++;         } 

i have function dynamically creates controls, called on page load , on selectedindexchanged.

private void addlinkbutton(int index)         {             linkbutton linkbutton = new linkbutton { id = string.concat("txtdomain", index) };             linkbutton.clientidmode = clientidmode.static;             linkbutton.text = "link button ";             linkbutton.click += linkbutton_click;             paneldomain.controls.add(linkbutton);             paneldomain.controls.add(new literalcontrol("<br />"));         } 

and click event linkbutton

void linkbutton_click(object sender, eventargs e)         {             //you logic here         } 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -