最近用到GridView自带的编辑、更新、删除、取消按钮,研究了一番其中包括绑定gridview数据、编辑行内数据、删除数据、根据条件隐藏显示编辑删除或自定义按钮,把代码共享一下,帮助有需要的人:

效果图1(根据审核未审核显示编辑删除按钮):

效果图2:(审核后隐藏编辑删除按钮)

前台代码:

<asp:GridView ID="GVData" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"BorderStyle="Solid" BorderWidth="1px" OnRowDataBound="GVData_RowDataBound" PageSize="15"Width="100%"  OnRowCancelingEdit="GVData_RowCancelingEdit" OnRowEditing="GVData_RowEditing" OnRowUpdating="GVData_RowUpdating" OnRowDeleting="GVData_RowDeleting"><PagerSettings Mode="NumericFirstLast" Visible="False" /><PagerStyle BackColor="LightSteelBlue" HorizontalAlign="Right" /><HeaderStyle BackColor="#464646" Font-Size="14px" ForeColor="White" Height="30px" /><AlternatingRowStyle BackColor="#EAEAEA" /><Columns><asp:TemplateField HeaderText="序号"><ItemTemplate><%# Container.DataItemIndex + 1%></ItemTemplate><ControlStyle Width="35px"></ControlStyle><ItemStyle Width="35px" /></asp:TemplateField><asp:BoundField DataField="库单商品序号" HeaderText="库单商品序号" ReadOnly="true"></asp:BoundField><asp:BoundField DataField="产品名称" HeaderText="产品名称" ReadOnly="true"></asp:BoundField><asp:BoundField DataField="入库数量" HeaderText="数量"></asp:BoundField><asp:BoundField DataField="单价" HeaderText="单价" ></asp:BoundField>                                                <asp:TemplateField HeaderText="金额"><ItemTemplate>                                <%# !String.IsNullOrEmpty(DataBinder.Eval(Container.DataItem, "单价").ToString()) ? DataBinder.Eval(Container.DataItem, "单价").ToString():"未填写单价"%></ItemTemplate>                                     </asp:TemplateField><asp:TemplateField HeaderText="金额"><ItemTemplate>                                <%#(!String.IsNullOrEmpty(Eval("入库数量").ToString())&&!String.IsNullOrEmpty(Eval("单价").ToString()))?(decimal.Parse(Eval("入库数量").ToString()) * decimal.Parse(Eval("单价").ToString())).ToString():"0"%></ItemTemplate>                                    </asp:TemplateField><asp:BoundField DataField="供应商" HeaderText="供应商" ReadOnly="true"></asp:BoundField><asp:BoundField DataField="经办人" HeaderText="采购人" ReadOnly="true"></asp:BoundField><asp:TemplateField HeaderText="状态"><ItemTemplate>                                       <%# DataBinder.Eval(Container.DataItem, "状态").ToString() == "0" ? "已提交未审核" : (DataBinder.Eval(Container.DataItem, "状态").ToString() == "1" ? "已审核" : "未通过")%>                                                      </ItemTemplate>                                       </asp:TemplateField><asp:BoundField DataField="创建时间" HeaderText="创建时间" ReadOnly="true"></asp:BoundField><asp:CommandField HeaderText="编辑" ShowEditButton="true"  /> <asp:CommandField HeaderText="删除" ShowDeleteButton="True" /> </Columns><RowStyle HorizontalAlign="Center" Height="35px" /><EmptyDataTemplate><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td align="center" style="border-right: black 1px; border-top: black 1px; border-left: black 1px;border-bottom: black 1px; background-color: whitesmoke;">该列表中暂时无数据!</td></tr></table></EmptyDataTemplate></asp:GridView>

后台代码:

ps:编辑、删除按钮不要通过属性去显示,手动设置显示

<asp:CommandField HeaderText="编辑" ShowEditButton="true"  /> 
                        <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />

 //绑定数据源public void DataBindToGridview(){MiroSoft.BLL.库单商品表 oper = new MiroSoft.BLL.库单商品表();if (Request.QueryString["id"] != null){List<MiroSoft.Model.库单商品表> list = new List<MiroSoft.Model.库单商品表>();
//这里将数据库查询出的数据转换成了list集合  便于下边循环判断条件去显示隐藏按钮
//dataset转换为list集合可查看最后一个方法list = oper.getlistkudanshangpinbiao("单具序号=" + Request.QueryString["id"].ToString());GVData.DataSource = list;GVData.DataBind();if (Request.QueryString["status"] != ""){for (int i = 0; i < this.GVData.Rows.Count; i++){//获取删除按钮   意思是获取gridview第12列第一个控件LinkButton delete = GVData.Rows[i].Cells[12].Controls[0] as LinkButton;LinkButton edit = GVData.Rows[i].Cells[11].Controls[0] as LinkButton;//获取按钮   意思是获取第i行的 lblStatus 控件     前台代码<asp:Label ID="lblStatus" runat="server" Enabled="false"></asp:Label>  //Label lblStatus = this.grvApplyInfo.Rows[i].FindControl("lblStatus") as Label;//根据条件去选择隐藏还是显示if (list[i].状态.Value.ToString().Trim() == "1"|| list[i].状态.Value.ToString().Trim() == "2"){edit.Visible=false;delete.Visible = false;}else{edit.Visible = true;delete.Visible = true;}}}}else{List<MiroSoft.Model.库单商品表> list = new List<MiroSoft.Model.库单商品表>();list = oper.getlistkudanshangpinbiao("");GVData.DataSource = list;GVData.DataBind();
//传过来的状态如果不为空if (Request.QueryString["status"] == "0"){for (int i = 0; i < this.GVData.Rows.Count; i++){//获取删除按钮   意思是获取gridview第12列第一个控件LinkButton lb = GVData.Rows[i].Cells[12].Controls[0] as LinkButton;//获取按钮   意思是获取第i行的 lblStatus 控件     前台代码<asp:Label ID="lblStatus" runat="server" Enabled="false"></asp:Label>  //Label lblStatus = this.grvApplyInfo.Rows[i].FindControl("lblStatus") as Label;//根据条件去选择隐藏还是显示if (list[i].状态.Value.ToString().Trim() == "1" || list[i].状态.Value.ToString().Trim() == "2"){lb.Visible = false;}else{lb.Visible = true;}}}}LabPageSum.Text = Convert.ToString(GVData.PageCount);LabCurrentPage.Text = Convert.ToString(((int)GVData.PageIndex + 1));this.GoPage.Text = LabCurrentPage.Text.ToString();}
//编辑protected void GVData_RowEditing(object sender, GridViewEditEventArgs e){GVData.EditIndex = e.NewEditIndex;DataBindToGridview();}//更新protected void GVData_RowUpdating(object sender, GridViewUpdateEventArgs e){//string id = ((TextBox)GVData.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString().Trim();string id = GVData.Rows[e.RowIndex].Cells[1].Text.ToString();string num = ((TextBox)(GVData.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();//数量 //强制转换  获取第四列第一个控件的值string money = ((TextBox)GVData.Rows[e.RowIndex].Cells[4].Controls[0]).Text.Trim();//单价  //强制转换MiroSoft.BLL.库单商品表 oper = new MiroSoft.BLL.库单商品表();int aa = oper.getresoult(id, num, money);if (aa > 0){this.GVData.EditIndex = -1;DataBindToGridview();}else{ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('修改失败!');</script>");}}//取消protected void GVData_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e){GVData.EditIndex = -1;DataBindToGridview();}//删除protected void GVData_RowDeleting(object sender, GridViewDeleteEventArgs e){string id = GVData.Rows[e.RowIndex].Cells[1].Text.ToString();//获取库单商品序号MiroSoft.BLL.库单商品表 oper = new MiroSoft.BLL.库单商品表();int aa = oper.getdelresoult(id);if (aa > 0){GVData.DataBind();DataBindToGridview();}else{ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('删除失败!');</script>");}}//删除时弹出提示框protected void GVData_RowDataBound(object sender, GridViewRowEventArgs e){//ZWL.Common.PublicMethod.GridViewRowDataBound(e);if (e.Row.RowType == DataControlRowType.DataRow){if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate){((LinkButton)e.Row.Cells[12].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[2].Text + "\"吗?')");}}}//将DataSet转化为list集合
public List<Model.库单商品表> getlistkudanshangpinbiao(string strWhere){StringBuilder strSql = new StringBuilder();strSql.Append("select 库单商品序号,单具序号,产品序号,产品名称,仓库序号,入库数量,单价,供应商,经办人,创建时间,更新时间,状态,备注 ");strSql.Append(" FROM 库单商品表 ");if (strWhere.Trim() != ""){strSql.Append(" where " + strWhere);}DataTable dt= DbHelperSQL.Query(strSql.ToString()).Tables[0];//转换成集合要有一个model类包含数据库所有的字段List<Model.库单商品表> list = new List<Model.库单商品表>();for (int i = 0; i < dt.Rows.Count; i++){Model.库单商品表 app = new Model.库单商品表{库单商品序号 = Convert.ToInt32(dt.Rows[i]["库单商品序号"]),单具序号 = Convert.ToInt32(dt.Rows[i]["单具序号"]),产品序号 = Convert.ToInt32(dt.Rows[i]["产品序号"]),产品名称 = dt.Rows[i]["产品名称"].ToString(),仓库序号 = Convert.ToInt32(dt.Rows[i]["仓库序号"]),入库数量 = Convert.ToDecimal(dt.Rows[i]["入库数量"]),单价 = Convert.ToDecimal(dt.Rows[i]["单价"]),供应商 = dt.Rows[i]["供应商"].ToString(),经办人 = dt.Rows[i]["经办人"].ToString(),创建时间 = Convert.ToDateTime(dt.Rows[i]["创建时间"]),更新时间 = Convert.ToDateTime(dt.Rows[i]["更新时间"]),状态 = Convert.ToInt32(dt.Rows[i]["状态"]),};//返回的数据如果不确定是否有值  加以判断if (!DBNull.Value.Equals(dt.Rows[i]["备注"])){app.备注 = dt.Rows[i]["备注"].ToString();}list.Add(app);//添加到集合}return list;}

.net GridView绑定数据、编辑、更新、删除(弹出确认对话框)、取消、根据条件隐藏或显示按钮操作相关推荐

  1. 实现对gridview删除行时弹出确认对话框的四种方法

    实现对gridview删除行时弹出确认对话框的四种方法 在.net2.0中,实现对gridview删除行时弹出确认对话框的四种方法 1,GridView中如何使用CommandField删除时,弹出确 ...

  2. php实现删除功能,点击删除弹出确认框,点确定删除数据,点取消返回。删除了商品后总价要相应的改变

    1.连接数据库: 2. 订单信息从product数据库下computers表调出不分页显示在网页product.php; 实现删除功能,点击删除弹出确认框,点确定删除数据,点取消返回.删除了商品后总价 ...

  3. 表单form提交前弹出确认对话框

    表单form提交前进弹出确认对话框 定义一个函数: <script language="javascript"> function sumbit_sure(){ var ...

  4. 可以弹出确认对话框的自定义Web服务器控件ConfirmButton

    作者:活靶子[原创]       出处:AspxBoy.Com 经常在论坛里看到类似这样的问题:"-如何在点击删除按钮的时候弹出个确认删除对话框". 下面我们来自己写一个这样的自定 ...

  5. GridView实现删除时弹出确认对话框

    在RowDataBound事件中添加如下代码:  1 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs ...

  6. html弹窗确认取消公告代码,js 弹出确认与取消对话框的四种方法

    1,js弹出删除确认框 复制代码 代码示例: 弹出窗口 2,js删除前确认 复制代码 代码示例: function delete_confirm(e) { if (event.srcelement.o ...

  7. a链接中 JS弹出确认对话框方法

    一种: <a href="javascript:if(confirm('确实要删除该内容吗?'))location='http://www.google.com'">弹 ...

  8. bat弹出确认或取消窗口

    需要在bat脚本里面弹出取消/确认框提示,可以用下面的案例: 示例: @echo off setlocal enabledelayedexpansionset Vbscript=Msgbox(&quo ...

  9. 如何在离开页面时弹出确认对话框

    离开页面确认主要是利用了onbeforeunload事件,当该事件声明为: Code <body onbeforeunload="return pageBeforeunload(eve ...

  10. JS弹出确认、取消对话框

    if(window.confirm('你确定要执行删除操作吗?')){alert("您点击了确定");}else{alert("您点击了取消");return ...

最新文章

  1. 世界首富太空争霸:从地上斗到天上,马斯克VS贝索斯,谁能赢
  2. 国内 Java 开发者必备的两个装备,你配置上了么?
  3. DLL load failed while importing _pywrap_tensorflow_internal
  4. cookie被淘汰_session正在被淘汰吗?
  5. Winform中将Bitmap对象通过pictureBox显示图片
  6. oracle查看数据库剩余空间,Oracle 查看数据库空间使用情况
  7. seajs常用API整理
  8. 字节跳动李航提出AMBERT!超越BERT!多粒度token预训练语言模型
  9. 大数据 数据库 评测_中国信通院公布第九批大数据产品能力评测结果,65款产品通过...
  10. java 获取vm配置参数_如何获取Java中的Java VM规范?
  11. C语言自学路之验证码
  12. SD卡与TF卡基础知识
  13. 华为云服务器建站教程
  14. 计算机更改为英语,win7如何修改语言 电脑语言改成英文的方法
  15. 【物理应用】Matlab实现两端固支梁热力耦合的有限元分析
  16. 如何批量新建文件夹,批量新建文件夹并命名
  17. 物联网导论-自动识别技术
  18. python科学计算—— numpy
  19. 华为机试python3题解(17题 持续更新ing)
  20. 蓝湖页面生成html,蓝湖的设计稿变html 好几点

热门文章

  1. 快速求2的n次幂(防Time Limit Exceeded)
  2. 全国日照时数空间分布数据/月度降雨量分布/月均气温分布
  3. 面向对象19:内部类
  4. hadoop hdfs文件给其他账号授权
  5. 信息熵 条件熵 信息增益 信息增益比 GINI系数
  6. NYOJ题目1057-寻找最大数(三)
  7. 题目458-小光棍数
  8. linux 子进程exit6,linux 惊群有关问题
  9. android反射改theme,全局修改默认字体,通过反射也能做到
  10. 拜托,别再让我优化大事务了,我的头都要裂开了