虎克的博客

Enthusiasm Biogeography-Biodiversity Informatics-Data Sciences

如何获取GridView隐藏列的值

| Comments

对于经常使用GridView的朋友们可能经常遇到在GridView上绑定某些列的数据,用于进行嵌套控件从这些列中取值进行相应的操作,但是又不希望这些列的数据对客户端可见。我们经常的处理方法是将这些列的Visible属性设置为false。但是这样做就带来了对这些隐藏列的取值问题。下面就是解释如何处理这些隐藏列的两种情况,一是隐藏主键列字段的取值;二是隐藏非主键列字段的取值。两者实际上都是通过设置其字段为主键DataKeyNames属性来实现。数据库使用NorthWind数据库

运行效果

2009-05-25_000741

Default.aspx页面

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html PUBLIC "–//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>

<html xmlns="http://www.w3.org/1999/xhtml&quot;>

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:GridView ID="gvMaster" runat="server" AutoGenerateColumns="False" CellPadding="4"

            DataKeyNames="ProductID" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None"

            Width="266px" AllowPaging="True">

            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

            <RowStyle BackColor="#EFF3FB" />

            <Columns>

                <asp:CommandField ShowSelectButton="True" />

                <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"

                    ReadOnly="True" SortExpression="ProductID" Visible="False" />

                <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" InsertVisible="False"

                    ReadOnly="True" SortExpression="CategoryID" Visible="False" />

                <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />

                <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />

                <asp:TemplateField HeaderText="Select">

                    <ItemTemplate>

                        <asp:CheckBox ID="CheckBox1" runat="server" />

                    </ItemTemplate>

                </asp:TemplateField>

            </Columns></p

   original link:
   <a href='http://Apiaceae.github.io/blog/2009/05/25/%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96GridView%E9%9A%90%E8%97%8F%E5%88%97%E7%9A%84%E5%80%BC/'>http://Apiaceae.github.io/blog/2009/05/25/%E5%A6%82%E4%BD%95%E8%8E%B7%E5%8F%96GridView%E9%9A%90%E8%97%8F%E5%88%97%E7%9A%84%E5%80%BC/</a><br/>
   &nbsp;written by <a href='http://Apiaceae.github.io'>Hooker</a>
   &nbsp;posted at <a href='http://Apiaceae.github.io'>http://Apiaceae.github.io</a>
   </p>

Comments