C# 将选项添加到下拉列表

C# 将选项添加到下拉列表,c#,asp.net,C#,Asp.net,我有一个包含多个值的下拉列表。我有一个名为“其他”的列表项。选择“其他”时,我希望生成一个带有必需字段验证器的文本框。我是这样写的: 标记: <asp:DropDownList ID="dl_test_name" runat="server" OnSelectedIndexChanged="SelectedIndexChanged" Height="22px" Width="103px"> <a

我有一个包含多个值的下拉列表。我有一个名为“其他”的列表项。选择“其他”时,我希望生成一个带有必需字段验证器的文本框。我是这样写的:

标记:

<asp:DropDownList ID="dl_test_name" runat="server" 
                  OnSelectedIndexChanged="SelectedIndexChanged" 
                  Height="22px" Width="103px">
    <asp:ListItem>Science</asp:ListItem>
    <asp:ListItem>Maths</asp:ListItem>          
    <asp:ListItem>Other</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="tb_other" runat="server" Width="94px" Visible="False">
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" 
                            ControlToValidate="tb_other" ErrorMessage="*">   
</asp:RequiredFieldValidator>
但在任何列表项上选择时,控件不会转到
selectedIndexChanged
事件。只有在重新加载页面后,它才能工作


问题是什么?

请在aspx中将dropdown的AutopostBack属性设置为true

要使您的
DropDownList
回发到服务器,您需要使用
AutopostBack
属性,如下所示:

<asp:DropDownList ID="dl_test_name" runat="server" 
                  OnSelectedIndexChanged="SelectedIndexChanged" 
                  Height="22px" Width="103px" AutoPostBack="true">

ohh,我忘了。谢谢大家!@安克尔·卡尔·安德森
<asp:DropDownList ID="dl_test_name" runat="server" 
                  OnSelectedIndexChanged="SelectedIndexChanged" 
                  Height="22px" Width="103px" AutoPostBack="true">