【问题标题】:adding option to drop down list添加选项到下拉列表
【发布时间】:2013-09-07 05:10:52
【问题描述】:

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

标记:

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

但在选择任何列表项时,控制不会转到SelectectedIndexChanged事件。只有在重新加载页面后它才能工作。

有什么问题?

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 哦,我忘记了。谢谢 ! @Ankur 卡尔安德森
      猜你喜欢
      • 1970-01-01
      • 2012-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 2016-08-25
      • 2010-11-09
      相关资源
      最近更新 更多