DataGrid Delete Problem

  • Thread starter Thread starter thebison
  • Start date Start date
T

thebison

Hi all,

I hope someone can help with this relatively simple problem.
I am building a timesheet application using ASP.NET C# with Visual
Studio 2003.As it is only a protoype application, my database has been
made in MSDE.

I have a DataGrid, and have inserted a delete link button into it
through Property Builder. My Delete method works fine, however I would
like to insert a JScript popup to confirm, as otherwise users can just
delete from the database. With my normal buttons, such as a 'submit'
button on a form, I can just use the following code:

btnSubmit.Attributes.Add("onclick","javascript:if(confirm('Create new
Department - are you sure?')== false) return false;");

However with the DataGrid, the Delete link is created through the
DataGrid, and I do not know how to assign JavaScript to it. I'm sure
someone else must've had this problem before, as no-one wants users to
be able to just delete records so freely! :-)

I have already tried following this guide, but can't get it to work..

http://www.dotnetjunkies.com/HowTo/1E7FEE4A-795C-4D33-A135-843EB07C94A8.dcik

Any suggestions on how anyone has achieved this would be much
appreciated!

Al
 
I have tried this example and it works. You need to make sure you have done
the following.

1) Add a template column with a linkbutton who's ID is DeleteLink and set
the command name to Delete.

2.) Use the ItemCommand event for the datagrid and check the name is Delete
and if so then
manually delete the item.

if your using VB in the DataGrid1_ItemDataBound then

Dim lb As LinkButton

If Not (e.Item.FindControl("DeleteLink")) Is Nothing Then

lb = e.Item.FindControl("DeleteLink")
lb.Attributes.Add("onClick", "return confirm('Are you sure you
wish to delete this item?');")


End If

3.) If you are using C# make sure you have setup the event handlers to fire,
and you should check this anyway with debug to make sure.
 
Hi,
The way to write events for the buttons contained in the Datagrid is
different from normal buttons.
Let me explain....
When you place the button on the form, the form becomes the 'container' of
the button. So you can write various events of the button that you have
placed in the 'form'.
But when you place the button in the grid, the grid becomes the 'container'
of the button whose container in turn is 'form'. So you can write various
events of the grid but not of the button.

In order to write events(or adding attributes) for the button contained in a
grid, you first need to find that button control in every row(i.e. item) of
the grid. You can do this using OnItemCreated() event of the grid. Whenever
the new item(i.e. row) is created, it searches for the button control in that
row and adds the "onclick()" attribute on it.

private void OnItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
Button btn = (Button)e.Item.FindControl("delbutton");
btn.Attributes.Add("onclick", "return confirm('Are you sure you want to
delete this?')");
}
}

Hope this will solve you problem...

Regards,
Amit Bajaj.
 
Didnt you read my post or the article he referred to, you could have saved
yourself the bother of answering this post.
 
Back
Top