Home > ASP.NET > Display confirm message

Display confirm message

In windows application, when user want to delete any information, usually a confirm message box’ll be launched, so if user really want to delete they must confirm this action again. In ASP.NET we can easily do the same in two ways:

1. Register confirm action inside the PreRender event to add javascript into the client side so it’ll operate when user click on the button.

private void Page_PreRender(object sender, EventArgs e)
{
   btnDelete.Attributes.Add("OnClick", "return confirm('Do you really want to delete this row? No undo action is available')");
}

2. Add  “OnClientClick” event inside HTML code like this:

<asp:Button ID="btnDelete" runat="server" OnClientClick="javascript:return confirm('Do you really want to delete this row? No undo action is available');" />

Hope this help you!!!

Advertisements
Categories: ASP.NET Tags:
  1. May 24, 2006 at 5:20 AM

    can u provide if wan to delete from gridview msgbox yes no?

  2. May 24, 2006 at 9:02 AM

    Just replace your delete command button and using the below code snipset

    <asp:TemplateField HeaderText="Handle">
          <ItemTemplate>
               <asp:Button ID="btnEdit" Text="Edit" Width="70px" CommandName="Edit"  runat="server" />
               <asp:Button ID="btnDelete" Text="Delete" Width="70px" OnClientClick="return confirm('Do you want to delete this row?')"                            CommandName="Delete"  runat="server" />
          </ItemTemplate>
          <EditItemTemplate>
                <asp:Button ID="btnUpdate" Text="Update" Width="70px" CommandName="Update" runat="server" />
                <asp:Button ID="btnCancel" Text="Cancel" Width="70px" CommandName="Cancel" runat="server" />
          </EditItemTemplate>
    </asp:TemplateField>

    Anything else is the same as the traditional code

  3. thalz142
    May 30, 2006 at 4:32 PM

    Hi sorry siphu im a newbies…u jus said Anything else is the same as the traditional code i v using vb6 b4 asp.net but i think its not same…pls

    Protected Sub GridView_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView.RowDeleting

    Me.GridView.Attributes.Add(“OnClick”, “return confirm(‘Are you sure you want to delete?’);”)

    but at the first time no msgbox appear and data delete without msgbox…when i click delete again after that its work..i thing i mus check it first to delete… how im gonna to fire it let say

    if gridview,…or somthing . row delete = true then
    delete

    else
    cancel click
    endif

    ist like this??

    can u help me on this..thankss

  4. siphu
    May 31, 2006 at 2:32 AM

    hi there!
    In ASP.NET 2.0, there is a attributes called “OnClientClick” where you can add an javascript call directly (in ASP.NET 1.0 or 1.1 it does not exist). So if you still using ASP.NET 1.0 or 1.1 you must add attribute using the OnItemDataBound event

    In the ItemDataBound event, you add attribute for delete button like this

    Button btn = e.Item.FindControl(“btnDelete”) as Button;
    btn.Attributes.Add(“OnClick”, “return confirm(‘Do you really want to delete this item?’)”);

    you shound not add “OnClick” attribute at the delete event, because the attribute only apply for delete button after you click on that button

    Hope this help you!

  5. June 1, 2006 at 9:26 AM

    hi sorry again its not work im using visual web developer 2.0…there is no OnItemDataBound but

    Protected Sub GridView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView.DataBound
    ‘Dim strScript As String = “”
    ‘strScript += “Confirm()”
    ‘strScript += “”
    ‘If (Not Page.ClientScript.IsStartupScriptRegistered(“clientScript”)) Then
    ‘ Page.ClientScript.RegisterStartupScript(Page.GetType(), “clientScript”, strScript)
    ‘Else
    ‘ Return
    ‘End If

    End Sub

    its not work….this my gridview

    can u help i cant fire that msgbox wheather click yes….can u help pls i v try to many sample on google still not work..plss:)

  6. thalz142
    June 1, 2006 at 4:36 PM

    hi there sorry i try to follow tis tutorial here http://aspalliance.com/articleViewer.aspx?aId=786&pId=-1

    and its work but when i put in code error

    Invalid postback or callback argument. Event validation is enabled using in configuration or in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

    if im follow the tutorial its so easy and work and nothing to do wit vb code behind…ermmmm…is there anything u can giv me some tip?? thx ur advance:)

  7. June 14, 2006 at 4:50 AM

    ok its work now thx siphu 🙂

  8. November 4, 2008 at 6:16 PM

    Hello admin, nice site you have!,

  9. November 27, 2008 at 10:13 AM

    Grant you just made yourself something to do with it,

  10. November 27, 2008 at 12:12 PM

    It is healthy, I shall come on your site more often, thank.,

  11. November 27, 2008 at 5:43 PM

    Wow!! It’s getting better and better.,

  12. anemictione
    May 27, 2010 at 11:57 AM

    Great prices on ED medications –

  13. Ashok kumar
    December 22, 2010 at 9:20 PM

    Hi,
    Sorry to tell like this.

    I am talking about comments. People are writing comments however they like hence causing nuisance. Request everybody to think and give comment.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: