对于经常使用GridView的朋友们可能经常遇到在GridView上绑定某些列的数据,用于进行嵌套控件从这些列中取值进行相应的操作,但是又不希望这些列的数据对客户端可见。我们经常的处理方法是将这些列的Visible属性设置为false。但是这样做就带来了对这些隐藏列的取值问题。下面就是解释如何处理这些隐藏列的两种情况,一是隐藏主键列字段的取值;二是隐藏非主键列字段的取值。两者实际上都是通过设置其字段为主键DataKeyNames属性来实现。数据库使用NorthWind数据库
运行效果
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">
<html xmlns="http://www.w3.org/1999/xhtml">
<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>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice], [CategoryID] FROM [Products]">
</asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>
</body>
</html>
注意上面划横线的部分,ProductID字段设置为隐藏,取值操作前需要将其设置为DataKeyNames主键。同样,如果想取CategoryID,则把DataKeyNames设置为=CategoryID就行了。
Default.aspx.cs页面
using System;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string str = String.Empty;
int rowNo = 0;
foreach (GridViewRow row in gvMaster.Rows)
{
bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;
if (isChecked)
{
// Gets the name of the primary key column our primary key is the PersonID
string hidecolumn = gvMaster.DataKeyNames[0];
int hidecolunmValue = (int)gvMaster.DataKeys[rowNo].Value;
Response.Write(hidecolunmValue+"<br/>");
}
// increment the row count
rowNo++;
}
Response.Write(str);
}
}
原为地址:http://www.highoncoding.com/Articles/87_Accessing_Invisible_Columns_of_GridView_Control.aspx