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

SelectectedIndexChangedただし、リスト項目を選択すると、コントロールはイベントに移動しません。ページをリロードした後にのみ機能します。

何が問題ですか?

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 に答える