惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 最新话题
小众软件
小众软件
C
Check Point Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Visual Studio Blog
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The GitHub Blog
The GitHub Blog
T
Tailwind CSS Blog
Recorded Future
Recorded Future
雷峰网
雷峰网
WordPress大学
WordPress大学
A
Arctic Wolf
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Project Zero
Project Zero
T
Tor Project blog
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Spread Privacy
Spread Privacy
G
Google Developers Blog
Security Latest
Security Latest
MongoDB | Blog
MongoDB | Blog
T
Threatpost
I
InfoQ
T
Tenable Blog
T
The Exploit Database - CXSecurity.com
S
Security Affairs
H
Hackread – Cybersecurity News, Data Breaches, AI and More
人人都是产品经理
人人都是产品经理
C
Cisco Blogs
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
aimingoo的专栏
aimingoo的专栏
C
CXSECURITY Database RSS Feed - CXSecurity.com
美团技术团队
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
D
Docker
博客园 - 【当耐特】
大猫的无限游戏
大猫的无限游戏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Vercel News
Vercel News
PCI Perspectives
PCI Perspectives
Microsoft Azure Blog
Microsoft Azure Blog
C
CERT Recently Published Vulnerability Notes
月光博客
月光博客
Cloudbric
Cloudbric
A
About on SuperTechFans
F
Fortinet All Blogs

博客园 - nect

【转】ie9 rc版软件兼容问题 [z]前端设计IE6/IE7/IE8/IE9/FF问题汇总:IE和FirFox兼容问题 - nect - 博客园 [z]ie和FF 在insertRow和insertCell的区别 [z]JS在IE和FF下attachEvent,addEventListener学习笔记 - nect - 博客园 WinForm使用webclient通过http的POST方式上传文件 转:另类解决:“ScriptManager”不是已知元素。原因可能是网站中存在编译错误。 - nect - 博客园 转:css妙用1-用图片替换文字 .net中的正则表达式(一)——转义字符 使用Cascadingdropdown控件遇到的一个问题 IP地址匹配算法 服务器控件的客户端验证脚本 从一个字符串中Remove另一个字符串 gridview 中模板列无法响应row_command事件 - nect - 博客园 在asp.net Atlas中调用 Web Service时无法找到自定义的Web Service对象的可能原因 【转】Prototype.js开发笔记//mark一下 在GridView中使用邮件链接 GridView 中格式化整理 [转]操纵自如--页面内的配合与通信 [导入]Java调用.net的WebService
【转】ASP.net2。0中解决无法获取 GridView 隐藏列值问题
nect · 2007-10-23 · via 博客园 - nect

在 GridView/DetailsView 中如果 BoundField 的 Visible=false 时,  回发的时候无法此列的值(GridViewRow.Cells[cellIndex].Text将为空),网上很多朋友提出了各种各样的解决方案,这里整理一下,并提供示例。

未反射 GridView 类,不曾仔细阅读其源码,不知内部实现对于 BoundField(普通绑定列),当此列 Visible=false 时,是未执行绑定计算,还是未保持 ViewState,也许这是就是传说的GridView性能由于DataGrid的一点吧。事实上,这样反而给粗心的开发者带来了“莫名其妙”的问题。DataGrid 中 BoundColumn 不存在此问题。

MSDN 对此是这样的说明:

备注

使用 Visible 属性显示或隐藏数据绑定控件中的 DataControlField 对象。

如果 Visible 属性为 false,则不显示数据值并且不做到客户端的往返行程。如果要往返不可见字段的数据,请将字段名添加到数据绑定控件的 DataKeyNames 属性。

http://msdn2.microsoft.com/zh-cn/library/system.web.ui.webcontrols.datacontrolfield.visible(VS.80).aspx
说明:BoundField 类继承自 DataControlField 类。

事实上,实际项目中,我几乎没有使用隐藏列的经验,即使在 1.x 的 DataGrid 中,为了记录某些有用的隐藏信息,我一般使用模板列中嵌套控件,如label 并设置其visible=false 最佳当然是用 input type=hidden runat=server(注:1.x 中没有 asp:hiddenfield 控件)。
而 2.0 ,最佳的方案,当然是使用 DataKeys 来存储,不像DataGrid.DataKey ,GridView/DetailsView.DataKeys 可以存储多个值。

以下为示例代码,代码包含各种方案的“自说明”注释,不再做过多的解释。


<%@ 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();
    }


    
sample data  
    
    
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>

示例下载