简体   繁体   English

从C#背后的代码调用JS函数

[英]Call JS function from code behind C#

I have a function onRowClick called RowClick and is working fine. 我有一个名为RowClick的onRowClick函数,并且工作正常。 I am trying to move it to a button and call the function from the code behind. 我试图将其移至按钮并从后面的代码中调用该函数。 For some reason is not triggering the function.. Anyone knows why and how I can fix this? 由于某些原因未触发该功能。.任何人都知道为什么以及如何可以解决此问题?

aspx.cs aspx.cs

  if (e.CommandName == "Addvoucher")
            {

               GridDataItem item = (GridDataItem)e.Item;

            var id = item.GetDataKeyValue("RowID");

            ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "RowClick("+id+");", true);

            }

aspx aspx

  <script>
        var popUpObj;
        function RowClick(sender, eventArgs) {
            var filterId = eventArgs.getDataKeyValue('RowID');


            popUpObj = window.open("voucher.aspx?param=" + filterId + "",
             "ModalPopUp",
             "toolbar=no," +
             "scrollbars=no," +
             "location=no," +
             "statusbar=no," +
             "menubar=no," +
             "resizable=0," +
             "width=530," +
             "height=500," +
             "left = 450," +
             "top=130" 
            );
             popUpObj.focus();
             LoadModalDiv();


         }


     function LoadModalDiv()
     {
         var bcgDiv = document.getElementById("divBackground");
         bcgDiv.style.display="block";
     }

     function HideModalDiv() {
         var bcgDiv = document.getElementById("divBackground");
         bcgDiv.style.display = "none";
     }

     </script>

IN page voucher.aspx 页内voucher.aspx

 <script type = "text/javascript">

             function OnClose() {

                 if (window.opener != null && !window.opener.closed) {
                     window.opener.location.reload(); //refreshing parent when popup close
                     // window.opener.HideModalDiv();
                 }

                 //if (window.closed==true) window.open("~/routedoc.aspx");
             }
             window.onunload = OnClose;

    </script>

Calling JavaScript function on code behind ie On Page_Load 在后面的代码上调用JavaScript函数,即在Page_Load上

ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:FUNCTIONNAME(); ", true);

If you have UpdatePanel there then try like this 如果那里有UpdatePanel ,请尝试这样

ScriptManager.RegisterStartupScript(GetType(), "Javascript", "javascript:FUNCTIONNAME(); ", true);

Change your js function like this 像这样更改您的js函数

function RowClick(filterId) {

popUpObj = window.open("voucher.aspx?param=" + filterId + "",
         "ModalPopUp",
         "toolbar=no," +
         "scrollbars=no," +
         "location=no," +
         "statusbar=no," +
         "menubar=no," +
         "resizable=0," +
         "width=530," +
         "height=500," +
         "left = 450," +
         "top=130" 
        );
         popUpObj.focus();
         LoadModalDiv();


     }

There is no need of this line now var filterId = eventArgs.getDataKeyValue('RowID'); 现在不需要此行var filterId = eventArgs.getDataKeyValue('RowID'); Now you can directly use the parameter filterId in your js function. 现在,您可以在js函数中直接使用参数filterId

Because you are calling RowClick() and in your code you are calling the second parameter eventArgs and actually it's an undefined value. 因为您正在调用RowClick()并且在代码中正在调用第二个参数eventArgs ,但实际上这是一个undefined值。

Make sure you pass the correct parameters. 确保传递正确的参数。

Since you just calling a javscript function then I would recommend to just on the grid row data bound just assign the value to an anchor a tag or a button to just call the javascript. 由于您只是调用javscript函数,因此我建议仅在网格行数据上绑定,只需将值分配给锚点即可a标记或按钮来调用javascript。

The problem is you are not passing any arguments for that js function from server side but you are getting data key value in client function as in your edited question pass row id from server side and change the client side function as below, 问题是您没有从服务器端传递该js函数的任何参数,而是从客户端获得了客户端函数中的数据键值,就像在服务器端编辑的问题传递行ID一样,并如下更改了客户端函数,

function RowClick(rowId)
{ 
  // use rowId
  popUpObj = window.open("voucher.aspx?param=" + rowId + "",
}

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

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