<%
@ Page Language
=
"
C#
"
%>
<%
@ Import Namespace
=
"
System.Data
"
%>
<%--
http:
//
community.csdn.net/Expert/TopicView3.asp?id=5646507--%>
<!
DOCTYPE html PUBLIC
"
-//W3C//DTD XHTML 1.0 Transitional//EN
"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"
>
<
script runat
=
"
server
"
>
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(
!
IsPostBack) {
LoadProductData();
}
}
protected
void
GridView1_RowDataBound(
object
sender, GridViewRowEventArgs e)
{
//
客户端 CSS 控制隐藏,不安全,毕竟数据还是呈现到客户端了
e.Row.Cells[
3
].Style.Add(HtmlTextWriterStyle.Display,
"
none
"
);
//
设置单元格隐藏, TableCell.Visible=false, OK
e.Row.Cells[
4
].Visible
=
false
;
}
protected
void
GridView1_RowCommand(
object
sender, GridViewCommandEventArgs e)
{
int
rowIndex
=
-
1
;
switch
(e.CommandName) {
case
"
Select
"
:
rowIndex
=
Convert.ToInt32(e.CommandArgument);
GridViewRow row
=
GridView1.Rows[rowIndex];
Response.Write(String.Format(
"
<pre style='color:red'>您选择了第 {0} 行\n当前行 ProductId={1}, CategoryId={2}(两者均由DataKeys获取)\n
"
,
(row.RowIndex
+
1
),
GridView1.DataKeys[rowIndex].Values[
"
ProductId
"
],
GridView1.DataKeys[rowIndex].Values[
"
CategoryId
"
]));
for
(
int
columnIndex
=
0
; columnIndex
<
GridView1.Columns.Count; columnIndex
++
) {
DataControlField field
=
GridView1.Columns[columnIndex];
string
text
=
null
;
if
(field
is
BoundField) {
text
=
row.Cells[columnIndex].Text;
}
else
if
(field
is
TemplateField) {
Label lbl
=
row.Cells[columnIndex].FindControl(
"
lblProductID
"
)
as
Label;
if
(lbl
!=
null
) {
text
=
lbl.Text;
}
else
{
text
=
row.Cells[columnIndex].Text;
}
}
Response.Write(String.Format(
"
{0}#Type={1}#Visible={2}#CanGetText={3}#Text={4}\n
"
,
field.HeaderText, field.GetType().Name.ToString(), field.Visible,
!
String.IsNullOrEmpty(text), text));
}
Response.Write(
"
</pre>
"
);
break
;
}
}
void
LoadProductData()
{
DataTable dt
=
CreateProductTable();
GridView1.DataSource
=
dt;
GridView1.DataBind();
}
#region
sample data
static
DataTable CreateProductTable()
{
DataTable tbl
=
new
DataTable(
"
Products
"
);
tbl.Columns.Add(
"
ProductID
"
,
typeof
(
int
));
tbl.Columns.Add(
"
ProductName
"
,
typeof
(
string
));
tbl.Columns.Add(
"
CategoryID
"
,
typeof
(
int
));
DataRow row
=
tbl.NewRow();
row[
0
]
=
1
;
row[
1
]
=
"
Chai
"
;
row[
2
]
=
1
;
tbl.Rows.Add(row);
row
=
tbl.NewRow();
row[
0
]
=
2
;
row[
1
]
=
"
Chang
"
;
row[
2
]
=
1
;
tbl.Rows.Add(row);
row
=
tbl.NewRow();
row[
0
]
=
3
;
row[
1
]
=
"
Aniseed Syrup
"
;
row[
2
]
=
2
;
tbl.Rows.Add(row);
row
=
tbl.NewRow();
row[
0
]
=
4
;
row[
1
]
=
"
Chef Anton's Cajun Seasoning
"
;
row[
2
]
=
2
;
tbl.Rows.Add(row);
row
=
tbl.NewRow();
row[
0
]
=
5
;
row[
1
]
=
"
Chef Anton's Gumbo Mix
"
;
row[
2
]
=
2
;
tbl.Rows.Add(row);
row
=
tbl.NewRow();
row[
0
]
=
47
;
row[
1
]
=
"
Zaanse koeken
"
;
row[
2
]
=
3
;
tbl.Rows.Add(row);
row
=
tbl.NewRow();
row[
0
]
=
48
;
row[
1
]
=
"
Chocolade
"
;
row[
2
]
=
3
;
tbl.Rows.Add(row);
row
=
tbl.NewRow();
row[
0
]
=
49
;
row[
1
]
=
"
Maxilaku
"
;
row[
2
]
=
3
;
tbl.Rows.Add(row);
return
tbl;
}
#endregion
protected
void
Button1_Click(
object
sender, EventArgs e)
{
LoadProductData();
}
</
script
>
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
<
head runat
=
"
server
"
>
<
title
>
Demo6_AccessHiddenGridViewColumnValue
</
title
>
</
head
>
<
body
>
<
form id
=
"
form1
"
runat
=
"
server
"
>
<
div
>
<
asp:GridView ID
=
"
GridView1
"
DataKeyNames
=
"
ProductId,CategoryId
"
runat
=
"
server
"
AutoGenerateColumns
=
"
False
"
OnRowCommand
=
"
GridView1_RowCommand
"
OnRowDataBound
=
"
GridView1_RowDataBound
"
CellPadding
=
"
4
"
ForeColor
=
"
#333333
"
>
<
Columns
>
<
asp:BoundField DataField
=
"
ProductID
"
HeaderText
=
"
列0 正常状态
"
/>
<
asp:BoundField DataField
=
"
ProductID
"
HeaderText
=
"
列1 直接设置Visible=false 无法获取值
"
Visible
=
"
False
"
/>
<
asp:BoundField DataField
=
"
ProductID
"
HeaderText
=
"
列2 width=0 客户端依然呈现无效
"
>
<
ItemStyle Width
=
"
0px
"
/>
</
asp:BoundField
>
<
asp:BoundField DataField
=
"
ProductID
"
HeaderText
=
"
列3 客户端 CSS 控制隐藏,不安全,毕竟数据还是呈现到客户端了
"
/>
<
asp:BoundField DataField
=
"
ProductID
"
HeaderText
=
"
列4 设置单元格隐藏, TableCell.Visible=false, OK
"
/>
<
asp:TemplateField HeaderText
=
"
列5 模板列直接调用 Eval
"
Visible
=
"
False
"
>
<
ItemTemplate
>
<%
# Eval(
"
ProductID
"
)
%>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField HeaderText
=
"
列6 模板列嵌入 Label
"
Visible
=
"
False
"
>
<
ItemTemplate
>
<
asp:label ID
=
"
lblProductID
"
runat
=
"
server
"
Text
=
'
<%# Eval("ProductID") %>
'
/>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:BoundField DataField
=
"
ProductName
"
HeaderText
=
"
列7
"
/>
<
asp:ButtonField CommandName
=
"
Select
"
Text
=
"
Select
"
HeaderText
=
"
列8 按钮
"
/>
</
Columns
>
<
FooterStyle BackColor
=
"
#990000
"
Font
-
Bold
=
"
True
"
ForeColor
=
"
White
"
/>
<
RowStyle BackColor
=
"
#FFFBD6
"
ForeColor
=
"
#333333
"
/>
<
SelectedRowStyle BackColor
=
"
#FFCC66
"
Font
-
Bold
=
"
True
"
ForeColor
=
"
Navy
"
/>
<
PagerStyle BackColor
=
"
#FFCC66
"
ForeColor
=
"
#333333
"
HorizontalAlign
=
"
Center
"
/>
<
HeaderStyle BackColor
=
"
#990000
"
Font
-
Bold
=
"
True
"
ForeColor
=
"
White
"
/>
<
AlternatingRowStyle BackColor
=
"
White
"
/>
</
asp:GridView
></
div
>
<
asp:Button ID
=
"
Button1
"
runat
=
"
server
"
OnClick
=
"
Button1_Click
"
Text
=
"
重新绑定数据
"
/>
</
form
>
</
body
>
</
html
>