How do you display and process confirmation dialog?

A

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
 
A

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>");
}
}
 
B

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>
 
A

Amelyan

Duh, :)

Thank you!
Amelyan

Brock Allen said:
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>
 
S

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top