【问题标题】:OnRowCommand in the Gridview gives errorGridview 中的 OnRowCommand 给出错误
【发布时间】:2012-03-04 09:07:22
【问题描述】:

我试图在网格视图中使用GridViewCommandEventArgs,但是当我运行它时,它一直说:除非指定了 UpdateCommand,否则数据源“SqlDataSource1”不支持更新。

我在网格中有多个Linkbuttons,因此OnRowCommand

OnRowCommand ="Grid_Row"

<asp:TemplateField>
                 <ItemTemplate>
                <asp:LinkButton ID="Update" CommandArgument='<%#Eval("Serno") %>' runat="server"    OnClientClick="return confirm('Are you sure you want to Update this?');" CommandName ="Update" >Update</asp:LinkButton>
            </ItemTemplate>
           </asp:TemplateField> 
             <asp:TemplateField>
                 <ItemTemplate>
             <asp:LinkButton ID="Remove" CommandArgument='<%#Eval("Serno") %>' runat="server"    OnClientClick="return confirm('Are you sure you want to Remove this Item?');" CommandName ="Remove" >Remove</asp:LinkButton>
                </ItemTemplate>
             </asp:TemplateField> 




   protected void Grid_Row(Object sender, GridViewCommandEventArgs e)
{



    LinkButton Remove = (LinkButton)GridView1.FindControl("Remove");
    LinkButton Update = (LinkButton)GridView1.FindControl("Update");
    if (e.CommandName == "Remove")
    {
        try
        {

            int index = Convert.ToInt32(e.CommandArgument);
            SqlConnection con = new SqlConnection(Constring);
            con.Open();
            SqlCommand cmd = new SqlCommand(" update [Order_Items] set status=0 WHERE [Serno] =" + index.ToString() + "", con);
            if (cmd.ExecuteNonQuery() > 0)
            {


                GridView1.DataBind();
                msg_lbl.Text = "Record Deleted";

            }
            else
            {

            }
            con.Close();
        }
        catch
        {


        }
    }
    else if (e.CommandName == "Update")
    {
        try
        {


            int index = Convert.ToInt32(e.CommandArgument);

           [Convert.ToDouble(e.CommandArgument)].FindControl("Quantity");

            GridViewRow clickedRow = ((LinkButton)e.CommandSource).NamingContainer as GridViewRow;
            string Q = ((TextBox)clickedRow.FindControl("Quantity_Txt") as TextBox).Text;
            string P = ((Label )clickedRow.FindControl("Amount_Lbl") as Label ).Text;


            double Quantity = Convert.ToDouble(Q);

            double Price = Convert.ToDouble(P);
            double Calc = Quantity * Price;


            SqlConnection con = new SqlConnection(Constring);
            con.Open();
            SqlCommand cmd = new SqlCommand("update [Order_Items] set Quantity='"+Quantity +"', Money='" + Calc + "' WHERE [Serno] =" + index.ToString() + "", con);
            if (cmd.ExecuteNonQuery() > 0)
            {


                GridView1.DataBind();
                msg_lbl.Text = "Record Updated";



            }
            else
            {

            }
            con.Close();
            }

        catch
        {


        }

    }



}

【问题讨论】:

  • 你的sql查询是什么,SelectCommand,能贴出你用的代码吗???

标签: c# asp.net gridview


【解决方案1】:

那是因为您为命令名称指定了关键字。将它们更改为其他字词,例如:UPDdelete

【讨论】:

    【解决方案2】:

    您似乎没有在数据源中指定 UpdateCommand。

    与指定选择命令和选择参数一样,必须指定更新命令。

    希望对你有帮助

    【讨论】:

    • 您也可以尝试将 commandName 更改为不同于“更新”或“删除”的名称。这些可能是一些关键字。尝试将其设置为例如'Update1' 看看它是否有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多