【问题标题】:unable to hide label and gridview from java script in asp.net无法从 asp.net 中的 javascript 隐藏标签和 gridview
【发布时间】:2015-09-18 11:01:04
【问题描述】:

我想在 javascript 中隐藏 labelgrid view
我已经成功隐藏了 imagescheckboxes 但还没有隐藏 labelgrid view
用javascript怎么做?

javascript

<script type="text/javascript">
        function txtOnKeyPress(txt1)
         {
            if (txt1 != 'undefined') 
            {
             document.getElementById("GVPaymentDetails").style.display='none';
             document.getElementById('LblValidReg').style.display='none';

            }
         }
    </script>

aspx 代码

 <asp:TextBox ID="txtRegno" runat="server"  AutoPostBack="false"
            MaxLength="10"  onkeydown="txtOnKeyPress(this);"></asp:TextBox>



 <asp:GridView ID="GVPaymentDetails" runat="server" AutoGenerateColumns="False" 
            BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" 
            CellPadding="4" GridLines="Horizontal" EmptyDataText="No Records Found">
            <RowStyle BackColor="White" ForeColor="#333333" />
            <Columns>
                <asp:BoundField HeaderText="Enquiry id" DataField="EnquiryId" />
                <asp:BoundField HeaderText="Reg Number" DataField="RegistrationNumber" />
                <asp:BoundField HeaderText="Name" DataField="Name" />
            </Columns>
            <FooterStyle BackColor="White" ForeColor="#333333" />
            <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
        </asp:GridView>

 <asp:Label ID="LblValidReg" runat="server"></asp:Label>

【问题讨论】:

  • 为什么不将它们包装在divasp:Panel 中并隐藏它?那么你只处理一个对象而不是两个。
  • 但我无法隐藏其中任何一个@HugoYates
  • 我在 div 中保存了这个 id 的标签 - divValidReg@HugoYates
  • document.getElementById('divValidReg').style.display = "none";不工作
  • 使用浏览器控制台检查两个控件 ID。两个控件生成的 id 可能不同,因此您无法在 java 脚本中访问。在两个控件中使用 clientId Mode="static"。

标签: javascript c# asp.net gridview


【解决方案1】:

您在 .aspx 标记中看到的 ID 不是最终出现在客户端页面上的 ID。 ASP.NET 生成一堆东西来代替这个 ID。您拥有的选项:

  1. 如果您的 javascript 在同一个 .aspx 文件中,您可以将客户端 ID 嵌入其中,如下所示:

    document.getElementById("<%= GVPaymentDetails.ClientID %>").style.display='none';
    
  2. 如果你能100%保证页面上没有其他控件具有相同的ID,则使用静态ID模式,这将强制ASP.NET在客户端渲染完全相同的ID:

    <asp:GridView ID="GVPaymentDetails" ClientIDMode="Static"
    

    在这种情况下,您的代码将按原样运行:

    document.getElementById("GVPaymentDetails").style.display='none';
    

正如在 cmets 中已经指出的那样,将这些控件包装在某种容器(如 Panel 或 div)中会让您的生活更轻松。

更新

事实上,如果你只是在它们周围放置一个简单的 div,你就不需要上面的任何诡计了:

<div id="GridViewContainerDiv">
    <asp:GridView ID="GVPaymentDetails" ...
    <asp:Label ID="LblValidReg" ...
</div>

document.getElementById("GridViewContainerDiv").style.display='none';

【讨论】:

    【解决方案2】:
    <script type="text/javascript">
        function txtOnKeyPress(txt1)
         {
            if (txt1 != 'undefined') 
            {
             document.getElementById("<%= GVPaymentDetails.ClientId%>").style.display='none';
             document.getElementById('<%= LblValidReg.ClientId%>').style.display='none';
    
            }
         }
    </script>
    

    <asp:GridView ID="GVPaymentDetails" runat="server" AutoGenerateColumns="False" ClientIdMode="static" 
    
    
    <asp:Label ID="LblValidReg" ClientIdMode="Static" runat="server"></asp:Label>
    

    【讨论】:

    • 看起来是不是和其他答案一模一样,只是后来才出现?
    【解决方案3】:

    使用

    $("#<%=GVPaymentDetails.ClientID %>").css('display','none');
    $("#<%=LblValidReg.ClientID %>").css('display','none');
    

    安装

     document.getElementById("<%= GVPaymentDetails.ClientId%>").style.display='none';
     document.getElementById('<%= LblValidReg.ClientId%>').style.display='none';
    

    【讨论】:

    • 这假设 OP 可能没有使用 jquery。另外,style.display='none'怎么了?
    • style.display='none' 没问题,但是,我怀疑 document.getElementById 可能是使用母版页
    • 您的代码在内部的工作方式与 document.getElementById 完全相同。事实上,它调用它。这是 javascript,这里没有母版/内容页面,至少在 ASP.NET 意义上
    • 朋友。我发现它没有显示。如果有任何元素为 null 。它破坏了代码。并进一步不动请告诉如何检查null。如果 null 也应该进入下一条语句
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    • 2010-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多