c# - Button Click event not firing for dynamically created button -


i have created dynamic controls on dropdownlist's selectedindexchanged event. button 1 of controls. have assigned event button debugger not coming on click event. following code.

protected void page_load(object sender, eventargs e)     {         try         {             token = session["logintoken"].tostring();             if (!ispostback)             {                 binddata();                 fssearch.visible = false;                 btndownload.visible = false;             }             else             {                 foreach (htmltablerow row in (htmltablerowcollection)session["dynamiccontrols"])                 {                     tblsearch.rows.add(row);                 }             }         }         catch         {         }     }      private void binddata()     {         ddlreportname.datasource = svccommon.getreports(token, out message);         ddlreportname.datavaluefield = "key";         ddlreportname.datatextfield = "value";         ddlreportname.databind();         ddlreportname.items.insert(0, "--select--");     }      protected void ddlreportname_selectedindexchanged(object sender, eventargs e)     {         try         {             string[] reportinfo = ddlreportname.selectedvalue.split('|');              session["dynamiccontrols"] = null;             tblsearch.rows.clear();              htmltablerow row = new htmltablerow();             htmltablecell cellfieldnamelbl = new htmltablecell();             htmltablecell cellfieldnameddl = new htmltablecell();             htmltablecell celloperatorlbl = new htmltablecell();             htmltablecell celloperatorddl = new htmltablecell();             htmltablecell cellvaluelbl = new htmltablecell();             htmltablecell cellvaluetxt = new htmltablecell();             htmltablecell celloperatorrbtn = new htmltablecell();             htmltablecell celladdmorefilter = new htmltablecell();             button btnaddmore = new button();              dropdownlist ddlcolumn = new dropdownlist();             dropdownlist ddloperator = new dropdownlist();             textbox txtvalue = new textbox();             radiobuttonlist rbtnoperator = new radiobuttonlist();              list<string> filtervalues = svccommon.getsearchcolumns(convert.toint64(reportinfo[0]), token, out message);              fssearch.visible = btndownload.visible = filtervalues.count > 0 ? true : false;              ddlcolumn.id = "_ddlcolumn0";             ddloperator.id = "_ddloperator0";             txtvalue.id = "_txtvalue0";             rbtnoperator.id = "_rbtnoperator0";             btnaddmore.id = "_btnaddmore0";               rbtnoperator.items.add("and");             rbtnoperator.items.add("or");             rbtnoperator.repeatdirection = repeatdirection.horizontal;              btnaddmore.text = "add more";              btnaddmore.click +=btnaddmore_click;              ddlcolumn.datasource = filtervalues;             ddlcolumn.databind();              ddloperator.datasource = new list<string>()                                          {                                                "equal",                                             "not equal",                                             "less than",                                             "less or equal",                                             "greater than",                                             "greater or equal",                                             "start with",                                             "not start with",                                             "end with",                                             "not end with",                                             "contains",                                             "not contains",                                             "between",                                             "not between",                                             "in",                                             "not in"                                         };             ddloperator.databind();              cellfieldnamelbl.innertext = "field name:";             cellfieldnameddl.controls.add(ddlcolumn);             celloperatorlbl.innertext = "operator";             celloperatorddl.controls.add(ddloperator);             cellvaluelbl.innertext = "value";             cellvaluetxt.controls.add(txtvalue);             celloperatorrbtn.controls.add(rbtnoperator);             celladdmorefilter.controls.add(btnaddmore);              row.cells.add(cellfieldnamelbl);             row.cells.add(cellfieldnameddl);             row.cells.add(celloperatorlbl);             row.cells.add(celloperatorddl);             row.cells.add(cellvaluelbl);             row.cells.add(cellvaluetxt);             row.cells.add(celloperatorrbtn);             row.cells.add(celladdmorefilter);              tblsearch.rows.add(row);             session["dynamiccontrols"] = tblsearch.rows;         }         catch (exception ex)         {         }     }      protected void btnaddmore_click(object sender, eventargs e)     {         try         {          }         catch         {         }     } 

the problem dynamically created controls in asp.net webforms aren't automatically added viewstate, postback event won't happen.

this should understand adding controls dynamically, , managing them via viewstate http://forums.asp.net/t/1900207.aspx?creating+buttons+dynamically

alternatively, easier way manage have buttons on page not visible, in selected_index_changed event, switch visibility true.


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -