后台代码如下:
public partial class admin_Default2 : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        databind();    }
protected void databind()
{
SqlConnection sqlcon = new SqlConnection("Server=COMMAND-B1TQIST; Database=users;Integrated Security=True");
        SqlCommand sqlcom = new SqlCommand("select * from 部门信息表",sqlcon); 
        sqlcon.Open();
        SqlDataAdapter da = new SqlDataAdapter(sqlcom);
        DataSet ds = new DataSet();
        da.Fill(ds, "部门信息表");
        GridView1.DataSource = ds.Tables["部门信息表"].DefaultView;
        GridView1.DataBind();
        sqlcon.Close();
}protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
   
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
    
  LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
    l.Attributes.Add("onclick", "javascript:return confirm('你确认要删除:\"" + e.Row.Cells[0].Text + "\"吗?')");
    //l.Attributes.Add("onclick", "javascript:return confirm('你确认要删除"吗?')");   
 }
    
}    
protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
            string ID = GridView1.Rows[e.RowIndex].Cells[1].Text;
            string sql = "delete from 部门信息表 where 部门编号='" + ID + "'";
            SqlConnection sqlcon = new SqlConnection("Server=COMMAND-B1TQIST; Database=users;Integrated Security=True");
            SqlCommand sqlcom = new SqlCommand(sql, sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();
            databind();
}
catch (Exception ex)
        {
            Response.Write(ex.Message);
        }}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
    }
}
前台主要代码如下:
<asp:GridView DataKeyNames="部门编号" ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand"  OnRowDataBound="GridView1_RowDataBound" OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting" > 
<Columns>
<asp:BoundField DataField="部门编号" HeaderText="部门编号" />
<asp:BoundField DataField="部门名称" HeaderText="部门名称" />
<asp:BoundField DataField="部门负责人" HeaderText="部门负责人" />
<asp:TemplateField HeaderText="删除">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CommandArgument='<%# Eval("部门编号") %>' CommandName="Delete" runat="server">删除</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:usersConnectionString %>"
     DeleteCommand="DELETE FROM [部门信息表] WHERE [部门编号] = @部门编号" InsertCommand="INSERT INTO [部门信息表] ([部门编号], [部门名称], [部门负责人]) VALUES (@部门编号, @部门名称, @部门负责人)"
     SelectCommand="SELECT * FROM [部门信息表]" UpdateCommand="UPDATE [部门信息表] SET [部门名称] = @部门名称, [部门负责人] = @部门负责人 WHERE [部门编号] = @部门编号">            
</asp:SqlDataSource>

解决方案 »

  1.   

    string ID = GridView1.DataKeys[e.RowIndex].Value;string sql = "delete from 部门信息表 where 部门编号=@ID"
    SqlCommand.Parameters.AddWithValue("ID", ID);
    ...
    最关键可能是把GridView的enableViewState设为false试试。
      

  2.   

    可以参考下我的代码:
    protected void btnDel_Click(object sender, EventArgs e)
            {    
                int j=0;
                dsDptManageAddTableAdapters.perTbDepartTableAdapter perDPT = new Browser.Business.WebPer.dsDptManageAddTableAdapters.perTbDepartTableAdapter();
                for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
                {
                    CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("chkDel");
                   
                    if (cbox.Checked == true)
                    {
                        j++;
                        string strGuid = this.GridView1.Rows[i].Cells[5].Text.ToString();
                        try
                        {
                            perDPT.DeleteQuery(strGuid);
                        }
                        catch
                        {
                            AjaxJSUnit.ExecuteBlock(this, "alert('该部门信息暂时不能删除!')");
                        }
                    }            }
                if (j == 0)
                {
                    AjaxJSUnit.ExecuteBlock(this, "alert('没选中任何行,请选择!')");
                }
                else
                {
                    GetDptData();
                }            
            }
      

  3.   

         string ID = GridView1.Rows[e.RowIndex].Cells[1].Text;  
    cells[1]是部门名称.