C# 隐藏gridview中的某一列(在前台不显示),以及如何获取隐藏列的地址
1、在前台添加OnRowDataBound="gvShow_RowDataBound" 事件
<CimesUI:CimesGridView Width="60%" style="margin:0 auto;" ID="gvShow" runat="server" AutoGenerateColumns="False" OnRowDataBound="gvShow_RowDataBound" allowpaging="true" pagesize="30">
2、在后台的gvShow_RowDataBound设置要隐藏的列
protected void gvShow_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header) { // 隐藏表头的列 e.Row.Cells[3].Visible = false; // 根据索引隐藏列 } else if (e.Row.RowType == DataControlRowType.DataRow) { // 隐藏数据行的列 e.Row.Cells[3].Visible = false; // 根据索引隐藏列 } }
3、获取隐藏列的值,在RowDataBound里面添加如下代码
后台代码
protected void gvShow_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header) { // 隐藏表头的列 e.Row.Cells[3].Visible = false; // 根据索引隐藏列 } else if (e.Row.RowType == DataControlRowType.DataRow) {
//这里的Reliability_Picture_SID就是隐藏的列,需根据实际情况调整
string reliabilityPictureSid = DataBinder.Eval(e.Row.DataItem, "Reliability_Picture_SID").ToString();
// 创建一个隐藏字段并将Reliability_Picture_SID的值赋给它
HiddenField hiddenField = new HiddenField();
hiddenField.Value = reliabilityPictureSid; // 将隐藏字段添加到行中
e.Row.Cells[0].Controls.Add(hiddenField);
// 隐藏数据行的列
e.Row.Cells[3].Visible = false;
}
}
前台代码
<CimesUI:CimesGridView ID="gvColse" runat="server" AutoGenerateColumns="False" CssClass="centered-gridview" DataKeyNames="Reliability_Picture_SID" OnRowDeleting="gvColse_RowDeleting" OnRowDataBound="gvColse_RowDataBound"> <Columns> <asp:CommandField HeaderText="操作" ShowDeleteButton="True"> <ItemStyle HorizontalAlign="Center" /> </asp:CommandField> <asp:TemplateField HeaderText="操作" HeaderStyle-Width="50px"> <ItemTemplate> <a href="javascript:;" onclick="return DownLoadCloseFile(this)">下载</a> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Reliability_Picture_SID" HeaderText="Reliability_Picture_SID" /> <asp:BoundField DataField="AbnormalCause" HeaderText="异常分类" /> <asp:BoundField DataField="AbnormalDevice" HeaderText="异常制程" /> <asp:BoundField DataField="AbnormalProcess" HeaderText="异常器件" /> <asp:BoundField DataField="CloseReasonDescr" HeaderText="具体原因描述" /> <asp:BoundField DataField="fileName" HeaderText="分析报告" /> </Columns> <EmptyDataTemplate> 没有附档存在! </EmptyDataTemplate> </CimesUI:CimesGridView>
1 2 3 4 5 6 7 8 9 10 11 12 13 | function DownLoadCloseFile(UserLink) { console.log( "1222222222223" ); var row = UserLink.parentNode.parentNode; console.log(row); var id = row.cells[1].innerHTML; console.log(id); let method = "CloseFile" var hiddenField = row.querySelector( 'input[type="hidden"]' ); if (hiddenField) { // 获取隐藏字段的值,即Reliability_Picture_SID var reliabilityPictureSid = hiddenField.value; } } |
分类:
C#
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix