search
Japanese Chinese Nederlands Espanol Italiano Deutsch Francais Twitter Rss Feeds
MicrosoftArticlesForumsFAQs
C# .NET
VB.NET
Visual Studio .NET
ADO.NET
Xml / Xslt
VB 6.0
.NET CF
GDI+
LINQ
Deployment
Security
FoxPro
Silverlight / WPF
Entity Framework
RIA Services

Web ProgrammingArticlesForumsFAQs
JavaScript
ASP
ASP.NET
Web Services

Non-MicrosoftArticlesForumsFAQs
NHibernate
Perl
PHP
Ruby
Java
Linux / Unix
Apple
Open Source

DatabasesArticlesForumsFAQs
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsFAQs
Excel
Word
Powerpoint
Outlook
Publisher
Money

Operating SystemsArticlesForumsFAQs
Windows 7
Windows Server
Windows Vista
Windows XP
Windows Update
MAC
Linux / UNIX

Server PlatformsArticlesForumsFAQs
BizTalk
Site Server
Exhange Server
IIS

Graphic DesignArticlesForumsFAQs
Macromedia Flash
Adobe PhotoShop
Expression Blend
Expression Design
Expression Web

OtherArticlesForumsFAQs
Subversion / CVS
Ask Dr. Dotnetsky
Active Directory
Networking
Uninstall Virus
Job Openings
Product Reviews
Search Engines
Resumes

 

Confirm Message Box before Deleting


By Pritam Baldota
Printer Friendly Version
View My Articles
248 Views
    

Confirm Message Box before Deleting in Datagrid in ASP.Net is very useful in all applications.


Confirm Message Box before Deleting

Scenarion I

To use confirm Message box before delete operation uses JavaScript for confirmation box. You have to add JavaScript on button.

Page_Load

if(!IsPostBack)   btnConfirm.Attributes.Add("onclick","return confirm('Do you want to Delete');");

Once clicked Ok button on Confirm Box it executes Button_Click server event else doesn’t do anything.

btnConfirm_Click
 
//Your server code to delete

Scenario II

The DataList and DataGrid controls easily allow to add a "Delete" button/hyperlink in your template. When clicked, this button/link raises a DeleteCommand event that you can handle to delete the data item.

<asp:datagrid id="dg" runat="server"AutoGenerateColumns="False">
<Columns>
<asp:ButtonColumn Text="Delete" ButtonType="PushButton" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:datagrid>


DataGrid’s Template Delete Button raises DeleteCommand event after clicking Delete Button of DataGrid. We needs to write javascript confirm method to this template button. This should be write in DataGrid’s ItemCreated event because it fires when datagrid’s item has been created.

private void dg_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
 
//Check for Item and Alternate Items if(e.Item.ItemType==ListItemType.AlternatingItem || e.Item.ItemType==ListItemType.Item)
  {
 Button btn=(Button)e.Item.Cells[0].Controls[0];
btn.Attributes.Add("onclick","return confirm(‘Do you want to continue?’);");
  }

}



button
Article Discussion: Confirm Message Box before Deleting
Pritam Baldota posted at Friday, September 29, 2006 4:46 AM
Original Article
 

Confirm Message Box
K Pravin Kumar Reddy replied to Pritam Baldota at Friday, September 29, 2006 4:55 AM

hello

use this

'Shows a confirmation box when button delete is clicked
        BtnDelete.Attributes.Add("onclick", "return confirm('are you sure you want to delete? ');")

c#.net

BtnDelete.Attributes.Add("onclick", "return confirm('are you sure you want to delete? ');");

 

  good one here
K Pravin Kumar Reddy replied to Pritam Baldota at Friday, September 29, 2006 4:57 AM

hello





The javascript code for it is simple:


function confirm_delete()
{
  if (confirm("Are you sure you want to delete the custom search?")==true)
    return true;
  else
    return false;
}


Using code-behind, you can attach the javascript popup dialog to the button:


_myButton.Attributes.Add("onclick", "return confirm_delete();");


If the button is inside a repeater (in this case called Searches), for example, which most of mine our, you have to attach it as follows by handling the ItemCreated event:

override protected void OnInit(EventArgs e)
{
   base.OnInit(e);
   this.Load += new System.EventHandler(this.Page_Load);
   Searches.ItemCreated += new RepeaterItemEventHandler(this.Item_Created);
   Searches.ItemCommand += new RepeaterCommandEventHandler(this.DeleteSearch_Click);
}

private void Item_Created(Object Sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
      {
            ImageButton _myButton = (ImageButton)e.Item.FindControl("btnDelete");
            _myButton.Attributes.Add("onclick", "return confirm_delete();");
      }
}

private void DeleteSearch_Click(object sender, RepeaterCommandEventArgs e)
{
    int _id = Int32.Parse(e.CommandArgument.ToString());
    // Do Your Thing...
}


Inside your repeater, you add the button as such:


... ImageButton id=btnDelete ImageUrl="images/icon_delete.gif" runat="server" CommandArgument='<%# ((DataRowView)Container.DataItem)["ID"] %> ...

When you click on the delete button, the javascript popup dialog asks if you want to delete the search. If you choose “cancel“, your “DeleteSearch_Click” event is never fired. If you choose “ok”, the event is fired and you can delete the item.

Adds a bit of professionalism to your web applications without requiring a lot of effort.

 

if the link button is inside a datalist
Sneha Mishra replied to K Pravin Kumar Reddy at Sunday, August 31, 2008 4:39 AM
if the link button is inside a datalist what would be the code for deleting an item using .net version 1.1
 

Cancel also deletes!
Ibrahim Bello replied to K Pravin Kumar Reddy at Friday, February 06, 2009 11:19 AM

Hi, I tried the above javascript code, confirm_delete() but to my chagrin cancel also deletes, even close deletes.

I'm coding in VB.NET. My page inherits from a masterpage. Thanks

 

ButtonColumn Confirmation in VB.net
Jass singh replied to Pritam Baldota at Wednesday, March 18, 2009 2:08 AM

Dear sir,

Following is ur code for C#.

Can u pls send same for vb.net...

required urgently

Thanks & Regards

jass

---------------------------------------------------------------------------------

DataGrid’s Template Delete Button raises DeleteCommand event after clicking Delete Button of DataGrid. We needs to write javascript confirm method to this template button. This should be write in DataGrid’s ItemCreated event because it fires when datagrid’s item has been created.

private void dg_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
 
//Check for Item and Alternate Items if(e.Item.ItemType==ListItemType.AlternatingItem || e.Item.ItemType==ListItemType.Item)
  {
 Button btn=(Button)e.Item.Cells[0].Controls[0];
btn.Attributes.Add("onclick","return confirm(‘Do you want to continue?’);");
  }

}

 

Confirmation message on delete button on some condition.
Pradeep Thorat replied to Jass singh at Wednesday, April 01, 2009 12:50 AM

The confirmation message should pop up on delete button click but after checking some condition. If the condition is false the confirmation message should not pop up. If there is some code for this please let me know.   It's very urgent.

Thanks in advance.