nav100
nav100

Reputation: 61

Listbox always returns 0

I am loading a ListBox OnSelectedChange of DropDownlist. If I select a 3rd value from the ListBox, it always returns 0. What could be wrong? I appreciate any help. Thank you. Here is my code.

    <asp:DropDownList ID="dropdown1" runat="server" Width="300" OnSelectedIndexChanged="onChange"
                                                AutoPostBack="true">
<asp:ListBox ID="list1" runat="server" Width="300" Rows="12" CausesValidation="true"/>


   protected void OnChange(object sender, EventArgs e)
    {
         LoadListBox();
    }

    void LoadListBox()
    {
        list1.Items.Clear();

        System.Data.DataTable rows = new System.Data.DataTable();
        rows = DAL.GetValues();
        foreach (System.Data.DataRow row1 in rows.Rows)                 {
                list1.Items.Add(new ListItem(row1["measurement"].ToString().Trim(), row1["measurement"].ToString()));
        }
     }                                               

Upvotes: 3

Views: 512

Answers (1)

ozhug
ozhug

Reputation: 1083

when you change the listbox value to three this will fire the onchange event

In your onchange event you should could check the value to see what the value has changed to.

in your code you are reloading the list of values in the onchanged event which will reset the selected value back to zero.

Upvotes: 1

Related Questions