如何使用asp.net下拉列表</select>禁用<select>标签(html下拉列表)

时间:2014-09-01 02:47:23

标签: c# asp.net

标签在我选择下拉列表后工作,我正在尝试禁用其他下拉列表。

但问题是,只有在点击提交按钮时才会触发请注意我的提交按钮还没有代码

以下是我的一些代码

在Option.aspx(客户端)

<select id="ddlgames" class="ddlgame" runat="server">
    <option value="" selected="selected">Select One</option>
    <option value="BasketBall">Basket Ball</option>
    <option value="VolleyBall">Volley Ball</option>
    <option value="FootBall">Foot Ball</option>
</select>

<asp:UpdatePanel ID="Update" runat="server">
    <ContentTemplate>
        <asp:DropDownListID="ddlPlayer"CssClass="Player" runat="server" AutoPostBack="True"  OnSelectedIndexChanged="ddlPlayer_SelectedIndexChanged">
        </asp:DropDownList> 
        <asp:Label ID="lblcomment" runat="server" Text="">
        </asp:Label> 
    </ContentTemplate> 
</asp:UpdatePanel>

<asp:Button ID="Submit" runat="server" OnClick="Submit_Click" Text="Submit" />

背后的代码

protected void Page_Load(object sender, EventArgs e)
{  
    if (!IsPostBack)
    {  
        ddlPlayer.Items.Add(new ListItem { Text = "Player", Value = "Y" });  
        ddlPlayer.Items.Add(new ListItem { Text = "Non Player", Value = "N" });
    }
}

protected void ddlNameOfReport_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ddlplayer.SelectedValue == "N")
    {
        ddlgames.Disabled = true;   
        lblcomment.Text = "Games option disabled";  
    }
    else
    {  
        ddlgames.Disabled = true;
        lblcomment.Text = "Games option enabled"; 
    } 
}

protected void Submit_Click(object sender, EventArgs e)
{
}
抱歉错误的语法:)

2 个答案:

答案 0 :(得分:0)

原因是ddlPlayer下拉列表位于更新面板中。 ddlgame位于更新面板的外部。

如果您在更新面板中添加ddlgame下拉列表并更改此类的ddlPlayer_SelectedIndexChanged事件

protected void ddlPlayer_SelectedIndexChanged(object sender, EventArgs e)
    {
        var ddlplayer = (DropDownList)sender;
        if (ddlplayer.SelectedValue == "N")
        {
            ddlgames.Disabled = true;
            lblcomment.Text = "Games option disabled";
        }
        else
        {
            ddlgames.Disabled = true; lblcomment.Text = "Games option enabled";
        }
    }

或者,如果您想将ddlgames保留在此更新面板的旁边,则必须将ddlgames添加到另一个更新面板并调用yourupdatepanel.Update(); ddlPlayer_SelectedIndexChanged事件中的方法。

答案 1 :(得分:0)

为您的下拉列表创建另一个UpdatePanel并添加AsyncPostBackTrigger

<asp:UpdatePanel ID="Update2" runat="server">
    <ContentTemplate>
<select id="ddlgames" class="ddlgame" runat="server">
    <option value="" selected="selected">Select One</option>
    <option value="BasketBall">Basket Ball</option>
    <option value="VolleyBall">Volley Ball</option>
    <option value="FootBall">Foot Ball</option>
</select>
</ContentTemplate> 
<Triggers>
 <asp:AsyncPostBackTrigger ControlID="ddlNameOfReport" />
  </Triggers>
</asp:UpdatePanel>

您的ddlPlayer_SelectedIndexChanged必须有Update2.Update()

protected void ddlNameOfReport_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ddlplayer.SelectedValue == "N")
    {
        ddlgames.Disabled = true;   
        lblcomment.Text = "Games option disabled";  
    }
    else
    {  
        ddlgames.Disabled = true;
        lblcomment.Text = "Games option enabled"; 
    } 
Update2.Update()

}