When I had problems with ViewState (this is what I suspect in your case), I used this to restore data in a dynamically popup dropdown
public void Page_Load(object sender, EventArgs e){ if (!IsPostBack) { Databind(); } else { LoadAllViewStates(); } } private void Databind() { DataTable questionnaireDT = null; DataTable questionsDT = null; DataTable indicatorDT = null; DataView tempView = QuestionnaireDS.Select(DataSourceSelectArguments.Empty) as DataView; questionnaireDT = tempView.Table; ViewState["QuestionnaireDL"] = questionnaireDT; QuestionnaireDL.DataSource = ViewState["QuestionnaireDL"]; QuestionnaireDL.DataBind(); tempView = QuestionDS.Select(DataSourceSelectArguments.Empty) as DataView; questionsDT = tempView.Table; ViewState["QuestionList"] = questionsDT; QuestionList.DataSource = ViewState["QuestionList"]; QuestionList.DataBind(); tempView = IndicatorDS.Select(DataSourceSelectArguments.Empty) as DataView; indicatorDT = tempView.Table; ViewState["IndicatorLst"] = indicatorDT; IndicatorLst.DataSource = ViewState["IndicatorLst"]; IndicatorLst.DataBind(); } private void LoadAllViewStates() { QuestionnaireDL.DataSource = ViewState["QuestionnaireDL"]; QuestionnaireDL.DataBind(); QuestionList.DataSource = ViewState["QuestionList"]; QuestionList.DataBind(); IndicatorLst.DataSource = ViewState["IndicatorLst"]; IndicatorLst.DataBind(); }
To restore the selected index, I passed selectedIndex to a hidden field.
Hope this helps?
By the way, why pass a DropDownList object as a parameter? Instead, call the function without parameters and populate the DropDownList object inside the function.
Also, make sure ViewState is not disabled.
source share