Skip to main content
 首页 » 编程设计

c#之DropDown 不触发 OnSelectedIndexChanged

2024年02月13日15dflying

我有一个带有下拉列表的页面

   <asp:Content runat="server" ContentPlaceHolderID="ContentPlaceHolder1"> 
  <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> 
    <asp:ListItem>Cellsite</asp:ListItem> 
    <asp:ListItem>Agreement</asp:ListItem> 
    <asp:ListItem>Event</asp:ListItem> 
    <asp:ListItem>User</asp:ListItem> 
</asp:DropDownList> 
<asp:Label runat="server" ID="EntityName"></asp:Label> 
<ig:WebScriptManager ID="WebScriptManager1" runat="server"></ig:WebScriptManager> 
<ig:WebDataGrid ID="EntityGrid" runat="server"  Width="100%"> 
    <Behaviors> 
        <ig:Sorting> 
        </ig:Sorting> 
    </Behaviors> 
</ig:WebDataGrid> 

后面的代码是

     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
        EntityName.Text = DropDownList1.SelectedItem.Text; 
    } 

由于某些原因,标签从未更新,事件 selectedindexchanged 根本没有触发,我需要在此事件中添加一个动态网格。有什么线索吗?

请您参考如下方法:

您需要在下拉列表中添加 AutoPostBack

 <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true"> 

当您没有 AutoPostBack=true 属性时,您实际上可以看出没有回发。