简体   繁体   中英

how can I combine code behind and javascript functions?

I have function in javascript (in client side) alertmemedium() that call to an alert, which the client shpuld choose one option of two. And I need that if he will click the OK button, The function RegisterUser() (from code behind) will run, if the client will click the seconed button nothing will happened. So even if I call RegisterUser() in alertmemedium() -- (like that <%RegisterUser();%>) -- in if condition the function from code behind ignore everything and just run by itself before it called. RegisterUser() written in code behind c#.

the js function:

function alertmemedium(){
        const swalWithBootstrapButtons = Swal.mixin({
          customClass: {
            confirmButton: 'btn btn-success',
            cancelButton: 'btn btn-danger'
          },
          buttonsStyling: false
        })

        swalWithBootstrapButtons.fire({
          title: 'Are you sure?',
          text: "Your Password has a medium strength!",
          type: 'warning',
          showCancelButton: true,
          confirmButtonText: 'Yes!',
          cancelButtonText: 'No, cancel!',
          reverseButtons: true
        }).then((result) => {
          if (result.value) {
            swalWithBootstrapButtons.fire(
              'Well done!',
              'You signed up to our website.',
              'success'
              )

this is the call to the cade behind function -- continue -->

              <%RegisterUser();%>
          } else if (
            result.dismiss === Swal.DismissReason.cancel
          ) {
            swalWithBootstrapButtons.fire(
              'Cancelled',
              'Improve Your Password!:)',
              'error'
              )
                          }
            })
    }

Better approach is to call an API instead method. You may try the accepted answer from this query:

How to call a C# function from JavaScript?

Another approach is to have a blank text button and fire that using click by javascript:

//make a button:
<asp:Button ID="DummyButton" runat="server" Text="" OnClick="RegisterUser"  />

//fire follwing by replacing <%RegisterUser();%>
document.getElementById('<%= DummyButton.ClientID %>').click();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM