C# Gridview删除行

C# Gridview删除行,c#,asp.net,gridview,C#,Asp.net,Gridview,当我点击删除按钮时,它不会调用删除功能。有人能帮我吗 protected void GridViewQuestion_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { ImageButton lb = (ImageButton)e.Row.Fi

当我点击删除按钮时,它不会调用删除功能。有人能帮我吗

protected void GridViewQuestion_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton lb = (ImageButton)e.Row.FindControl("ImageButtonDelete");

                lb.Attributes.Add("onclick", "javascript:return " +
                     "confirm('Are you sure you want to delete this record " +
                     DataBinder.Eval(e.Row.DataItem, "question") + "')");
            }
        }

        protected void GridViewQuestion_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int questionID = (int)GridViewQuestion.DataKeys[e.RowIndex].Value;

            TopicEntity.deleteQuestion(questionID);

            BindGridData();
        }

public bool deleteQuestion(int QuestionID)
        {
            SqlConnection dbConnection = new SqlConnection(connectionString);

            SqlCommand dbCommand = new SqlCommand();
            dbCommand.CommandText = "DELETE FROM Question QuestionID=@QuestionID";
            dbCommand.Connection = dbConnection;
            dbCommand.Parameters.AddWithValue("@QuestionID", QuestionID);

            bool deleteSuccess = false;

            try
            {
                dbConnection.Open();
                dbCommand.ExecuteNonQuery();
                deleteSuccess = true;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                dbConnection.Close();
            }
            return deleteSuccess;
        }

您的标记看起来像什么?您的GridView应该有类似的功能:

<asp:GridView OnRowDeleted="GridViewQuestion_RowDeleting" />
编辑:

错误在SQL语句中。应该是这样的:

dbCommand.CommandText = "DELETE FROM Question where QuestionID=@QuestionID";

您缺少WHERE关键字。

如果问题是单击ImageButton时没有触发删除事件,则您忘记了在ImgaButton的标记中添加CommandName=Delete。另外,请确保您的GridView绑定在页面_加载时在内部执行,如果!iPostBack

尝试以下方法:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Delete")
    {
        int imageID = Convert.ToInt32(e.CommandArgument);
        string constring = @"Server=User-PC\SQLEXPRESS;initial catalog=Student;Integrated Security=true";
        SqlConnection conn = new SqlConnection(constring);
        conn.Open();
        string query="Delete from [student].[dbo].[ImageTable] where ID=" +imageID;
        SqlCommand cmd = new SqlCommand(query, conn);
        cmd.ExecuteNonQuery();
        this.LoadGrid();
        conn.Close();

    }
}

我已经在gridview中写了这些。当我点击删除按钮时,会出现一个消息框。但它并没有从数据库中删除。@lixi Ok,所以您的事件处理程序实际上已被访问。你的问题听起来好像你的活动没有启动。至于从数据库中删除数据,您需要发布deleteQuestion方法的代码。无法得知那里发生了什么。@lixi请查看我的编辑。问题在您的命令文本中。确定。。我忘了把WHERE加进去。但即使我添加Where键,它也不起作用。我把我的gridview放在一个updatepanel中。这会影响吗?@lixi在GridViewQuestion\u行删除方法中设置断点。你需要弄清楚你是否真的在启动那个事件。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Delete")
    {
        int imageID = Convert.ToInt32(e.CommandArgument);
        string constring = @"Server=User-PC\SQLEXPRESS;initial catalog=Student;Integrated Security=true";
        SqlConnection conn = new SqlConnection(constring);
        conn.Open();
        string query="Delete from [student].[dbo].[ImageTable] where ID=" +imageID;
        SqlCommand cmd = new SqlCommand(query, conn);
        cmd.ExecuteNonQuery();
        this.LoadGrid();
        conn.Close();

    }
}