开发者

Listbox always returns 0

开发者 https://www.devze.com 2023-04-04 00:57 出处:网络
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开发者_JS百科 you. Here is my c

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开发者_JS百科 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()));
        }
     }                                               


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消