在C#在GridView中添加LinkButton为模板列, 实现删除功能,并在删除前提示用户是否进行删除
例子是IQCFeedstockRecordModify.cs
前端代码
<cimesui:cimesgridview width="100%" id="gvShow" runat="server" allowpaging="true" autogeneratecolumns="false" pagesize="15" OnRowDataBound ="gvShow_RowDataBound" onrowcommand="gvShow_RowCommand" DataKeyNames="delFlag" OnRowUpdating="gvShow_RowUpdating" AutoGenerateEditButton="True" OnRowEditing="gvShow_EditCommand" AutoGenerateUpdatingButton="True" >
<Columns> <asp:BoundField DataField="InspectionOrderNumber" HeaderText="送检单号" ItemStyle-Wrap="false" /> <asp:BoundField DataField="Device" HeaderText="品号" ItemStyle-Wrap="false" /> <asp:BoundField DataField="MAKTX" HeaderText="品名规格" ItemStyle-Wrap="false" /> <asp:BoundField DataField="Unit" HeaderText="单位" ItemStyle-Wrap="false" /> <asp:BoundField DataField="CheckTheQuantity" HeaderText="数量" ItemStyle-Wrap="false" /> <asp:BoundField DataField="PONumber" HeaderText="采购单号" ItemStyle-Wrap="false" /> <asp:BoundField DataField="buyer" HeaderText="采购员" ItemStyle-Wrap="false" /> <asp:BoundField DataField="POItem" HeaderText="核对" ItemStyle-Wrap="false" /> <asp:TemplateField> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" Text='删除' OnClick="LinkButton1_Click" ></asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> </cimesui:cimesgridview>
删除前提示用户是否删除
protected void gvShow_RowDataBound(object sender, GridViewRowEventArgs e) { e.Row.Attributes.Add("style", "background:#FFF"); if (e.Row.RowType == DataControlRowType.DataRow) { LinkButton linkbtnDel = (LinkButton)e.Row.FindControl("LinkButton1"); linkbtnDel.Attributes.Add("onclick", "return confirm(\"你确认要删除:["+e.Row.Cells[1].Text +"]吗\")"); } }
删除语句
protected void LinkButton1_Click(Object sender, EventArgs e) { LinkButton LinkButton = sender as LinkButton; GridViewRow row = LinkButton.NamingContainer as GridViewRow; int a = row.RowIndex; string id = dtUpdateData.Rows[a]["ID"].ToString(); using (CimesTransactionScope cts = CimesTransactionScope.Create()) { string strSql = "UPDATE AOI_Feedstock_Add_Record SET delFlag = 1 WHERE ID =#[STRING] "; DBCenter.ExecuteParse(strSql, id); cts.Complete(); } BindData(); }