2

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

标记:

<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>

代码隐藏:

protected void SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList dropDownList = (DropDownList)sender;
    if (dropDownList.SelectedValue == "Other")
    {
        tb_other.Enabled = true;
        tb_other.Text = string.Empty;
        tb_other.Visible = true;
    }
    else
    {
        tb_other.Enabled = false;
        tb_other.Text = dropDownList.SelectedValue;
    }
}

but when selecting on any list item ,control doesn't go to SelectectedIndexChangedevent. 只有在重新加载页面后它才能工作。

问题是什么?

4

2 回答 2

4

要将您的DropDownList帖子发回服务器,您需要使用该AutoPostback属性,如下所示:

<asp:DropDownList ID="dl_test_name" runat="server" 
                  OnSelectedIndexChanged="SelectedIndexChanged" 
                  Height="22px" Width="103px" AutoPostBack="true">
于 2013-09-07T05:18:16.820 回答
3

Arathy,在 aspx 中将下拉菜单的 AutopostBack 属性设置为 true

于 2013-09-07T05:12:40.513 回答