what is the difference between these two code snippets
1)
<asp:Button ID="BtnDelete" runat="server" Text="Delete" style = "width: 95%" OnClientClick="return confirm('Are you sure you want to delete the EMP?');"
onclick="BtnDeleteEmp_Click"/>
this works perfect, when i click on the button it asks me whether i am sure, if i click ok then the code in code behind executes and deletes the record from the database.
2) but i dont want to ask the confirmation to the user every time.. i used some validation, only when there is a value in dropdown list i need to ask confirmation , so i modified like this.
Because both call onclick="BtnDeleteEmp_Click". The only difference is hte onclientclick is calling a separate js routine while the other runs it inline. So essentially, they do the same thing, the js location is different.
var classif = $('#<%=ddlClass2.ClientID%>').val();
if (classif != "Please select") {
if (confirm("Are you sure you want to delete the Class? This action cannot be undone.")) {
return true;
}
Csharp22
Member
296 Points
410 Posts
What is the difference
Mar 27, 2012 04:16 PM|LINK
Hi ,
what is the difference between these two code snippets
1)
<asp:Button ID="BtnDelete" runat="server" Text="Delete" style = "width: 95%" OnClientClick="return confirm('Are you sure you want to delete the EMP?');"
onclick="BtnDeleteEmp_Click"/>
this works perfect, when i click on the button it asks me whether i am sure, if i click ok then the code in code behind executes and deletes the record from the database.
2) but i dont want to ask the confirmation to the user every time.. i used some validation, only when there is a value in dropdown list i need to ask confirmation , so i modified like this.
<asp:Button ID="BtnDelete" runat="server" Text="Delete" style = "width: 95%" OnClientClick="Confirm()"
onclick="BtnDeleteEmp_Click"/>
function Confirm() {
var class = $('#<%=ddlClass2.ClientID%>').val();
if (class != "Please select") {
return confirm('Are you sure you want to delete the EMP?');
}
}
when i used the above code, no matter if i click yes or no in the confirmation it always executes the code behind c# code , what am i missing?
bbcompent1
All-Star
33097 Points
8529 Posts
Moderator
Re: What is the difference
Mar 27, 2012 04:18 PM|LINK
Because both call onclick="BtnDeleteEmp_Click". The only difference is hte onclientclick is calling a separate js routine while the other runs it inline. So essentially, they do the same thing, the js location is different.
ajb587
Member
442 Points
102 Posts
Re: What is the difference
Mar 27, 2012 04:19 PM|LINK
The return value of you functin Confirm() is not being passed to the ASP Button control.
Try
<asp:Button ID="BtnDelete" runat="server" Text="Delete" style = "width: 95%" OnClientClick="return Confirm();"
onclick="BtnDeleteEmp_Click"/>
bbcompent1
All-Star
33097 Points
8529 Posts
Moderator
Re: What is the difference
Mar 27, 2012 04:21 PM|LINK
Csharp22
Member
296 Points
410 Posts
Re: What is the difference
Mar 27, 2012 04:45 PM|LINK
I tried the above code, but now even thougth i clicked OK, it doesnt execute the server side code.
PS: The delete button is inside the update panel
<asp:Button ID="BtnDelete" runat="server" UseSubmitBehavior = "false" Text="Delete" style = "width: 95%" OnClientClick="return Confirm();"
onclick="BtnDeleteEmp_Click"/>
function Confirm() {
var classif = $('#<%=ddlClass2.ClientID%>').val();
if (classif != "Please select") {
if (confirm("Are you sure you want to delete the Class? This action cannot be undone.")) {
return true;
}
}
else {
return false;
}
}
bbcompent1
All-Star
33097 Points
8529 Posts
Moderator
Re: What is the difference
Mar 27, 2012 04:52 PM|LINK
Hmm, what does your code behind look like?
bbcompent1
All-Star
33097 Points
8529 Posts
Moderator
Re: What is the difference
Mar 27, 2012 04:52 PM|LINK
I would set a breakpoint at that point in your code behind to see what happens and if its even calling the routine.
Csharp22
Member
296 Points
410 Posts
Re: What is the difference
Mar 27, 2012 04:57 PM|LINK
Code behind just deletes the record from the database.
i have a breakpoint, but after the changes it never hits the breakpoint.
bbcompent1
All-Star
33097 Points
8529 Posts
Moderator
Re: What is the difference
Mar 27, 2012 05:15 PM|LINK
You can always do this guy as a code behind check like in this thread.
http://forums.asp.net/t/1517822.aspx/1
ajb587
Member
442 Points
102 Posts
Re: What is the difference
Mar 27, 2012 05:23 PM|LINK
Javascript
<script type="text/javascript"> function Confirm() { var ddlClass = $('#<%=ddlClass2.ClientID%>').val(); rval = null; if (ddlClass != "Please Select") rval = confirm('Are you sure you want to delete the EMP?'); else rval = false; alert(rval); return rval; } </script>ASPX
It looks like the "UseSubmitBehavior" property is preventing the server sider post back in your current code.