【问题标题】:change visibility of table tr on dropdown value更改表 tr 在下拉值上的可见性
【发布时间】:2023-02-03 18:35:03
【问题描述】:

我在使用 c# 的 asp.net web 应用程序中工作。以下是设计视图

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"      
   Inherits="WebApplication18.WebForm1" %>

    <!DOCTYPE html>

      <html xmlns="http://www.w3.org/1999/xhtml">
      <head runat="server">
      <title></title>
     </head>
     <body>
          <form id="form1" runat="server">
           <asp:ScriptManager ID="sc1" runat="server"></asp:ScriptManager>
          <div>
                <table id="tblid" runat="server">
                    <tr id="tr11" runat="server">
                     <td>
                            <asp:Label ID="lblselect" runat="server" Text="Select List Type">  
                          </asp:Label>         
                     </td>
                     <td>
                     <asp:UpdatePanel ID="upd" runat="server" UpdateMode="Conditional">
                        <ContentTemplate>
                            <asp:DropDownList ID="ddllistType" runat="server"   
                AutoPostBack="true" OnSelectedIndexChanged="ddllistType_SelectedIndexChanged">
                                <asp:ListItem Text="select" Value="0"></asp:ListItem>
                                <asp:ListItem Text="Agent" Value="1"></asp:ListItem>
                                <asp:ListItem Text="Customer" Value="2"></asp:ListItem>
                                <asp:ListItem Text="Branch" Value="3"></asp:ListItem>
                            </asp:DropDownList>
                        </ContentTemplate>
                    </asp:UpdatePanel>                        
                </td>
            </tr>
            <tr id="trlic" runat="server" style="display:none;">
                <td>
                    <asp:Label ID="lblvalue" runat="server" Text="Item Types"></asp:Label>
                </td>
                <td>
                    <asp:CheckBoxList ID="cblItemType" runat="server">
                        <asp:ListItem Text="Premium" Value="1"></asp:ListItem>
                        <asp:ListItem Text="Budget" Value="2"></asp:ListItem>
                        <asp:ListItem Text="Normal" Value="3"></asp:ListItem>
                    </asp:CheckBoxList>
                </td>
            </tr>
        </table>
    </div>
</form>
</body>
</html>

下面是我的代码背后

protected void ddllistType_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        
        if (ddllistType.SelectedValue.ToString().Equals("1"))
        {
            trlic.Style.Add("display", "none");
        }
        else
        {
            trlic.Style.Add("display", "block");
        }
    }
    catch (Exception ex)
    {
    }
}

当用户将从 ddllistType 中选择代理时,trlist 应该隐藏,否则应该显示。但是上面的代码不起作用。请在这里帮助我。

【问题讨论】:

    标签: c#


    【解决方案1】:

    您正在使用 Style.Add("display", "none") 来隐藏和显示元素,但我认为您应该改用 Visible 属性。

    protected void ddllistType_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (ddllistType.SelectedValue.ToString().Equals("1"))
            {
                trlic.Visible = false;
            }
            else
            {
                trlic.Visible = true;
            }
    
            sc1.Update();
        }
        catch (Exception ex)
        {
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-30
      • 2012-09-22
      • 1970-01-01
      • 2018-04-05
      • 1970-01-01
      • 2015-05-12
      • 2011-10-08
      相关资源
      最近更新 更多