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

标签 javascript c# asp.net function

我有一个名为 RowClick 的 onRowClick 函数,并且工作正常。我试图将其移至按钮并从后面的代码调用该函数。由于某种原因没有触发该功能。任何人都知道为什么以及如何解决这个问题?

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

  <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页面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>

最佳答案

像这样改变你的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();


     }

现在不需要这行var filterId = eventArgs.getDataKeyValue('RowID'); 现在您可以直接在js函数中使用参数filterId

关于javascript - 从 C# 背后的代码调用 JS 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30431959/

相关文章:

javascript文档onmousedown取消元素onclick

php - Fadeshow 不显示在页面上 (Javascript/CSS/PHP)

c# - 计时器在跨线程中不起作用

c# - 在 C# 中使用正则表达式对列表元素进行编号

asp.net - 动态添加的链接按钮在单击按钮时消失,留下空列

javascript - 如何从 asp net C# 中单击按钮后的代码调用 javascript 函数?

javascript - Webpack:通过文件加载器复制 JSON 文件

javascript - Node.js 中是否可以嵌套 Redis 语句?

c# - WPF 4 中仍然存在的内存泄漏

c# - 从服务中检索 Json 并传递给 ApiController