【问题标题】:User Control Public Shared Variables in ASP.NET 1.1 Not Working As ExpectedASP.NET 1.1 中的用户控制公共共享变量未按预期工作
【发布时间】:2008-09-29 13:50:48
【问题描述】:

假设我有一个包含一些用户控件的 Web 表单。我的“主要”Web 表单的标题标签是在其中一个用户控件中生成的。目前将这些数据传递给 Web 表单是这样完成的。

Public Sub SetPageValues(ByVal sTitle As String, ByVal sKeywords As String, ByVal sDesc As String)
    MySystem.Web.UI.Main.PageSettings(sKeywords, sDesc, sTitle)
End Sub

Main 是 Web 表单的名称。这是在 Main 中设置该值的 sub。

 Public Shared Sub PageSettings(ByVal strKeywords As String, ByVal strDesc As String, ByVal strTitle As String)
    Dim _lblTitle As System.Web.UI.webcontrols.Literal = lblTitle
    Dim _lblMetaDesc As System.Web.UI.webControls.Literal = lblMetaDesc
    Dim _lblMetaKeywords As System.Web.UI.WebControls.Literal = lblMetaKeywords
    Dim _lblMetatitle As System.Web.UI.WebControls.Literal = lblMetaTitle
    _lblTitle.Text = strTitle
    _lblMetaDesc.Text = "<meta name=""description"" content=""" + strDesc + """>"
    _lblMetaKeywords.Text = "<meta name=""keywords"" content=""" + strKeywords + """>"
    _lblMetatitle.Text = "<meta name=""title"" content=""" + strTitle + """>"
End Sub

在所有这些之后,我们运行池化内存并每 400 分钟回收一次,但是,页面标题会损坏并无法正确显示。除了迁移到新版本 .net 之外,还有其他人有什么想法吗?

通过在用户控件中创建属性,现在可以正确传递值。

【问题讨论】:

    标签: asp.net controls


    【解决方案1】:

    就个人而言,这就是我会做的。 首先 - 将 TITLE 更改为 HTML.GenericControl 在 ASPX 端,它看起来像这样:

    <title runat="server" id="title" />
    

    然后,我会将 META 标记修改为 html 通用控件

    <meta name="description" content="description" id="description" runat="server" />
    <meta name="keywords" content="keys" id="keywords" runat="server" /> 
    

    此时,您可以像这样修改值:

    title.InnerText = "This Title"
    keywords.Attributes("content") = "key,word"
    description.Attributes("content") = "A demonstration of Setting title and meta tags" 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-25
      • 1970-01-01
      • 2022-08-18
      • 2017-11-30
      • 1970-01-01
      • 2017-05-24
      相关资源
      最近更新 更多