Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have a gridview and on its selected index changed event i am using ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "confirm('Are you sure you want to delete');", true);

This works fine and displays confirm message.
My problem is if the result of the confirm message is true then i want to execute some code and if the result of confirm message is false i don't want the code to execute.

But at present in either case the code gets executed.

Can anyone help me out please.

I am using Ajax update panel and VS2008

Thanks in advance
Posted

Try this:

"javascript:if(confirm('Are you sure you want to delete?') == false) return false;"
 
Share this answer
 
v2
Comments
forme 29-Jun-10 7:48am    
Thanks but it does not work.. :(
R. Giskard Reventlov 29-Jun-10 8:09am    
Yes it does: I use it all the time. How have you implemented this?
Sandeep Mewara 29-Jun-10 8:55am    
Reason for my vote of 5
It *will* work. If you say it is not working then you are doing something wrong. All you need is to have a if based on the return value and stop the execution using return false. Try again or post what you tried that isn't working.
forme 29-Jun-10 9:09am    
Thank you
This is my code
if (ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "javascript:if(confirm('Are you sure you want to delete?') == false) return false;", true)==true)
{
some code.....
}

and it gives me an error "operator == cannot be applied to operands of type void
R. Giskard Reventlov 29-Jun-10 9:20am    
NO!

ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "javascript:if(confirm('Are you sure you want to delete?') == false) return false;", true);

is all you need.
use return confirm instead of just confirm like below.


ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "return confirm('Are you sure you want to delete');", true);
 
Share this answer
 
Comments
forme 30-Jun-10 3:04am    
Am I doing something wrong? but i am not getting what is the mistake i am doing.Kindly put some code. Here is the complete code that i am using protected void gvSuppDelete_SelectedIndexChanged(object sender, EventArgs e) { ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "script", "javascript:if(confirm('Are you sure you want to delete?') == false) return false;", true); SupplierItem si = new SupplierItem(); si.Id = Convert.ToInt32(gvSuppDelete.SelectedValue); si.SupplierName = gvSuppDelete.SelectedRow.Cells[1].Text; si.SupplierAddress = gvSuppDelete.SelectedRow.Cells[2].Text; si.LastUpdatedBy = Session["UserName"].ToString(); si.Status = false; try { BALSupplier sup = new BALSupplier(); int res = sup.UpdateSupplier(si); if (res > 0) { CloseWindow = "alert('Deleted successfully')"; ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "CloseWindow", CloseWindow, true); //lblMsg.Text = "Deleted successfully"; Clear(); gvSuppDelete.DataBind(); } else { if (res == -1) { CloseWindow = "alert('Supplier could not be deleted')"; ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "CloseWindow", CloseWindow, true); } } } catch (Exception ex) { CloseWindow = "alert('" + ex.Message + "')"; ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "CloseWindow", CloseWindow, true); } } please tell me where i am going wrong.
From your comments on the other answers, you seem to be going about this wrong. First of all, ScriptManager.RegisterStartupScript is a void function. This means that it doesn't return anything. So, you can't use it in an if statement like you've done.

I'm not that familiar with the web side, but I can tell you that what you're doing is wrong.

You probably need to register a full script that includes the code to be run if the person confirms that they want to delete.

I'm also curious why you would run code only if registering the startup script succeeded. That won't actually run the confirm, it just sets the UpdatePanel to run it when it is updated.
 
Share this answer
 
Hi,
You can try this:
C#
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "javascript:return confirm('Are you sure you want to delete');", true);


As per the scenario which you are telling, it is working fine for me.


--Amit
 
Share this answer
 
Comments
jkkr 31-Mar-17 1:48am    
how to get the return value in this code ?
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "javascript:if(confirm('Are you sure you want to delete?') == false) return false;", true);

means user select yes then { my loigc here} no { then another}

plz give me soltuion
 
Share this answer
 
Comments
sudevsu 31-Dec-14 11:05am    
Did you achieve this Vinod? I have a same situation, on Dropdown select value I need a confirm message and clicking ok shud do somecode cancel close and continue with grid
See you can add (OnClientClick="return confirm('Are you sure you want to delete?');") this code in your server-side control

example of server-side control in .aspx file
XML
<asp:LinkButton runat="server" ID="lbDelete" Text="Delete" CssClass="ActionLinkButton" OnClientClick="return confirm('Are you sure you want to delete?');" CommandName="lbDelete" 
CommandArgument='<%#((GridViewRow)Container).RowIndex%>'>
</asp:LinkButton>


That will show a confirm dialog, and the Server-Side Click event will be executed only if the user selects Ok.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900