Asp.NET DropDownList Resets SelectedIndex after PostBack

After much research online, I am still shocked by this problem. I have a page that loads the names and the number of categories into a drop-down list. I only do this if !(Page.IsPostBack) . When AutoPostBack fired SelectedIndex = 0 . I tried a few different things. Here is my code:

Page

 <form id="AddAssignmentForm" runat="server"> <asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server" /> <asp:UpdatePanel ID="CommentUpdate" runat="server"> <ContentTemplate> Add Comment <asp:DropDownList ID="ddlCategory" runat="server" Width="206" OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged" AutoPostBack="true" /> <asp:TextBox ID="txtName" runat="server" Width="200" /> <asp:TextBox ID="txtAbbrv" runat="server" Width="200" /> <asp:TextBox ID="txtDescription" runat="server" Width="200" Height="90" TextMode="MultiLine" /> </ContentTemplate> </asp:UpdatePanel> </form> 

Here is the code end code.

 private void Page_Load(object sender, System.EventArgs e) { if (!Page.IsPostBack) { GetCategories(); } } public void GetCategories() { String strSql = @"SELECT Name, Total FROM MyTable"; if (con.State == ConnectionState.Closed) con.Open(); OleDbCommand cmdsql = new OleDbCommand(strSql, con); OleDbDataReader cmdReader = cmdsql.ExecuteReader(); if (cmdReader.HasRows) { while (cmdReader.Read()) { ddlCategory.Items.Add(new ListItem(cmdReader["Category_Name"].ToString(), cmdReader["Total"].ToString())); } ddlCategory.SelectedIndex = -1; } cmdReader.Close(); con.Close(); } public void FillForm(int index) { ListItem item = ddlCategory.Items[index]; txtName.Text = item.Text + " " + (Convert.ToInt32(item.Value) + 1).ToString(); txtAbbrv.Text = item.Text.Substring(0, 1) + (Convert.ToInt32(item.Value) + 1).ToString(); } public void ddlCategory_SelectedIndexChanged(Object sender, EventArgs e) { //When I break here SelectedIndex always = 1. FillForm(ddlCategory.SelectedIndex); } 

I just want to fill out a form based on the selected index, but I cannot get the correct answer. Any help is appreciated.

+6
source share
8 answers

I have discovered a problem. The values ​​populated from my SQL statement contained duplicate values. For some reason, this led to the fact that the whole thing was malfunctioning in strange ways that made it so that every time I selected ListItem, the whole list would be reset. After making sure that the values ​​are not repeated, the code started working fine. Thanks for helping everyone.

+3
source

Add AppendDataBoundItems = "true" for the dropdown

+11
source

Make sure your value fields are unique for each drop-down list. If each element has the same value, it will default to index 0.

+2
source

Do you have ViewState enabled or disabled? SelectedIndex is based on zero, so it was reset, I think it will be set to zero.

Also, what do the other properties of the drop-down list set? Is the selected value correct?

Try using a different browser. I had a problem with a cascading drop-down list where it didn’t shoot / behave correctly in Firefox.

+1
source

This happened to me when trying to use a combined column value for a DataValueField. For instance:

The stored procedure was written as follows:

 SELECT Description, Value1 + ',' + Value2 AS Value FROM DropDownListTable 

And DataValueField used the Value field, which was a combination of the Value1 and Value2 fields, separated by a comma. (I also tried the pipe and did not limit it, but had the same results)

  With ddl .DataTextField = "Description" .DataValueField = "Value" .DataSource = ds .DataBind() End With 

As soon as I used Value1 or Value2 as a DataValueField, the problem disappeared.

+1
source

I had the same problem: my dropdownlist stateview goes to index 1 right after the postback event from another control. My suggestion is just to make sure your dropdownlist values ​​are not empty .

Hope to help someone .... :)

0
source

I also struggled with this, I tried EnableViewState="true" ViewStateMode="Enabled" , but this is not really necessary, you just need to add IsPostBack to the Page_Load event. Remember to add IsPostBack, i.e. ...

 if (!IsPostBack) { LoadDropDown(); } 
0
source

You need to upload the list to DropDownList, if not IsPostBack

Code example:

 if (!IsPostBack) { //fill here } 
0
source

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


All Articles