我有一个gridview,每行都有一个编辑和删除按钮。 我设法让我的编辑按钮工作,但没有设置删除按钮。 这是我的aspx:
VALUES
(1, 1, convert(datetime,'2010-01-01'),0,10.0),
(2, 1, convert(datetime,'2010-01-02'),0,11.0),
(3, 1, convert(datetime,'2010-01-03'),10,12.0),
(4, 2, convert(datetime,'2010-01-04'),0,12.0),
(5, 2, convert(datetime,'2010-01-05'),10,11.0),
(6, 2, convert(datetime,'2010-01-06'),10,13.0),
(7, 3, convert(datetime,'2010-01-07'),10,14.0),
(8, 3, convert(datetime,'2010-01-07'),10,16.0),
(9, 3, convert(datetime,'2010-01-09'),10,13.0)
这是我背后的代码:
<asp:GridView ID="gvfaq" runat="server" AutoGenerateColumns="false" DataKeyNames="faq_id" OnRowDataBound="gvfaq_RowDataBound" OnRowCommand="gvfaq_RowCommand" OnRowDeleting="gvfaq_RowDeleting" >
<Columns>
<asp:BoundField ItemStyle-Width="100px" DataField="text" HeaderText="Text" />
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button runat="server" ID="editbutton" CommandArgument='<%# Eval("faq_id") %>' CommandName="Edit" Text="Edit" />
<asp:Button runat="server" ID="delbutton" CommandArgument='<%# Eval("faq_id") %>' CommandName="Delete" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
奇怪的是,我有另一个页面,从我复制/重新发布此代码的地方,并且工作正常。
感谢。
更新 如果我检查在其他页面按钮上生成的html,则会添加此代码,但在此处不会成功:
protected void gvfaq_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int ID = Convert.ToInt32(e.CommandArgument);
DeleteFAQ(ID);
}
}
protected void gvfaq_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button l1 = (Button)e.Row.FindControl("editbutton");
l1.Attributes.Add("onclick", "Javascript:suport(" + l1.CommandArgument.ToString() + ");return false;");
l1.Attributes.Add("onClientClick", "Javascript:suport(" + l1.CommandArgument.ToString() + ");return false;");
Button l2 = (Button)e.Row.FindControl("delbutton");
l2.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete this record?')");
l2.Attributes.Add("onClientClick", "javascript:return confirm('Are you sure you want to delete this record?')");
}
}
protected void gvfaq_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridView gv = (GridView)this.FindControl("gvfaq");
int ID = (int)gv.DataKeys[e.RowIndex].Value;
DeleteFAQ(ID);
}
protected void DeleteFAQ(int ID)
{
using (SqlCommand myCommand = new SqlCommand())
using (SqlConnection myServerCreate = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ServerCreate"].ConnectionString))
{
myServerCreate.Open();
myCommand.Connection = myServerCreate;
myCommand.CommandText = "delete from faq where faq_id = " + ID;
myCommand.ExecuteNonQuery();
}
ScriptManager.RegisterStartupScript(Page, typeof(Page), "Close", "__doPostBack('','Delete');", true);
}
答案 0 :(得分:0)
在itemtemplate删除按钮中将命令名更改为“删除”,因为它当前不同
<asp:Button runat="server" ID="delbutton" CommandArgument='<%# Eval("faq_id") %>' CommandName="Delete" Text="Delete" />
然后检查命令名
protected void gvfaq_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int ID = Convert.ToInt32(e.CommandArgument);
DeleteFAQ(ID);
}
}
此外,如果您通过行命令事件处理删除功能,则无需在gvfaq_RowDeleting
事件中明确地添加该功能。您应该将其保留为空白。