c# - Dynamically Generated Dropdowns Postback -
i'm having trouble same dynamically generated dropdowns , viewstate.
long story short, user upload excel file, file parsed , dropdowns created appropriate data. done on when asp button pressed, , controls added table follows:
public void generatefromsheet(ordereddictionary columns, datatable oppcolumns, list<string> requireddrops) { int index = 0; foreach (dictionaryentry entry in columns) { dropdownlist ddl = new dropdownlist() { id = "ddlmapping" + entry.key.tostring(), datasource = columns, datatextfield = "key", datavaluefield = "value", selectedindex = index, enabled = requireddrops.contains(entry.key) ? false : true }; ddl.databind(); dropdownlist ddl2 = new dropdownlist() { id = "opportunitymappingddl" + index, datasource = oppcolumns, datatextfield = "attributedisplayname", datavaluefield = "tablecolumnname" }; ddl2.databind(); htmltablecell td = new htmltablecell() { id = "tdmapping" + index }; td.controls.add(ddl); htmltablecell td2 = new htmltablecell() { id = "tdoppmapping" + index }; td2.controls.add(ddl2); htmltablerow tr = new htmltablerow() { id = "trmapping" + index }; tr.cells.add(td); tr.cells.add(td2); tblfilemapping.rows.add(tr); index++; } }
however, on each postback after this, drop-downs erased. i've looked online solution , points recreating controls using same id's when created state can restored viewstate. i've tried follows below storing should create in viewstate:
public void generatefromviewstate() { ordereddictionary columns = (ordereddictionary) viewstate["xlcolumns"]; int index = 0; foreach (dictionaryentry entry in columns) { dropdownlist ddl = new dropdownlist() { id = "ddlmapping" + entry.key.tostring(), }; dropdownlist ddl2 = new dropdownlist() { id = "opportunitymappingddl" + index, }; htmltablecell td = new htmltablecell() { id = "tdmapping" + index }; td.controls.add(ddl); htmltablecell td2 = new htmltablecell() { id = "tdoppmapping" + index }; td2.controls.add(ddl2); htmltablerow tr = new htmltablerow() { id = "trmapping" + index }; tr.cells.add(td); tr.cells.add(td2); tblfilemapping.rows.add(tr); index++; } }
i call method in page_load controls not retain previous data , selected values.
so couple of things wrong here:
- on page_load controls recreated state not restored.
- for technical reasons, project manager mentioned should not use session state store anything.
- another pm advised me controls should regenerated on page_init. since i'm storing control data in viewstate, isn't possible because viewstate isnt ready , data null.
can advise on how succesfully restore viewstate these dynamically generated controls. i've tried searching , tried bunch of solutions online, nothing have tried seems work.
thanks!
you doing right, have recreate controls datasource , rebinding controls again. without beeing done, creating controls not match previous. can call first method that.
Comments
Post a Comment