我有asp.net 2.0 c#应用程序。 我有2个脚本要添加到用户控件的控件集合中。它不是一个接一个地添加它们,而是只打开一个脚本标记并将2个src字符串作为字符串
一起抛出string tagLinks = "/Resources/Javascript/js/taglinks.js";
HtmlGenericControl scriptTagLinks = new HtmlGenericControl("script");
scriptTagLinks.Attributes["type"] = "text/javascript";
scriptTagLinks.Attributes["src"] = tagLinks;
this.Controls.Add(scriptTagLinks);
script = new HtmlGenericControl("script");
script.Attributes.Add("type", "text/javascript");
script.Attributes.Add("src", gsJSHost);
this.Controls.Add(script);
这就是:
<script type="text/javascript">/Resources/Javascript/js/taglinks.jsvar pageTracker =
_gat._getTracker('UA-1213766-27'); pageTracker._initData();pageTracker._trackPageview();</script>
答案 0 :(得分:0)
以下是我发现的答案:
我(基本上)将您的代码添加到我的用户控件加载事件(这将注入您正在寻找的脚本标记)。我没有包含在gsJSHost
全局变量中的全局脚本,所以我用变量名替换。
这是我的控制码:
Public Partial Class MyControl
Inherits System.Web.UI.UserControl
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim tagLinks As String = "/Resources/Javascript/js/taglinks.js"
Dim scriptTagLinks As HtmlGenericControl = New HtmlGenericControl("script")
scriptTagLinks.Attributes.Add("type", "text/javascript")
scriptTagLinks.Attributes.Add("src", tagLinks)
Me.Controls.Add(scriptTagLinks)
Dim script As New HtmlGenericControl("script")
script.Attributes.Add("type", "text/javascript")
script.Attributes.Add("src", "gsJSHost")
Me.Controls.Add(script)
End Sub
End Class
这是我的结果页面来源:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title></head>
<body>
<form name="form1" method="post" action="Default.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUIMTI2NzczMzlkZE8ExRatvhvKqmX5DtpWJX8MhAcE" />
</div>
<div>
<script type="text/javascript" src="/Resources/Javascript/js/taglinks.js"></script><script type="text/javascript" src="gsJSHost"></script>
</div>
</form>
</body>
</html>
请在我的Google代码演示项目中下载源代码: http://code.google.com/p/stackoverflow-answers-by-scott/
直接链接到压缩下载:
http://stackoverflow-answers-by-scott.googlecode.com/files/1716701.zip
我希望这有帮助,