I have a delete button inside an UpdatePanel. I need to display a confirm popup, so that the user can confirm if he wants to delete or not. The Update has its UpdateMode as "Conditional", and, inside the Button's on_click event, I have the following code:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
string script = "<script language='javascript'> confirm('Are you sure you want to change this selection ? ')</script>";
ScriptManager.RegisterClientScriptBlock(Button1, typeof(Button), "Popup", script, false);
GridView1.SelectedIndex = 1;
// here goes the code that erases from the database
}
The weird thing is that the GridView.SelectedIndex statement is always executed, and it doesn't even wait for the user to select OK or CANCEL from the Confirm dialog.
My question is, how can I get the return values from the confirm dialog? Is it possible? If it isn't possible, how can I achieve that the code is executed only if the user press "OK"?
I've read some really good answers in this forum, I hope you can help me.
Thanks!!
You should add an onclick event to your delete button.
MyButton.Attributes.Add("onclick","return confirm('my message');")
The 'return' statement will exit the click event without firing a postback if cancel was clicked. If ok is clicked, the event fires like normal.
I tried this and it doesn't work. I wrote the code on the page_load event, but when I click the button, it does fire the Button_click event but doesn't display the dialog . I am sure it has to do with the UpdatePanel that I added to the page. The only way I have found to display the dialog is the one I posted before, using the ScriptManager.RegisterClientScriptBlock. I added the return in the following statement, but it still doesn't work, it still executes the code that follows without waiting for the user's answer.
ScriptManager.RegisterClientScriptBlock(Button1, typeof(Button), "Popup", "return confirm('my message');", true);
It works fine for me. I'm a VB guy, but luckily translating wasn't too hard. Here's a complete page in C#:
code-behind file:
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partialclass ConfirmOnUpdatePanel : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e) {this.ImageButton1.Attributes.Add("onclick","return confirm('Continue?');"); }protected void ImageButton1_Click(object sender, ImageClickEventArgs e) {this.ImageButton1.AlternateText ="Clicked " + System.Convert.ToString(DateTime.Now); }}
aspx code:
<%@. Page Language="C#" AutoEventWireup="true" CodeFile="ConfirmOnUpdatePanel.aspx.cs" Inherits="ConfirmOnUpdatePanel" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> </div> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:ImageButton ID="ImageButton1" runat="server" AlternateText="Delete" OnClick="ImageButton1_Click" /> </ContentTemplate> </asp:UpdatePanel> </form></body></html>
I copied and pasted you posted and it still didn't work. So, I took the Button.Attributes.Add out of the page load and, instead, put it in the Button_clicked event. Now, the behavior changed. The first time you click the button , it doesn't do anything. Then, you click it again and it works ok.
I tried putting the code of the Button.Attributes both in the page_load event and the button_clicked event and it does the same thing: doesn't shows up the first time.
Any idea why this happens?
The .aspx I'm using is the same that you posted here and the .aspx.cs is the following:
using System;
using System.Data;
using
System.Configuration;using
System.Collections;using
System.Web;using
System.Web.Security;using
System.Web.UI;using
System.Web.UI.WebControls;using
System.Web.UI.WebControls.WebParts;using
System.Web.UI.HtmlControls;public
partialclassConfirmOnUpdatePanel : System.Web.UI.Page{
protectedvoid Page_Load(object sender,EventArgs e){
}
protectedvoid ImageButton1_Click(object sender,ImageClickEventArgs e){
this.ImageButton1.Attributes.Add("onclick","return prompt('Continue?');");this.ImageButton1.AlternateText = System.Convert.ToString(DateTime.Now);}
}
Thanks in advance!
No comments:
Post a Comment