开发者

get the new selectedValue of the listBox using the OnSelectedIndexChanged event

开发者 https://www.devze.com 2023-03-16 20:09 出处:网络
I have a listBox in a asp.net web form. OnLoad i add items to the listbox and i add a OnSelectedIndexChanged event handler:

I have a listBox in a asp.net web form. OnLoad i add items to the listbox and i add a OnSelectedIndexChanged event handler:

public void OnSelectedIndexChanged(object sender, EventArgs eventArgs)
{
    StreamWriter sw = new StreamWriter(@"C:\Users\me\Desktop\log.txt");

    sw.WriteLine(listBox.SelectedValue);

    sw.Flush();
    sw.Close();
}

The stream writer is so i can see the output...

I have AutoPostBack set to true on the listbox but everytime i select a new ite开发者_开发技巧m it reloads the page but the print out is always the first item in the list instead of the item i clicked on. Anyone have an idea of what im doing wrong?

Thanks


OnLoad runs every time the page is loaded. Check Page.IsPostBack to make sure you are only adding items to the list the first time you load the page. i.e.

if (!Page.IsPostBack) { FillMyListbox(); }
0

精彩评论

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

关注公众号