IT Community - Software Programming, Web Development and Technical Support

How to give confirmation for deleting in datagrid?

This is a discussion on How to give confirmation for deleting in datagrid? within the ASP and ASP.NET Programming forums, part of the Web Development category; Hi friends, I need to know how to give confirmation when deleting row in datagrid? Could anybody answer me?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > ASP and ASP.NET Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 03-26-2008, 05:39 AM
krishnakumar krishnakumar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 206
krishnakumar is on a distinguished road
Question How to give confirmation for deleting in datagrid?

Hi friends,

I need to know how to give confirmation when deleting row in datagrid?

Could anybody answer me?
__________________
Krishnakumar.S
Beware of Everything -that is un true; stick to the Truth shall succeed slowly but steadily
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-26-2008, 05:54 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Wink Re: How to give confirmation for deleting in datagrid?

we can add the javascript to each and every Delete link button (one per row). We will do that in ItemDataBound event handler.

See the following code...

Code:
Private Sub DataGrid1_ItemDataBound
(ByVal sender As Object, ByVal e As DataGridItemEventArgs) 
Handles DataGrid1.ItemDataBound
   Dim l As LinkButton
   If e.Item.ItemType = ListItemType.Item Or 
   e.Item.ItemType = ListItemType.AlternatingItem Then
   l = CType(e.Item.Cells(0).FindControl("cmdDel"), LinkButton)
   l.Attributes.Add("onclick", "return getconfirm();")
   End If
End Sub
Here we check whether the item (row) is of type Item or AlternatingItem. We then add DHTML OnClick event handler that calls a client side function getconfirm().

The Confirmation JavaScript function

Code:
function getconfirm() 
{ 
if (confirm("Do you want to delete record?")==true) 
return true; 
else 
return false; 
}
This function displays a javascript confirmation dialog to the user. If user clicks on Ok the function returns true and the form is posted back as usual. If user clicks on cancel it returns false indicating event cancellation. This will cancel the postback of the form.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-26-2008, 06:03 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Cool Re: How to give confirmation for deleting in datagrid?

You can add confirm message to any html tag like this,

Code:
<a href="#" onclick="return confirm('Are you sure you want to delete?')">delete</a>
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-03-2008, 06:12 AM
GDevakii GDevakii is offline
D-Web Sr.Programmer
 
Join Date: Aug 2007
Posts: 138
GDevakii is on a distinguished road
Smile Re: How to give confirmation for deleting in datagrid?

You notice that I added a TemplateColumn and I wrote myself the ItemTemplate
the item template has the following code
<a id="lnkDelete" href='<%#DataBinder.Eval(Container,"DataItem.Produ ctID")%>'
onclick="return confirm('Are you sure you want to delete')"
onserverclick="DeleteProduct"
runat="server">delete</a>
------------------------------------------------------------------
the DeleteProduct Method code is here:
protected void DeleteProduct(object s, System.EventArgs e)
{
//Cast the s object to an HtmlAnchor, I'm sure it is an HtmlAnchor
HtmlAnchor deleteLnk = (HtmlAnchor)s;

//Retrive the ProductID from the HRef attribute
int productID = int.Parse(deleteLnk.HRef);

//Perform delete procedure.
string deleteSql = "DELETE FROM Products WHERE ProductID="+productID;
SqlCommand cmdDelete = new SqlCommand(deleteSql,myConn);
myConn.Open();
cmdDelete.ExecuteNonQuery();
BindGrid();
myConn.Close();
}
-------------------------------------------------------------------------
(or)


private void BindGrid()
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Products",myConn);
dgItems.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection) ;
dgItems.DataBind();
if(dgItems.Items.Count!=0)
{
//string that hold the javascript confirm message
string confirm = "return confirm('Are you sure you want to delete?')";
foreach( DataGridItem item in dgItems.Items)
{
LinkButton deleteBtn=(LinkButton)item.Cells[2].Controls[0];
deleteBtn.Attributes.Add("onclick",confirm);
}
}
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting file contents using php Archimedees PHP Programming 1 09-20-2007 02:44 AM
deleting specific rows in a table using php Archimedees PHP Programming 6 09-14-2007 02:51 AM
can i format the hard disk without deleting some files saravanan Computer Hardware 3 09-04-2007 06:11 AM
Can any one give more info about FTP? raj Server Management 1 07-18-2007 01:42 AM
Give away ebooks! drecko Promotion Techniques 5 02-22-2007 08:43 PM


All times are GMT -7. The time now is 09:08 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0