//将按钮列转换为模板列<asp:TemplateField HeaderText="删除" ShowHeader="False"> <ItemTemplate> <asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("ST_RCategoryId") %>' runat="server" CausesValidation="false" CommandName="Delete" Text="删除" OnClick="LinkButton1_Click"></asp:LinkButton> </ItemTemplate> </asp:TemplateField>//后台//删除前弹出确认对话框 protected void dg_RCategoryList_RowDataBound(object sender, GridViewRowEventArgs e) ...{ if (e.Row.RowType == DataControlRowType.DataRow) ...{ LinkButton lb = (LinkButton)e.Row.FindControl("LinkButton1"); if(lb.Text.Equals("删除")) ...{ lb.Attributes.Add("onclick","javascript:return confirm('"+string.Format( "是否要删除记录{0}?",DataBinder.Eval(e.Row.DataItem,"ST_RCategoryId"))+"')"); } } }//删除 protected void dg_RCategoryList_RowCommand(object sender, GridViewCommandEventArgs e) ...{ if (e.CommandName == "Delete") ...{ int id = Convert.ToInt32(e.CommandArgument); string ST_sqldb = ConfigurationSettings.AppSettings["strcon"]; //连接ST_GinShopManage数据库 SqlConnection ST_Conn = new SqlConnection(ST_sqldb); ST_Conn.Open(); //定义sql语句 String delsql = "delete from ST_RoomCategory where ST_RCategoryId = @RCategoryId"; //创建ST_mycommand对象,调用delsql SqlCommand ST_mycommand = new SqlCommand(delsql, ST_Conn); ST_mycommand.Parameters.Add("@RCategoryId", SqlDbType.VarChar); //从dg_RCategoryList中获取RCategoryId值 ST_mycommand.Parameters["@RCategoryId"].Value = id; ST_mycommand.ExecuteNonQuery(); dg_RCategoryList.EditIndex = -1; //dg_RCategoryList.EditItemIndex = -1; //更新dg_RCategoryList Show_RCatgegoryList(); } }