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

时间:2015-05-25 05:49:57

标签: 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 page 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>

4 个答案:

答案 0 :(得分:1)

在后面的代码上调用JavaScript函数,即On Page_Load

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

如果你有UpdatePanel,那就试试这个

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

因为您正在调用RowClick()并在您的代码中调用了第二个参数eventArgs,实际上它是undefined值。

确保传递正确的参数。

由于你只是调用一个javscript函数,所以我建议只在网格行数据绑定上将值分配给一个锚a标签或一个按钮来调用javascript。

答案 3 :(得分:0)

问题是你没有从服务器端传递该js函数的任何参数,但是你在客户端函数中获取数据键值,就像在服务器端编辑的问题传递行id一样,并改变客户端函数,如下所示,< / p>

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