添加 2 个带有 src 属性的 HTMLGenericControl 脚本不起作用

发布于 2024-08-11 01:23:33 字数 805 浏览 6 评论 0原文

我有 asp.net 2.0 c# 应用程序。 我有 2 个脚本想要添加到用户控件的控件集合中。它不是一个接一个地添加它们,而是只打开一个 script 标签并将 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>

I have asp.net 2.0 c# application.
I have 2 scripts I want to add to a user control's control collection. Instead of adding them one after another, it only opens one script tag and throws the 2 src strings together as strings

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);

This is what happens:

<script type="text/javascript">/Resources/Javascript/js/taglinks.jsvar pageTracker =
_gat._getTracker('UA-1213766-27'); pageTracker._initData();pageTracker._trackPageview();</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是你 2024-08-18 01:23:33

以下是我对您的答案的发现:

我(基本上)将您的代码添加到我的用户控件加载事件中(这将注入您正在寻找的脚本标签)。我没有 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

我希望这有帮助,

谢谢!

Here is what I've found out about your answer:

I added (basically) your code to my user control loaded event (which will inject the script tags that you're looking for). I don't have the global script that is contained in your gsJSHost global variable so I substituted with the variable name.

This is my control code:

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

This is my resulting page source:

<!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>

Please download the source code at my Google code demo project:
http://code.google.com/p/stackoverflow-answers-by-scott/

Direct link to zipped download:
http://stackoverflow-answers-by-scott.googlecode.com/files/1716701.zip

I hope this helps,

Thanks!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文