隐藏gridview列并在单击时获取它

时间:2016-04-24 12:52:22

标签: jquery asp.net gridview

我正在尝试学习使用asp.net的jquery,我有一个gridview。我想在gridview中隐藏userId,但获取点击行的Id值。这是我的gridview:

<asp:GridView ID="addGridControl" CssClass="mGrid"  PagerStyle-CssClass="pgr" AllowPaging="true" PageSize="10" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="EntityDataSource1">
        <Columns>
            <asp:TemplateField Visible="false">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%#Eval("CustomerId") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName"></asp:BoundField>
            <asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName"></asp:BoundField>
            <asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle" SortExpression="ContactTitle"></asp:BoundField>
            <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address"></asp:BoundField>
            <asp:BoundField DataField="City" HeaderText="City" SortExpression="City"></asp:BoundField>
            <asp:BoundField DataField="Region" HeaderText="Region" SortExpression="Region"></asp:BoundField>
            <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode"></asp:BoundField>
            <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country"></asp:BoundField>
            <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone"></asp:BoundField>
            <asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax"></asp:BoundField>
        </Columns>
    </asp:GridView>

这是我的jquery尝试:

$("#addGridControl tr").click(function () {
        var secilen = $(this).closest('tr').find("#Label1").attr("Text");
        alert(secilen);
    });

我相信你可以看到它在第一眼看上去不起作用。这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

你应该替换:

<div id= "title">
  <h1>Projects Home</h1>
</div>
<div id= "nav">
  <ul>
    <li><a href="">Project 1</a></li>
    <li><a href="">Project 2</a></li>
    <li><a href="">Project 3</a></li>
  </ul>   
</div>

通过

<asp:TemplateField Visible="false">

使用班级风格:

<asp:TemplateField HeaderStyle-CssClass="notVisible" ItemStyle-CssClass="notVisible" FooterStyle-CssClass="notVisible">

.notVisible { display: none; } 属性设置为Visible时,ASP.NET将从HTML输出中删除控件。因此,客户端代码将无法找到它。