从C#后面的代码中调用JS函数
本文关键字:调用 JS 函数 代码 | 更新日期: 2023-09-27 17:50:41
我在RowClick上有一个名为RowClick的函数,运行良好。我正试图将它移到一个按钮上,并从后面的代码中调用该函数。由于某种原因没有触发该功能。。有人知道我为什么以及如何解决这个问题吗?
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页面凭证.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函数,即在Page_Load 上
ClientScript.RegisterStartupScript(GetType(), "Javascript", "javascript:FUNCTIONNAME(); ", true);
如果你有UpdatePanel
,那么试试这个
ScriptManager.RegisterStartupScript(GetType(), "Javascript", "javascript:FUNCTIONNAME(); ", true);
因为您正在调用RowClick()
,而在代码中,您正在调用第二个参数eventArgs
,实际上它是一个undefined
值。
确保您传递了正确的参数。
由于您只是调用一个javscript函数,因此我建议只在网格行数据绑定上,只将值分配给一个锚a
标记或一个按钮来调用javascript。
问题是你没有从服务器端传递js函数的任何参数,但你在客户端函数中获得了数据键值,就像在你编辑的问题中一样,从服务器端通过行id,并将客户端函数更改如下,
function RowClick(rowId)
{
// use rowId
popUpObj = window.open("voucher.aspx?param=" + rowId + "",
}