How do you display and process confirmation dialog?

Discussion in 'ASP .Net Web Controls' started by Amelyan, Apr 28, 2005.

  1. Amelyan

    Amelyan Guest

    What is the common way in ASP.NET C# to pop up a confirmation dialog box?
    E.g.

    Are you sure you want to delete this item?
    Yes No
     
    Amelyan, Apr 28, 2005
    #1
    1. Advertising

  2. Amelyan

    Brock Allen Guest

    You can emit javascript to call "confirm('Do you want to continue?')"

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > What is the common way in ASP.NET C# to pop up a confirmation dialog
    > box? E.g.
    >
    > Are you sure you want to delete this item?
    > Yes No
     
    Brock Allen, Apr 28, 2005
    #2
    1. Advertising

  3. Amelyan

    Amelyan Guest

    Hi Brock,

    I have implemented your suggestion. It works fine. Clicking on Delete
    button invokes the confirm script which pops up the message. Then when user
    click OK or Cancel, it invokes my server side btnDelete_Click handler.

    However, I don't know how to get the value OK or Cancel in my button click
    handler (please see TODO below). At that point, how do I get user input OK
    (true) or Cancel (false)?

    Thanks again,
    Amelyan

    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button btnDelete;
    private void Page_Load(object sender, System.EventArgs e)
    {
    string jsConfirm = @"<script language='javascript'>
    function confirmButton()
    {
    var agree=confirm('Delete this item?');
    if (agree)
    return true;
    else
    return false;
    }
    </script>";
    Page.RegisterClientScriptBlock("confirm",jsConfirm);
    btnDelete.Attributes.Add("onclick","return confirmButton();");
    }

    ...

    private void btnDelete_Click(object sender, System.EventArgs e)
    {
    // TODO: How do I know here whether user clicked OK or Cancel here?
    Response.Write("<script>alert('User clicked OK or Cancel!!');</script>");
    }
    }



    "Brock Allen" <> wrote in message
    news:...
    > You can emit javascript to call "confirm('Do you want to continue?')"
    >
    > -Brock
    > DevelopMentor
    > http://staff.develop.com/ballen
    >
    >
    >
    >> What is the common way in ASP.NET C# to pop up a confirmation dialog
    >> box? E.g.
    >>
    >> Are you sure you want to delete this item?
    >> Yes No

    >
    >
    >
     
    Amelyan, Apr 28, 2005
    #3
  4. Amelyan

    Brock Allen Guest

    You should never make it to btnDelete_Click if they hit cancel. This works
    for me and only calls the server event when they choose "OK":

    <script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
    _button.Attributes.Add("onclick", "return confirm('delete?');");
    }

    protected void _button_Click(object sender, EventArgs e)
    {
    Response.Write("Click");
    }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <body>
    <form id="form1" runat="server" >
    <asp:Button runat="server" ID="_button" Text="Click" OnClick="_button_Click"
    />
    </form>
    </body>
    </html>

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > Hi Brock,
    >
    > I have implemented your suggestion. It works fine. Clicking on
    > Delete button invokes the confirm script which pops up the message.
    > Then when user click OK or Cancel, it invokes my server side
    > btnDelete_Click handler.
    >
    > However, I don't know how to get the value OK or Cancel in my button
    > click handler (please see TODO below). At that point, how do I get
    > user input OK (true) or Cancel (false)?
    >
    > Thanks again,
    > Amelyan
    > public class WebForm1 : System.Web.UI.Page
    > {
    > protected System.Web.UI.WebControls.Button btnDelete;
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > string jsConfirm = @"<script language='javascript'>
    > function confirmButton()
    > {
    > var agree=confirm('Delete this item?');
    > if (agree)
    > return true;
    > else
    > return false;
    > }
    > </script>";
    > Page.RegisterClientScriptBlock("confirm",jsConfirm);
    > btnDelete.Attributes.Add("onclick","return confirmButton();");
    > }
    > ...
    >
    > private void btnDelete_Click(object sender, System.EventArgs e)
    > {
    > // TODO: How do I know here whether user clicked OK or Cancel here?
    > Response.Write("<script>alert('User clicked OK or
    > Cancel!!');</script>");
    > }
    > }
    > "Brock Allen" <> wrote in message
    > news:...
    >
    >> You can emit javascript to call "confirm('Do you want to continue?')"
    >>
    >> -Brock
    >> DevelopMentor
    >> http://staff.develop.com/ballen
    >>> What is the common way in ASP.NET C# to pop up a confirmation dialog
    >>> box? E.g.
    >>>
    >>> Are you sure you want to delete this item?
    >>> Yes No
     
    Brock Allen, Apr 28, 2005
    #4
  5. Amelyan

    Amelyan Guest

    Duh, :)

    Thank you!
    Amelyan

    "Brock Allen" <> wrote in message
    news:...
    > You should never make it to btnDelete_Click if they hit cancel. This works
    > for me and only calls the server event when they choose "OK":
    >
    > <script runat="server">
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > _button.Attributes.Add("onclick", "return confirm('delete?');");
    > }
    >
    > protected void _button_Click(object sender, EventArgs e)
    > {
    > Response.Write("Click");
    > }
    > </script>
    >
    > <html xmlns="http://www.w3.org/1999/xhtml">
    > <body>
    > <form id="form1" runat="server" >
    > <asp:Button runat="server" ID="_button" Text="Click"
    > OnClick="_button_Click" />
    > </form>
    > </body>
    > </html>
    >
    > -Brock
    > DevelopMentor
    > http://staff.develop.com/ballen
    >
    >
    >
    >> Hi Brock,
    >>
    >> I have implemented your suggestion. It works fine. Clicking on
    >> Delete button invokes the confirm script which pops up the message.
    >> Then when user click OK or Cancel, it invokes my server side
    >> btnDelete_Click handler.
    >>
    >> However, I don't know how to get the value OK or Cancel in my button
    >> click handler (please see TODO below). At that point, how do I get
    >> user input OK (true) or Cancel (false)?
    >>
    >> Thanks again,
    >> Amelyan
    >> public class WebForm1 : System.Web.UI.Page
    >> {
    >> protected System.Web.UI.WebControls.Button btnDelete;
    >> private void Page_Load(object sender, System.EventArgs e)
    >> {
    >> string jsConfirm = @"<script language='javascript'>
    >> function confirmButton()
    >> {
    >> var agree=confirm('Delete this item?');
    >> if (agree)
    >> return true;
    >> else
    >> return false;
    >> }
    >> </script>";
    >> Page.RegisterClientScriptBlock("confirm",jsConfirm);
    >> btnDelete.Attributes.Add("onclick","return confirmButton();");
    >> }
    >> ...
    >>
    >> private void btnDelete_Click(object sender, System.EventArgs e)
    >> {
    >> // TODO: How do I know here whether user clicked OK or Cancel here?
    >> Response.Write("<script>alert('User clicked OK or
    >> Cancel!!');</script>");
    >> }
    >> }
    >> "Brock Allen" <> wrote in message
    >> news:...
    >>
    >>> You can emit javascript to call "confirm('Do you want to continue?')"
    >>>
    >>> -Brock
    >>> DevelopMentor
    >>> http://staff.develop.com/ballen
    >>>> What is the common way in ASP.NET C# to pop up a confirmation dialog
    >>>> box? E.g.
    >>>>
    >>>> Are you sure you want to delete this item?
    >>>> Yes No

    >
    >
    >
     
    Amelyan, Apr 28, 2005
    #5
  6. Here's some server side code that uses javascript to display a confirmation
    message.

    myDeleteButton.Attributes.Add("onclick", _
    "return confirm('Are you sure you want to delete?');")

    In this example, the Delete button will post back only if the person
    confirms they want to delete. Otherwise your server code is never called in
    response to the button click.

    Here's a more detailed analysis of your options:
    http://SteveOrr.net/articles/clientsidesuite.aspx

    And here's information about making this technique work with a datagrid:
    http://www.dotnetjunkies.com/HowTo/1E7FEE4A-795C-4D33-A135-843EB07C94A8.dcik

    --
    I hope this helps,
    Steve C. Orr, MCSD, MVP
    http://Steve.Orr.net


    "Amelyan" <> wrote in message
    news:%...
    > What is the common way in ASP.NET C# to pop up a confirmation dialog box?
    > E.g.
    >
    > Are you sure you want to delete this item?
    > Yes No
    >
     
    Steve C. Orr [MVP, MCSD], Apr 29, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alek Davis
    Replies:
    1
    Views:
    6,378
    Alek Davis
    Aug 29, 2003
  2. Amelyan
    Replies:
    1
    Views:
    8,382
    Steve C. Orr [MVP, MCSD]
    Apr 29, 2005
  3. Joey
    Replies:
    1
    Views:
    627
    S. Justin Gengo
    Oct 28, 2005
  4. Amil Hanish
    Replies:
    0
    Views:
    324
    Amil Hanish
    Aug 14, 2006
  5. Matt
    Replies:
    1
    Views:
    288
    Randy Webb
    Nov 20, 2004
Loading...

Share This Page