8

After doing a lot of research online I'm still stumped by this problem. I have a page that loads the names and count of categories into a drop down list. I only do this if !(Page.IsPostBack). When AutoPostBack fires the SelectedIndex = 0. I've tried several 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 back 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 be able to populate the form based on the selected index, but I can't seem to get the correct answer. Any help is appreciated.

Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63
Tyler Ferraro
  • 3,753
  • 1
  • 21
  • 28
  • I'm assuming you have viewstate enabled? Have you checked the request to see what the value being returned for that control is? How many items are in the dropdown? – Dan Apr 13 '11 at 21:49
  • I'm fairly certain I've run into this issue before. Can you try making the update panel conditional and setting an async post back trigger to your DropDownLists selectedindex changed event? – Biff MaGriff Apr 13 '11 at 21:51
  • Right now there are 4 items in the dropdown. I'm not sure what you mean by checking the request to see what value is being returned though. I checked the ddlCategory.SelectedIndex in SelectedIndexChanged, and it's always 1. – Tyler Ferraro Apr 13 '11 at 21:53
  • I changed the update panel to conditional and added the trigger. It didn't change anything. When I change the DropDownList selection it automatically jumps back to the first item in the list. – Tyler Ferraro Apr 13 '11 at 21:56
  • Is GetCategories() being called from anywhere else? – Jack Marchetti Apr 13 '11 at 21:58
  • No it is not. I posted all code on the page, minus a few unnecessary details that 100% do not affect the code as it is. – Tyler Ferraro Apr 13 '11 at 22:00
  • @Tyler what is in the request?: put a breakpoint on the postback event and see what is in Context.Request - specifically to do with that control (should be able to find it using the control's client ID). if you like post it here so I can see. – Dan Apr 13 '11 at 22:44

8 Answers8

15

Add AppendDataBoundItems="true" for dropdown list

user1799214
  • 511
  • 2
  • 11
  • 25
4

Make sure that your value fields are unique for each dropdown list item. If each item has the same value, it will default on index 0.

MarzSocks
  • 4,229
  • 3
  • 22
  • 35
  • Been pulling my hair out all afternoon trying to figure this out. I had two ListItems with the same `Value="-1"`, changing one of them solved the problem. – delliottg Oct 09 '18 at 20:03
4

I discovered the problem. The values being populated from my SQL statement contained values that repeated. For some reason this was causing the entire thing to malfunction in weird ways which made it so that every time I selected a ListItem the whole list would reset. By making sure no values repeated, the code started working perfectly. Thanks for everyone's help.

Tyler Ferraro
  • 3,753
  • 1
  • 21
  • 28
1

This happened to me when attempting to use a combined column value for the DataValueField. For example:

The stored procedure was written like this:

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

And the DataValueField used the Value field which was a combination of the Value1 and Value2 fields separated by a comma. (I also tried a pipe and no delimiter 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 the DataValueField, the problem went away.

ghodges
  • 35
  • 1
  • 6
1

I struggled with this too, I tried EnableViewState="true" ViewStateMode="Enabled" but it's not needed in fact, you just have to addd IsPostBack in Page_Load event. Do not forget to add IsPostBack, that is it...

if (!IsPostBack)
{
    LoadDropDown();
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
krishgg
  • 11
  • 1
1

You have to load list to DropDownList if not IsPostBack

Example code:

if (!IsPostBack)
{
   //fill here
}
Ramil Mammadov
  • 462
  • 6
  • 11
1

Do you have Viewstate enabled or disabled? SelectedIndex is Zero based, so it were resetting I think it would be set to zero.

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

Try a different browser. I had an issue with cascading drop downs where it wasn't firing/behaving correctly in Firefox.

Jack Marchetti
  • 15,536
  • 14
  • 81
  • 117
  • ViewState is Enabled. If I disable it, then my list gets cleared on PostBack. – Tyler Ferraro Apr 13 '11 at 21:49
  • I tried it in Internet Explorer and I'm getting the same issue. Right now the SelectedIndexChanged isn't even being fired as the DropDownList just resets to the top selection when I change it. – Tyler Ferraro Apr 13 '11 at 22:02
  • 1
    I discovered the problem. The values being populated from my SQL statement contained values that repeated. For some reason this was causing the entire thing to malfunction in weird ways. By making sure null values became 0's, the code started working perfectly. Thanks for everyone's help. – Tyler Ferraro Apr 13 '11 at 22:42
0

I've been experiencing the same problem, my dropdownlist stateview jump to index 1 right after a postback event from another control. My suggestion simply make sure your dropdownlist values are not empty.

Hope its help someone .... :)

Alam
  • 11