C # ASP.Net Dynamically populated DropDownList loses selected index on submit button

I dynamically populate a drop-down list of all 50 states from ArrayList on the Load page. When the user selects the SUBMIT button (btnSubmit_Click event), the SelectedIndex property of the dropdownlist control is always 0, despite the user choice.


Additional troubleshooting code added. Getting -1 from both the session variable (bbb) and ddlState.selectedindex (bbb).

HTML code in the form:

<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="True" onselectedindexchanged="ddlState_SelectedIndexChanged" > </asp:DropDownList> 

Code for:

 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; } 
+6
source share
9 answers

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.

+4
source

This should work for you. However, I'm a little confused why you are passing a drop down menu to a function to get the states. Do you have some drop down lists to populate? I think we need your html to get a lot of 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(); } 
+1
source

In your example, you are trying to get the selected value from a session variable, but there is no code that actually sets anything in the session.

Even if you have some kind of asynchronous call that sets the session variable, this is a very dangerous practice: as soon as someone opens the second tab, you run the risk of getting data corruption.

+1
source

First you will need to populate and bind the list only in the if( !IsPostBack ) { ... .

Next, the ["ddlState"] session is never populated, not sure where you want to populate it, but you can do it in the selected index, modified as you said. this should work if you don't call DataBind () on the postback.

0
source

How about this:

 protected void GetStatesForDdl(DropDownList ddlStateList) { //remove event handler ddlStateList.SelectedIndexChanged -= new EventHandler(ddlState_SelectedIndexChanged); //business as usual AppInputFormProcessor getStates = new AppInputFormProcessor(); ArrayList States = new ArrayList(); States = getStates.GetStates(); ddlStateList.DataSource = States; ddlStateList.DataBind(); // then force it to select desired index ddlStateList.SelectedIndex = (int)Session["ddlState"]; //ok, stick it back to control ddlStateList.SelectedIndexChanged += new EventHandler(ddlState_SelectedIndexChanged); } 

I suggest using local variables in ViewState instead of Session and SelectedValue instead of SelectedIndex.

0
source

This may help you:

Binding a drop-down list indicates the DataTextField and DataValueField properties. The reason for getting the selected index as 0 at the time of sending is that the drop-down list does not contain unique values ​​associated with each element in it. And remember that the property specified in the DataValueField must be unique. that is, each item in the drop-down list must be uniquely identified.

For example, consider the State class as follows:

 public class State { public string Name { get; set; } public string Id { get; set; } } 

Now the drop-down list should be anchored as indicated below. Here, for each state item, the "Id" property is unique.

 ddlStateList.DataSource = States; ddlStateList.DataTextField = "Name"; ddlStateList.DataValueField = "Id"; ddlStateList.DataBind(); 
0
source

If you have disabled the ViewState function, you need to save SelectedIndex in the session and on the Page_Load page, you always load the data and set the index directly. When you bind the data source, send the index as the default parameter by index (from the session) and set the SelectedIndex value to this number after calling DataBind ().

0
source

When you say you are not using ViewState, does this mean that you have disabled it for the page or your drop-down list? This may be your problem. I know that many ask you this, but I do not see an answer from you to find out if you are using a viewstate. When your user makes a choice and your page is sent back, you still need a viewstate, so your page knows what the user chooses, even after submitting. Then you can save it in a session or in a view.

On the other hand, if your viewport is on, add one piece of code to a new page to see when the problem starts to appear. Remember not to touch the view state and go by default. First, add a page with a drop-down list and a button and bind it to two dummy values ​​and read the user's choice in the response message. As you continue to add more code and controls, if the problem reappears, you can find out which line of code gives you sadness.

Hope this helps.

0
source

If you link your dropdown list items to OnInit, all view state will be handled automatically by the wireframe. If you bind it to Page_Load, you do not add them until the viewstate is applied. Move it to OnInit and you won’t have to worry about it.

0
source

Source: https://habr.com/ru/post/910248/


All Articles