我动态地将控件添加到我的ascx.cs用户控件中,因此我还需要从我的ascx.cs用户控件中动态执行javascript代码。
我正在使用:
执行我的javascriptPage.ClientScript.RegisterStartupScript(typeof(Page), "reOpen", "<script type='text/javascript'> reOpenClick(); </script>")
这是我在ascx文件中的javascript
<%@ Register Assembly="obout_Window_NET" Namespace="OboutInc.Window" TagPrefix="obout" %>
<obout:Window runat="server" ID="ow_dialog" Width="200" Height="50"
Title="Open" IsModal="true" ShowCloseButton="true"
StyleFolder="~/Styles/obout/window/grandgray" VisibleOnLoad="false">
.
.
.
.
</obout:Window>
<script type="text/javascript">
function reOpenClick() {
ow_dialog.setTitle("Open");
ow_dialog.screenCenter();
ow_dialog.Open();
}
</script>
像往常一样运行代码我收到错误ReferenceError: ow_dialog is not defined
。但是当在Web浏览器的控制台中输入reOpenClick()时,一切运行正常。
为什么在控制台中键入函数时我得到ReferenceError: ow_dialog is not defined
?
答案 0 :(得分:1)
将脚本标记添加到标记
<script type="text/javascript">
// place your javascript here
</script>
<script type="text/javascript" href="path to js file" />
或
在您控制的load
事件中,执行以下操作:
if (!Page.ClientScript.IsClientScriptIncludeRegistered("keyName"))
Page.ClientScript.RegisterClientScriptInclude("keyName",
Page.ResolveUrl("~/Scripts/jsfile.js"));
如果用户控件位于UpdatePanel
和/或其Visible
属性默认设置为false,则会出现"Object expected"
错误,因为您的脚本只是而不是在页面加载时加载,并且您正在调用的函数不存在。
解决方法是在主页面中使用style="display:none"
代替Visible="false"
进行用户控制。