如何禁用<;选择>;标签(html下拉列表)使用asp.net下拉列表

本文关键字:下拉列表 使用 asp net html 标签 lt 何禁用 选择 gt | 更新日期: 2023-09-27 18:20:07

当我在下拉列表中选择并尝试禁用另一个下拉列表后,标签就会工作。

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

这是我的一些代码

在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)
{
}

抱歉语法错误:)

如何禁用<;选择>;标签(html下拉列表)使用asp.net下拉列表

为下拉列表创建另一个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()
}

原因是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";
        }
    }

或者,如果你想将该地址集放在这个更新面板之外,那么你必须将地址集添加到另一个更新面板中,并调用yourupdatepanel。Update();ddlPlayer_SelectedIndexChanged事件中的方法。