Click here to Skip to main content
16,016,894 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a html button and a hidden field like this
<button id="btnperfdel" runat="server" type="button" class="btn btn-outline-secondary" onclick="myconfirm"></button>
<asp:HiddenField ID="myanswer" runat="server" ClientIDMode="Static" />

and this is my script that i want to run
<script type = "text/javascript">
      function myconfirm() {

          var myanswer = document.getElementById("myanswer")

          if (confirm("Confirm Delete?")) {
              myanswer.value = "Yes"
          }
          else {
              myanswer.value = "No"
          }
      }

    </script>

and this is my background code
Protected Sub btnperfdel_ServerClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnperfdel.ServerClick

       If myanswer.Value = "Yes" Then
           'some random things
       Else
           ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('You clicked Cancel')", True)
       End If
   End Sub

But the javascript is not firing for onclick event. I tried onclientclick but if only returns alert 'You clicked cancel'.

What I have tried:

I tried other javascripts also, but all returns false value for onclientclick event and for onclick event, it doesn't doing anything. I'm novice in javascript. Please,Can someone help me out?
Posted
Comments
Bob@work 27-Oct-21 18:18pm    
You're trying to run a javascript triggered by a DOM even, but the button is controlled by the server. The button attribute runat="server" preempts the onclick="myconfirm" and treats the button like a server-side control, not part of the DOM.

Using your browser, right-click the button and inspect it. you'll see what the server is doing to the button code.

The asp page code:
<asp:button id="LoginButton" runat="server" commandname="Login" text="Log In" validationgroup="LoginUserValidationGroup">

will generate something like this on the user's browser:


< input type="submit" name="ctl00$MainContent$LoginUser$LoginButton" value="Log In" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$MainContent$LoginUser$LoginButton", "", true, "LoginUserValidationGroup", "", false, false))" id="ctl00_MainContent_LoginUser_LoginButton" >

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