c# - Dynamic Controls in PlaceHolder were lost after an action is performed -
i have placeholder in webform master page code
<asp:dropdownlist id="noofsubjectlist" runat="server" autopostback="true" onselectedindexchanged="noofsubject_index_changed"> <asp:listitem>1</asp:listitem> <asp:listitem>2</asp:listitem> <asp:listitem>3</asp:listitem> <asp:listitem>4</asp:listitem> <asp:listitem>5</asp:listitem> <asp:listitem>6</asp:listitem> <asp:listitem>7</asp:listitem> <asp:listitem>8</asp:listitem> <asp:listitem>9</asp:listitem> <asp:listitem>10</asp:listitem> <asp:listitem>11</asp:listitem> <asp:listitem>12</asp:listitem> </asp:dropdownlist> <asp:placeholder id="placeholder" runat="server" />
based on number(n) selected dropdownlist (1), dynamically creating 2 sets of dropdownlist (2,3) selected n numbers. in first dropdownlist loads data database. code shown below,
protected void noofsubject_index_changed(object sender, eventargs e) { int value = convert.toint32(noofsubjectlist.selecteditem.text.tostring()); dataset ds1 = new dataset(); mysql.data.mysqlclient.mysqlconnection mysqlconnection = new mysql.data.mysqlclient.mysqlconnection ("database=school;server=localhost;uid=root;pwd=;"); mysql.data.mysqlclient.mysqlcommand mysqlcommand = new mysql.data.mysqlclient.mysqlcommand ("select dept tabledept status='active'", mysqlconnection); mysql.data.mysqlclient.mysqldataadapter mysqladaptor = new mysql.data.mysqlclient.mysqldataadapter(mysqlcommand); mysqladaptor.fill(ds1); (int = 0; <= value - 1; i++) { dropdownlist _sublist = new dropdownlist(); _sublist.id = "sublist" + (i + 1); _sublist.width= 75; dropdownlist _deptlist = new dropdownlist(); _deptlist.id = "deptlist" + (i + 1); _deptlist.width = 75; _deptlist.selectedindexchanged += deptlist_index_changed; literal _spacer = new literal(); _spacer.text = "<br />"; literal _spacerlbl = new literal(); _spacerlbl.text = " "; label _lbl = new label(); _lbl.id = "lbl_subject" + i; _lbl.text = "subject" + (i+1); label _lbl1 = new label(); _lbl1.id = "lbl_dept" + i; _lbl1.text = "department"; _deptlist.datasource = ds1; _deptlist.datatextfield = ds1.tables[0].columns["dept"].columnname; _deptlist.datavaluefield = ds1.tables[0].columns["dept"].columnname; _deptlist.databind(); _deptlist.autopostback = true; placeholder.controls.add(_lbl1); placeholder.controls.add(_spacerlbl); placeholder.controls.add(_deptlist); placeholder.controls.add(_spacerlbl); placeholder.controls.add(_lbl); placeholder.controls.add(_sublist); placeholder.controls.add(_spacer); } } protected void deptlist_index_changed(object sender, eventargs e) { \\based on selection in dropdownlist2, list loaded \\database dropdownlist3 }
the above event triggered based on selection of number n dropdownlist (1). if select item dropdownlist (2) data generated database , added dropdownlist (3) . problem when try select item dropdownlist (2), dynamically created controls lost. how overcome issue?
i googled , found out missing called postback
in code. not able find out relevant resource learn
i got link shows easy example of creating controls after postback.
Comments
Post a Comment