为什么当我的用户控件呈现时 Javascript 无法正常运行?
我使用 HTMLTextWriter 和 StringWriter 的组合来呈现我的用户控件。用于呈现用户控件的代码是通过我页面上某些 Javascript 的 ajax 调用启动的。生成的 StringWriter 文本将返回到 javascript 代码并附加到我的页面。渲染代码如下所示:
Dim sw As New System.IO.StringWriter
If Not IsDBNull(reader.Item("UserControlName")) Then
'Use Reflection to create the appropriate class.
'First get the type
'The usercontrol name is retrieved from my datatable reader
Dim htmlcontroltype = Type.GetType("MyProject." & reader.Item("UserControlName"))
If Not IsNothing(htmlcontroltype) Then
'Create an instance of the class
Dim htmlcontrol = Activator.CreateInstance(htmlcontroltype)
htmlcontrol = htmlcontrol.LoadControl("~/" & reader.Item("UserControlName") & ".ascx")
htmlcontrol.DataBind()
Dim tw As New HtmlTextWriter(sw)
htmlcontrol.RenderControl(tw)
Else
sw.Write("No matching User Control was found in the project.")
End If
End If
Return sw.ToString
这将返回我的用户控件,该控件似乎工作正常,直到我尝试使用 Javascript 做任何好的事情。
例如,如果我想要一个谷歌图表,我会粘贴 他们的Javascript到我的用户控件中。就像这样:
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="TestControl.ascx.vb" Inherits="MyProject.TestControl" %>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
[ -- snip! -- ]
var chart = new google.visualization.LineChart(document.getElementById('visualization1'));
chart.draw(data, { width: 300, height: 250, title: 'Company Performance' });
}
</script>
<div id="visualization1" style="width: 300px; height: 300px;"></div>
但是!当我运行该程序时,它会出错,声称“谷歌未定义”。
值得注意的是,这并不限于 Google Charts - 我在 Twitter 搜索 API 和其他 API 中也遇到过类似的问题。另外,放置线
在我的默认或母版页中没有区别。
最后,如果在设计时使用通常的 @Register 指令将用户控件添加到我的母版页中,则此代码将不会出现任何问题 - 让我相信问题是由 StringWriter 引起的。有没有人遇到过类似的问题或者可以阐明这个主题?
I am using a combination of HTMLTextWriter and StringWriter to render my user controls. The code used to render the user control is initiated through an ajax call from some Javascript on my page. The resulting StringWriter text is returned to the javascript code and is appended to my page. The rendering code looks like this:
Dim sw As New System.IO.StringWriter
If Not IsDBNull(reader.Item("UserControlName")) Then
'Use Reflection to create the appropriate class.
'First get the type
'The usercontrol name is retrieved from my datatable reader
Dim htmlcontroltype = Type.GetType("MyProject." & reader.Item("UserControlName"))
If Not IsNothing(htmlcontroltype) Then
'Create an instance of the class
Dim htmlcontrol = Activator.CreateInstance(htmlcontroltype)
htmlcontrol = htmlcontrol.LoadControl("~/" & reader.Item("UserControlName") & ".ascx")
htmlcontrol.DataBind()
Dim tw As New HtmlTextWriter(sw)
htmlcontrol.RenderControl(tw)
Else
sw.Write("No matching User Control was found in the project.")
End If
End If
Return sw.ToString
This will return my user control which seems to work fine, until I try to do anything nice with Javascript.
If, for instance, I want to have a google chart, I paste in their Javascript into my user control. Like so:
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="TestControl.ascx.vb" Inherits="MyProject.TestControl" %>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
[ -- snip! -- ]
var chart = new google.visualization.LineChart(document.getElementById('visualization1'));
chart.draw(data, { width: 300, height: 250, title: 'Company Performance' });
}
</script>
<div id="visualization1" style="width: 300px; height: 300px;"></div>
But! When I run the program it will error, claiming that "google is undefined".
It is important to note that this isn't restricted to Google Charts - I have had similar problems with the Twitter Search API and others. Also, placing the line<script src="http://www.google.com/jsapi" type="text/javascript"></script>
in my default or master page makes no difference.
Finally, this code will work with no problems if the user control is added to my master page at design time using the usual @Register directive - leading me to believe the problem is caused by the StringWriter. Has anyone experienced a similar problem or can shed some light on the subject?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题不在于 google.setOnLoadCallback 等,我认为您存在范围界定问题。试试这个:
这将显式地将drawChart函数的上下文设置为可以访问google变量的东西。
I don't think that the issue is with google.setOnLoadCallback, etc, I think you have a scoping issue there. Try this:
That will explicitly set the context of the drawChart function to something which has access to the google variable.
我找到了解决该问题的方法,尽管我仍然不知道是什么原因造成的。我将 Google 图表代码放入主页并将其包装到 jQuery 函数中,在加载用户控件时调用它。
这又导致了另一个问题,即 iFrame 在 Firefox 和 Chrome 中返回为空(但在 IE 中却没有,奇怪的是)。此问题已在此处得到解决。
长话短说,它比我想要的更混乱,但最终结果是一样的。
I found a work around for the issue, although I still don't know what's causing it. I placed my Google chart code into my main page and wrapped it into a jQuery function, calling it when the user control was loaded.
This then led to a further problem where the iFrame came back empty in Firefox and Chrome (but not in IE, bizarrely). This problem was addressed here.
Long story short, it's messier than I would have liked, but the end result is the same.