Delete Button

Topics: Developer Forum, User Forum
Mar 14, 2007 at 5:02 PM
First of all, I think this Bulk Edit Gridview is awesome. Thanks Matt.

It allows for my users to quickly edit the data presented to them, but in certain situations, they would like to delete certain records because I build a file out of the information presented in the grid.

Is there a way to add a Delete button to each row. I can add an "update/cancel" button, but when I add a Delete button, it never shows in the grid.

Any thoughts?
Developer
Mar 14, 2007 at 5:32 PM
I ran in to this as well, just add a button field and define the delete command in your SqlDataSource

<asp:ButtonField HeaderText="Delete" Text="Delete" ButtonType="Button"
ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle"
CommandName="Delete" />

If you need to something more complicated than can be defined in the DeleteCommand of your SqlDataSource, then you can do CommandName="mydelete" and then use the OnRowCommand function for the GridView:

protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "mydelete")
{
// Do work here

GridView1.DataBind();
}
}
Feb 20, 2008 at 5:32 PM
This is a slight variation to the javascript confirmation by Børge. Since using ButtomTyp="Button" vs. "LinkButton" results in a different type, I've got some dumb casting in there. I'm sure there's a better way :)


protected override void OnRowDataBound(GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
for (int j = 0; j < e.Row.Cellsi.Controls.Count; j++)
{
if (e.Row.Cellsi.Controlsj is LinkButton)
{
LinkButton l = (LinkButton)e.Row.Cellsi.Controlsj;
if (l.CommandName.ToLower() == "delete")
{
l.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete this record?') ");
}
}
else if (e.Row.Cellsi.Controlsj is Button)
{
Button l = (Button)e.Row.Cellsi.Controlsj;
if (l.CommandName.ToLower() == "delete")
{
l.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete this record?') ");
}
}
}
}
}
base.OnRowDataBound(e);
}
Feb 21, 2008 at 8:34 PM
doh. I'd like to retract that code I posted above, or if somebody has a simple javascript fix/suggested, I'd love to hear it :)

Basically, when you use a linkbutton like the following, it works fine:
<asp:ButtonField HeaderText="Delete" Text="Delete" ButtonType="Link" ItemStyle-HorizontalAlign="Center"
ItemStyle-VerticalAlign="Middle" CommandName="Delete" />

But if you change the ButtonType to "Button", then the onClick event will contain the javascript you see above, followed by the typical javascript:__doPostBack('mystuff','Delete$1')

So the first statement does a return and never fires the postback. With a linkbutton, the HREF is the postback, the onClick is only the confirmation. I'm pretty sure instead of doing an attribute.add you could surround the existing postback javascript with an if confirm("you sure") type bit. I think that'd require you to move the base.OnRowDataBound(e); to fire before this new stuff.