asp.net中在Gridview中自定义一列操作,应该怎么做?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了asp.net中在Gridview中自定义一列操作,应该怎么做?相关的知识,希望对你有一定的参考价值。

我想在每行后面加一个“加密”操作。对该行中的一个数据加密,应该怎么实现?

思路:先定义事件,在linkbutton上绑定事件,也就是一个CRUD的方法,然后在后台的OnRowCommand事件里调用绑定的事件即可。
具体举例如下:
1、前端的代码如下:
<asp:GridView ID="gridViewDxjk" CssClass="gridview" runat="server" AllowPaging="True"
DataKeyNames="P_ID" AutoGenerateColumns="False"
RowStyle-HorizontalAlign="Center" BorderWidth="1px" PageSize="17"
onrowdeleting="gridViewDxjk_RowDeleting"
OnRowDataBound="gridViewDxjk_RowDataBound"
onpageindexchanging="gridViewDxjk_PageIndexChanging" >
<HeaderStyle CssClass="head" />
<PagerStyle CssClass="pager" />
<RowStyle CssClass="row" />
<EditRowStyle CssClass="editrow" />
<AlternatingRowStyle CssClass="altrow" />
<EmptyDataRowStyle CssClass="empty" />
<Columns>
<asp:HyperLinkField HeaderText="编辑" ControlStyle-Width="50" DataNavigateUrlFields="P_ID" DataNavigateUrlFormatString="smsModify.aspx?id=0" Text="编辑" >
<ControlStyle Width="50px"></ControlStyle></asp:HyperLinkField>
<asp:CommandField ShowDeleteButton="true" DeleteText="删除" >
<ControlStyle Width="50px"></ControlStyle></asp:CommandField>
<asp:BoundField DataField="P_ID" HeaderText="id" SortExpression="P_ID" ItemStyle-HorizontalAlign="Center" Visible="False" >
<ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
<asp:BoundField DataField="P_NAME" HeaderText="名称" SortExpression="P_NAME" />
<asp:BoundField DataField="P_Type" HeaderText="通知方式" SortExpression="P_Type" ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
<asp:BoundField DataField="P_Fzr" HeaderText="姓名" SortExpression="P_Fzr" ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
<asp:BoundField DataField="P_tel" HeaderText="通知手机" SortExpression="P_tel" ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
<asp:BoundField DataField="P_jg" HeaderText="通知间隔(小时)" SortExpression="P_jg" ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
<asp:BoundField DataField="P_on" HeaderText="是否开启" SortExpression="P_on" ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
<asp:BoundField DataField="P_lasttime" HeaderText="最后发送时间" SortExpression="P_lasttime" ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
<asp:BoundField DataField="P_memo" HeaderText="备注" SortExpression="P_memo" ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle></asp:BoundField>
</Columns>
<EmptyDataTemplate>
没有数据!
</EmptyDataTemplate>
<PagerTemplate>
<table class="gvPage" style="font-size:12px;">
<tr>
<td style="text-align: right">
第<asp:Label ID="lblPageIndex" runat="server" Text=\'<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>\'></asp:Label>页
/共<asp:Label ID="lblPageCount" runat="server" Text=\'<%# ((GridView)Container.Parent.Parent).PageCount %>\'></asp:Label>页
<asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">首页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev" CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">上一页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">下一页</asp:LinkButton>
<asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">尾页</asp:LinkButton>
<asp:TextBox ID="txtNewPageIndex" runat="server" Text=\'<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>\' Width="20px" AutoPostBack="true" ></asp:TextBox>
<asp:LinkButton ID="btnGoEx" runat="server" CommandArgument="GO" CommandName="Page" Text="GO" OnClick="btnGoEx_Click"></asp:LinkButton>
</td>
</tr>
</table>
</PagerTemplate>
</asp:GridView>
2、后端的话,由于需要弹出删除前的确认框,所以,这里需要在RowDataBound里面做点逻辑,同时,要想真正的删除,还需要触发RowDeleting事件,具体代码如下:
//报警删除
protected void gridViewDxjk_RowDeleting(object sender, GridViewDeleteEventArgs e)

string key = gridViewDxjk.DataKeys[e.RowIndex].Value.ToString();
bool flag = bll.Delete(Int32.Parse(key));
if (flag)
NXT_WLService.App_Code.JScript.Alert("删除成功!", this);
else
NXT_WLService.App_Code.JScript.Alert("删除失败!", this);


protected void gridViewDxjk_RowDataBound(object sender, GridViewRowEventArgs e)

if (e.Row.RowType == DataControlRowType.DataRow)

LinkButton btn = (LinkButton)e.Row.Cells[1].Controls[0];
if (btn.Text.Equals("删除"))//删除钮才加提示讯息
btn.OnClientClick = "if (confirm(\'你确认要删除?\')) javascript:__doPostBack(\'gridViewDxjk\',\'Delete$" + e.Row.RowIndex.ToString() + "\'); else return false;";

参考技术A 前台gridview里
<asp:TemplateField HeaderText="操作" >
<ItemTemplate>
<asp:LinkButton ID="lnkname" runat="server" CommandArgument='<%# Eval("Id") %>' CommandName="Update" Text="加密"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

后台的OnRowCommand事件里
protected void OnRowCommand(object sender, GridViewCommandEventArgs e)

if (e.CommandName=="Update" && StringtoNum.IsNum(e.CommandArgument.ToString()))

int Id = int.Parse(e.CommandArgument.ToString());
//加密代码

以上是关于asp.net中在Gridview中自定义一列操作,应该怎么做?的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET Gridview 与动态网格上的链接按钮列

获取 ASP.NET GridView 单元格值并在 JQuery 进度条中显示

asp.net c#在gridview添加dropdownlist

ASP.net GridView 的小问题

我可以在 asp.net(vb) 中自定义 ajax 文件上传吗?

asp.net中GridView怎样进行分页,编辑,删除操作