ASP.Net C#其中ID等于Cell

时间:2011-04-06 11:22:23

标签: c# asp.net

大家好,我有这样的发言:

cmd = new SqlCommand("UPDATE Comments SET Flagged = '" + "Yes" + "' WHERE Comment_ID ='" + Row.Cells[0] + "'", con);

Visual Studio强调Row.Cells [0]在当前上下文中不存在Row。

你能看一看,看看我哪里出错。

完整方法代码:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

       GridView1.RowCommand += new GridViewCommandEventHandler(GridView1_RowCommand);
        if (e.CommandName == "cmdFlag")
        {
        con.Open();

        cmd = new SqlCommand("UPDATE Comments SET Flagged = '" + "Yes" + "' WHERE Comment_ID ='" + Row.Cells[0] + "'", con);

        cmd.ExecuteNonQuery();

        Response.Redirect("~/renteronly/flagset.aspx");
        }
}

2 个答案:

答案 0 :(得分:1)

Mark,您需要将 CommandArgument 属性绑定到 Comment_ID

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
    if (e.CommandName == "cmdFlag")
      {
        con.Open();
        cmd = new SqlCommand("UPDATE Comments SET Flagged = 'Yes' WHERE Comment_ID = @Comment_ID", con);
        cmd.Parameters.AddWithValue("@Comment_ID",e.CommandArgument);
        cmd.ExecuteNonQuery();
        Response.Redirect("~/renteronly/flagset.aspx");
      }
   }

答案 1 :(得分:0)

您可以像GridView.SelectedRow一样获取所选行。