简体   繁体   English

dropdownlist不会在SelectedItemChanged上触发

[英]dropdownlist won't fire on SelectedItemChanged

I have a DDL within an update panel and I cannot get the SelectedItemChanged method to fire when I bind data to it. 我在更新面板中有一个DDL,并且在将数据绑定到它时无法启动SelectedItemChanged方法。 In Page_Load I have: 在Page_Load中,我有:

ddl.DataSource = GetList();
ddl.DataBind();

GetList() returns a List<string> GetList()返回一个List<string>

The values are all there but nothing happens when I select one. 这些值都存在,但是当我选择一个时什么也没有发生。

If I am to manually put them in like so: 如果我要像这样手动放置它们:

ddl.Items.Add("1");
ddl.Items.Add("2");
ddl.Items.Add("3");

it works, why? 它有效,为什么呢?

The method below is never entered into if I bind the data. 如果我绑定数据,则永远不会输入以下方法。

protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        string test = ddl.SelectedItem.Text;
    }

<asp:DropDownList runat="server" ID="ddl" Width="150px"
OnSelectedIndexChanged="ddl_SelectedIndexChanged"></asp:DropDownList>

By default the Change asp:DropDownList does not do postback like button as that is not required in many cases. 默认情况下,Change asp:DropDownList不会像按钮那样回发,因为在许多情况下不需要这样做。 You have to set it to true to get the postback. 您必须将其设置为true才能获得回发。

Change 更改

<asp:DropDownList runat="server" ID="ddl" Width="150px"
OnSelectedIndexChanged="ddl_SelectedIndexChanged"></asp:DropDownList>

To

<asp:DropDownList runat="server" ID="ddl" Width="150px" AutoPostBack="true"
OnSelectedIndexChanged="ddl_SelectedIndexChanged"></asp:DropDownList>

请在DropDownList上设置属性AutoPostBack =“ true”(如果尚未设置),这将允许下拉列表回发任何SelectedIndex更改

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

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