我正在尝试在网格视图中使用GridViewCommandEventArgs
,但是当我运行它时仍然说:数据源“SqlDataSource1”不支持更新,除非指定了UpdateCommand。
我在网格中有多个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
{
}
}
}
答案 0 :(得分:2)
这是因为您为命令名指定了关键字。将它们更改为其他字词,例如:UPD
或delete
。
答案 1 :(得分:1)
您似乎未在数据源中指定UpdateCommand。
与指定select命令和选择参数的方式相同,您必须指定更新命令。
希望这有帮助