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(); }
精彩评论