删除给出gridview中的特定行 [英] deleting particular row in giving gridview
问题描述
需要帮助。我正在删除我的网格中的特定行
我的edititem模板中有一个删除按钮
need help . i am deleting particular row in my grid
there is a delete button in my edititem template
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:Button ID="btnedit" runat="server" Text="Edit" />
<asp:Button ID="btnDelete" runat="server" Text="Detete" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnupdate" CommandName="update" runat="server" Text="Edit" />
<asp:Button ID="btncancel" CommandName="cancel" runat="server" Text="Detete" />
</EditItemTemplate>
;
和我的c#代码是
protected void GridView1_RowDeleting(object sender,GridViewDeleteEventArgs e)
{
SqlConnection con = new SqlConnection(Data Source = .; Initial Catalog = master; Integrated安全=真);
SqlCommand cmd = new SqlCommand();
con.Open();
int idd = Convert.ToInt32(GridView1.DataKeys [e.RowIndex] .Values [id]。ToString());
Button delete =(Button)GridView1.Rows [e。 RowIndex] .FindControl(btncancel);
if(delete.CommandName ==caancel)
// int idd = Int32.Parse(GridView1.DataKeys [e.RowIndex] .Values [id]。ToString());
cmd =新的SqlCommand(从食物中删除id =+ idd,con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.DataBind();
}
我收到此错误
异常详细信息:System.Web.HttpException:Multiple找到具有相同IDlname的控件。 FindControl要求控件具有唯一的ID。
先生我想知道这个错误即将发生的原因以及我的代码中缺少的内容。这是一次学习经历,所以我请求你先生请帮助我,我是一名新生
and my c# codes are
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
SqlCommand cmd=new SqlCommand();
con.Open();
int idd = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["id"].ToString());
Button delete = (Button)GridView1.Rows[e.RowIndex].FindControl("btncancel");
if(delete.CommandName=="caancel")
// int idd = Int32.Parse(GridView1.DataKeys[e.RowIndex].Values["id"].ToString());
cmd = new SqlCommand("delete from food where id="+idd, con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.DataBind();
}
and i am getting this error
Exception Details: System.Web.HttpException: Multiple controls with the same ID 'lname' were found. FindControl requires that controls have unique IDs.
sir i want to know the reason why this error is coming and what i am missing in my codes. this is a learning experience so i request to you sir please help me i am a new student
推荐答案
你的错误与你提出的问题无关。您只需要了解HTML要求id
属性应始终在页面上具有唯一值。您现在不仅会遇到此异常,而且在一般情况下,如果您违反了浏览器行为,则浏览器行为是不可预测的。它只是无效的HTML。
用于HTMLid
和ASP.NET <$ c $之间的关系c> Control.ID 或Control.ClientID
,请了解以下主题:
http:// msdn。 microsoft.com/en-us/library/system.web.ui.control.id%28v=vs.110%29.aspx [ ^ ],
http:// msdn。 microsoft.com/en-us/library/system.web.ui.control.clientid%28v=vs.110%29.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.web.ui.clientidmode(v = vs.110).aspx [ ^ ],
http://msdn.microsoft .com / en-us / library / system.web.ui.inamingcontainer%28v = vs.110%29.aspx [ ^ ],
http://msdn.microsoft.com/en -us / library / system.web.ui.control.namingcontainer%28v = vs.110%29.aspx [ ^ ]。
-SA
Your error is unrelated to your question as formulated. You just need to understand that HTML requires thatid
attributes should always have values unique on the page. Not only you get this exception now, but, in general case, the browser behavior is unpredictable if you violate it. It would be just not valid HTML.
For relationships between HTMLid
and ASP.NETControl.ID
orControl.ClientID
, please learn the following topics:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.id%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.ui.clientidmode(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.ui.inamingcontainer%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.web.ui.control.namingcontainer%28v=vs.110%29.aspx[^].
—SA
这篇关于删除给出gridview中的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!