简体   繁体   English

如何结合代码后面和 javascript 函数?

[英]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.我在 javascript(在客户端) alertmemedium()中有 function 调用警报,客户端应该选择两个选项中的一个。 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.我需要的是,如果他点击 OK 按钮,function RegisterUser() (来自后面的代码)将运行,如果客户端点击第二个按钮什么都不会发生。 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.因此,即使我在alertmemedium()中调用RegisterUser() ) - (就像 <%RegisterUser();%>) - 在 if 条件下,来自后面代码的 function 会忽略所有内容并在调用之前自行运行。 RegisterUser() written in code behind c#. RegisterUser() 用 c# 后面的代码编写。

the js function: 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 -->这是对 function 后面的 cade 的调用 -- 继续 -->

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

Better approach is to call an API instead method.更好的方法是调用 API 代替方法。 You may try the accepted answer from this query:您可以尝试从此查询中接受的答案:

How to call a C# function from JavaScript? 如何从 JavaScript 调用 C# function?

Another approach is to have a blank text button and fire that using click by javascript:另一种方法是有一个空白文本按钮并使用 javascript 的点击触发:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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