简体   繁体   中英

How to execute method on listbox item select asp.net

Need to select an item from listbox and on select a method is executed load_data() , the data is loaded and displayed but instead after that the page gets refreshed and again the value displayed is set to default . Its autopostback property is true . What should I do ?

 protected void ListBox1_SelectedIndexChanged1(object sender, EventArgs e)
    {
        load_data(listBox1.SelectedItem.Text);
    }

If you are initially databinding your ListBox in your Page_Load method, make sure you only databind when it's not a postback like this...

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // initial databind ListBox here (where the default data is loaded)
    }
}

You can still keep your SelectedIndexChanged event

protected void ListBox1_SelectedIndexChanged1(object sender, EventArgs e)
{
    load_data(listBox1.SelectedItem.Text);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM