Monday, March 26, 2012

Newbie: parser error when using a Literal tag

Here goes.. I'm starting with a working application and am trying to make some modifications and things aren't going well.

I wanted to add a Literal tag to a page like in this article:
http://www.odetocode.com/Code/82.aspx

But I keep getting a parser error on my page:
Parser Error Message: The base class includes the field 'stylesheet', but its type (System.Web.UI.WebControls.Literal) is not compatible with the type of control (System.Web.UI.HtmlControls.HtmlGenericControl).

Here's my code:
-- from .aspx:


<LINK href="http://links.10026.com/?link=<asp:Literal id="stylesheet" runat="Server" />" type="text/css" rel="stylesheet">

-- from .aspx.vb:


Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Data.SqlClient
Imports System.Text
Imports System.Web.Security
Imports blueinc.BusinessLogicLayer
Imports blueinc.DataAccessLayer

Namespace myCompany.Web

Public Class _default
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents dlEvents As System.Web.UI.WebControls.DataList
Protected WithEvents PlaceHolder As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents lnkRegister As System.Web.UI.WebControls.LinkButton
Protected WithEvents lblUserLoggedOn As System.Web.UI.WebControls.Label
Protected WithEvents Separator As System.Web.UI.WebControls.Label
Protected WithEvents lnkLogOff As System.Web.UI.WebControls.LinkButton
Protected WithEvents dgClinics As System.Web.UI.WebControls.DataGrid

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Dim EventsTable As BusinessLogicLayer.Events = New BusinessLogicLayer.Events
Public ClinicsTable As BusinessLogicLayer.Clinics = New BusinessLogicLayer.Clinics
Protected stylesheet As System.Web.UI.WebControls.Literal

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim uc As Control = Page.LoadControl("UserControls/Login.ascx")

If Not Request.IsAuthenticated Then
PlaceHolder.Controls.Add(uc)
lblUserLoggedOn.Visible = False
lnkLogOff.Visible = False
Else
Dim UserDetails As myCompany.BusinessLogicLayer.UserDetails
UserDetails = Session("UserDetails")

PlaceHolder.Controls.Remove(uc)
If Not IsNothing(UserDetails.UserName) Then
lblUserLoggedOn.Text = "Currently logged on as " & UserDetails.UserName
Else
lblUserLoggedOn.Text = "Authenticated but logged on as NO-ONE!"
End If
lblUserLoggedOn.Visible = True
lnkLogOff.Visible = True

End If

If Not IsPostBack Then
dlEvents.DataSource = EventsTable.GetList
dlEvents.DataBind()

dgClinics.DataSource = ClinicsTable.GetList
dgClinics.DataBind()

Dim OrgName As String

OrgName = Session("org")
If OrgName = "" Then
OrgName = Request("org")
End If
If OrgName = "" Then
OrgName = "foo"
End If

stylesheet.Text = OrgName & "/styles.css"
End If

End Sub

I would have snipped the code-behind, but I figured I'd end up snipping the offending line... I'd be happy to provide more info if required.

J<LINK href="http://links.10026.com/?link=" type="text/css" rel="stylesheet" id="stylesheet" runat="server" />

Protected WithEvents stylesheet As System.Web.UI.HtmlControls.HtmlGenericControl

' code:
stylesheet.Attributes.Add("href", "whatever value")


I think you're confusing the parser with nested tags. Try to make stylesheet a string instead of a literal and inject its value using <%= %>.
Even better, make the LINK tag runat=server (and declare it as a HtmlGenericControl in the code-behind) and set its properties using the Attributes collection.
Thank you both, its working now. I am making the link runat=server.

And when I took the literal out of the <link/> it also worked, so at least I know what the problem was!

Cheers,
J

0 comments:

Post a Comment