I want to run this confirm script but it's not working for both HTML and asp.net button. it's Showing only "You clicked cancel". Can someone help me out? Here is my code.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<button id="btnperfdel" runat=server type="button" class="btn btn-outline-secondary" onclientclick="confirm()"><i class="mdi mdi-delete">Delete</i> </button>
<asp:Button ID="Button1" runat="server" Text="Button" onclientclick="confirm()"/>
</div>
<script type = "text/javascript">
function confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Confirm Delete?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
Here's my backend
Protected Sub btnperfdel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnperfdel.ServerClick
Dim confirmValue As String = ""
confirmValue = Request.Form("confirm_value")
If confirmValue = "Yes" Then
Label1.Visible = False
Else
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('You clicked Cancel')", True)
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim confirmValue As String = ""
confirmValue = Request.Form("confirm_value")
If confirmValue = "Yes" Then
Label1.Visible = False
Else
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('You clicked Cancel')", True)
End If
End Sub
CodePudding user response:
And you using the same name for a function as the built in js fucntion name confirm?
That is a "very" bad idea.
Try giving your js function name a name that's not the same as a built in function.
Say like this:
<asp:Button ID="Button1" runat="server" Text="Button" onclientclick="myconfirm()"/>
<script type = "text/javascript">
function myconfirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Confirm Delete?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
So, that looks to be a VERY bad name for the function name.
Also, note that you can "return" true/false to the button click. I would think that in 99% of cases, the user clicks cancel, then you don't want the code to run, and no further action is required. So you can in fact do this:
<asp:Button ID="Button1" runat="server" Text="Button" onclientclick="return myconfirm2()"/>
<script type = "text/javascript">
function myconfirm2() {
return confirm("Confirm Delete?");
}
If you do the above, then the button click does not run if user clicks cancel.
In fact, you can even dump ALL of the code, and do this:
<asp:Button ID="Button1" runat="server" Text="Button"
onclientclick="return confirm('confirm delete');"/>
So, if you return true, or false for the client click event, then the server side code button will not run. And in 9 out of 10 cases, you don't need to know if user hit cancel, but ONLY that we don't run the button code server side anyway.
Regardless, change your name of confirm, since that is a built in js function name, and that is VERY confusing to use the same name.
Edit:====================================================
I would suggest you try/use this:
<asp:Button ID="Button1" runat="server" Text="Button" onclientclick="myconfirm()"/>
<asp:HiddenField ID="myanswer" runat="server" ClientIDMode="Static" />
<script type = "text/javascript">
function myconfirm() {
var myanswer = document.getElementById("myanswer")
if (confirm("Confirm Delete?")) {
myanswer.value = "Yes"
}
else {
myanswer.value = "No"
}
}
</script>
And code behind would be:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If myanswer.Value = "Yes" Then
' do whatever here for yes
Debug.Print("yes")
Else
' do whtever here for no
Debug.Print("no")
End If
End Sub