Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listbox always returns 0

Tags:

c#

asp.net

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()));
        }
     }                                               
like image 321
nav100 Avatar asked Feb 15 '26 06:02

nav100


1 Answers

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.

like image 90
ozhug Avatar answered Feb 16 '26 20:02

ozhug



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!