简体   繁体   English

如何在列表框项目上执行方法选择asp.net

[英]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 . 需要从列表框中选择一个项目,然后在选择一个方法时执行load_data(),加载并显示数据,但是刷新页面之后,再次将显示的值设置为default。 Its autopostback property is true . 其autopostback属性为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... 如果您最初是在Page_Load方法中对ListBox进行数据绑定,请确保仅在它不是这样的回发时才进行数据绑定...

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 您仍然可以保留SelectedIndexChanged事件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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