【问题标题】:Gridview delete rowGridview 删除行
【发布时间】:2013-05-04 01:27:26
【问题描述】:

当我点击删除按钮时,它不会调用删除功能。谁能帮帮我?

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;
        }

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    您的标记是什么样的?你的 GridView 应该有类似的东西:

    <asp:GridView OnRowDeleted="GridViewQuestion_RowDeleting" />
    

    或者,通过方法名称,您可能想要使用OnRowDeleting

    <asp:GridView OnRowDeleting="GridViewQuestion_RowDeleting" />
    

    编辑

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

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

    您缺少WHERE 关键字。

    【讨论】:

    • 我已经在我的 gridview 中写了这些。当我单击删除按钮时,会提示一个消息框。但它没有从数据库中删除。
    • @lixi 好的,您的事件处理程序实际上已经到达。您的问题听起来好像您的活动没有触发。至于从数据库中删除数据,您需要发布 deleteQuestion() 方法的代码。没有办法知道那里发生了什么。
    • @lixi 请看我的编辑。问题出在您的命令文本中。
    • 好的.. 我忘记添加 WHERE 了。但即使我添加了 Where 键,它也无法正常工作。我把我的gridview放在一个更新面板中。会影响吗?
    • @lixi 在您的GridViewQuestion_RowDeleting() 方法中放置一个断点。你需要弄清楚你是否真的触发了那个事件。
    【解决方案2】:

    如果问题是单击 ImageButton 时没有触发 Delete 事件,那么您忘记在 ImgaButton 的标记中添加 CommandName="Delete"。还请确保Page_Load 上的 GridView bindif(!IsPostBack) 内这样做

    【讨论】:

      【解决方案3】:

      试试这样:

      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();
      
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-03-02
        • 1970-01-01
        • 1970-01-01
        • 2016-01-30
        • 2016-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多