我有一个包含在 UpdatePanel 内的 asp.net 按钮,当单击它时,它会执行异步回发并注册一些显示 jquery 对话框的 javascript。
protected void btnAddData(object sender, EventArgs e) {
StringBuilder jqueryDialog = new StringBuilder();
jqueryDialog.Append("$(document).ready(function() {");
jqueryDialog.Append("RefreshData();");
jqueryDialog.Append("$('#divData').dialog({ modal: false, draggable: true, title: 'Historical Data', width: 700 });");
jqueryDialog.Append("});");
ScriptManager sm = ScriptManager.GetCurrent(this);
if (sm != null && sm.IsInAsyncPostBack) {
ScriptManager.RegisterClientScriptBlock(
this, typeof(Page), Guid.NewGuid().ToString(),
jqueryDialog.ToString(), true);
}
}
如您所见,javascript 首先调用名为 RefreshData() 的函数,该函数作为 javascript 存在于我的标记中。
<script type="text/javascript" language="javascript">
if ($) {
$(document).ready(function () {
function RefreshData() {
alert("Data Refreshed!");
}
});
}
</script>
但是,Firefox 会给出错误,指出RefreshData 未定义。这是否意味着我在异步回发上注册的任何 JavaScript 将无法使用 我在标记中定义了 javascript 函数吗?
感谢您的帮助。
最佳答案
不要在 document.ready 函数中定义 RefreshData
函数,该函数是一个匿名回调。定义在外部,以便外部可以访问:
<script type="text/javascript">
if (typeof($) != 'undefined') {
function RefreshData() {
alert("Data Refreshed!");
}
}
</script>
此外,您可能不需要包装在文档中。准备好您的服务器端包括:
StringBuilder jqueryDialog = new StringBuilder();
jqueryDialog.Append("RefreshData();");
jqueryDialog.Append("$('#divData').dialog({ modal: false, draggable: true, title: 'Historical Data', width: 700 });");
关于javascript - 在异步回发上注册 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10001164/