I dynamically populate a dropdownlist of all 50 states from an ArrayList on PageLoad. When the user selects the SUBMIT button (btnSubmit_Click event), the SelectedIndex property of the dropdownlist control is always 0 despite what selection the user selects.
HTML code in form:
<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="True"
onselectedindexchanged="ddlState_SelectedIndexChanged" >
</asp:DropDownList>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
//------------------------------------------------
// Populates state dropdownlists
//------------------------------------------------
if (!IsPostBack)
{
GetAllStatesForDdl(ddlDLState);
GetAllStatesForDdl(ddlOldState);
GetStatesForDdl(ddlState);
}
}
private void GetAllStatesForDdl(DropDownList ddlStateList)
{
AppInputFormProcessor getStates = new AppInputFormProcessor();
ArrayList States = new ArrayList();
States = getStates.GetAllStates();
ddlStateList.DataSource = States;
ddlStateList.DataBind();
}
private void GetStatesForDdl(DropDownList ddlStateList)
{
AppInputFormProcessor getStates = new AppInputFormProcessor();
ArrayList States = new ArrayList();
States = getStates.GetStates();
ddlStateList.DataSource = States;
ddlStateList.DataBind();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
int aaa = ddlState.SelectedIndex;
int bbb = Convert.ToInt32(Session["ddlState"]);
}
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
Session["ddlState"] = ddlState.SelectedIndex;
}
When I had trouble with ViewState (that is what l suspect in your case) l used this to restore data to a dynamically populated dropdown object
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 the selectedIndex into a hidden field.
Hope this helps?
By the way, why pass in the DropDownList object as a parameter? Instead call a parameterless function and populate the DropDownList object within the function.
Also, ensure that ViewState isnt switched off.
In your example, you are trying to get the selected value from a session variable, but there's no code shown that actually sets anything in the session.
Even if you have some sort of async call that sets a session variable, this is a very dangerous practice: as soon as someone opens up a 2nd tab, you risk the chance of data corruption.
This should work for you. However, I am sort of confused why you are passing the dropdown to the function to get the states. Do you have multiple dropdowns to be filled? I think we need to see your html to be of more help.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
GetStatesForDdl(ddl);
}
private void GetStatesForDdl(DropDownList ddlStateList)
{
AppInputFormProcessor getStates = new AppInputFormProcessor();
ArrayList States = new ArrayList();
States = getStates.GetStates();
ddlStateList.DataSource = States;
ddlStateList.DataBind();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With