Home All Groups Group Topic Archive Search About

How do you display and process confirmation dialog?

Author
28 Apr 2005 7:21 PM
Amelyan
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

Author
28 Apr 2005 8:56 PM
Brock Allen
You can emit javascript to call "confirm('Do you want to continue?')"

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



Show quoteHide quote
> 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
Author
28 Apr 2005 9:13 PM
Amelyan
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>");
  }
}



Show quoteHide quote
"Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
news:582805632503041665175808@msnews.microsoft.com...
> 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
>
>
>
Author
28 Apr 2005 9:26 PM
Brock Allen
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



Show quoteHide quote
> 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" <ballen@NOSPAMdevelop.com> wrote in message
> news:582805632503041665175808@msnews.microsoft.com...
>
>> 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
Author
28 Apr 2005 9:34 PM
Amelyan
Duh, :-)

Thank you!
Amelyan

Show quoteHide quote
"Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
news:582815632503060010755456@msnews.microsoft.com...
> 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" <ballen@NOSPAMdevelop.com> wrote in message
>> news:582805632503041665175808@msnews.microsoft.com...
>>
>>> 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
>
>
>
Author
28 Apr 2005 11:24 PM
Steve C. Orr [MVP, MCSD]
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


Show quoteHide quote
"Amelyan" <bamel***@wi.rr.com> wrote in message
news:%23J1R6dCTFHA.3464@tk2msftngp13.phx.gbl...
> 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
>