Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 29-Jun-10 0:48am
forme546
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this:
 
"javascript:if(confirm('Are you sure you want to delete?') == false) return false;"
  Permalink  
v2
Comments
forme at 29-Jun-10 7:48am
   
Thanks but it does not work.. :(
digital man at 29-Jun-10 8:09am
   
Yes it does: I use it all the time. How have you implemented this?
Sandeep Mewara at 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 at 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
digital man at 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.
forme at 30-Jun-10 0:42am
   
I am 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.
digital man at 30-Jun-10 3:24am
   
Put the script code in the OnLoad event. In any case, you should test to ensure that it hasn't already been registered. If you don't understand this you should read up on it first. If I just give you everything how the hell are you going to learn anything?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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);
  Permalink  
Comments
forme at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Hi,
You can try this:
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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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
  Permalink  
Comments
sudevsu at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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
<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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 250
1 CHill60 210
2 Zoltán Zörgő 175
3 Richard Deeming 166
4 Peter Leow 160
0 Sergey Alexandrovich Kryukov 9,328
1 OriginalGriff 6,806
2 Peter Leow 4,442
3 Zoltán Zörgő 3,984
4 Richard MacCutchan 2,807


Advertise | Privacy | Mobile
Web02 | 2.8.150129.1 | Last Updated 28 Mar 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100