当前位置: 动力学知识库 > 问答 > 编程问答 >

vb.net - Server Control Error: Unknown server tag with ASP.NET

问题描述:

I am trying to get a custom control working in VB ASP.Net

I've read a few threads but it doesn't seem to help me much.

I have a solution including:

  • Website project called "SiteCI"
  • Class Library project called "SiteCI-classes"

The website project has a reference to the library project, and the library DLL are correctly generated inside the bin folder of the website project.

When I create a random class within the website project, I can access to my test control class called MailLink by typing:

SiteCI_classes.Controls.MailLink

It works perfectly, Visual Studio suggests it to me, so the link between the two projects are correctly set.

Please note that I clean/rebuild the project every time to see if it really works or not, so that's not a problem of incorrect DLL build.

In the Web Config file inside system.web -> pages -> controls I have added:

<add assembly="SiteCI-classes" namespace="Controls" tagPrefix="custom" />

But on a ASP.Net page when I add:

<custom:MailLink runat="server" id="test1"></custom:MailLink>

I am getting the Unknown server tag 'custom:MailLink' error.

To check if I did not make a mistake while typing in the web config file I tried to change the SiteCI-classes to SiteCI_classes and I got an error saying that it wasn't able to load the assembly, so I guess that part is OK, and then I tried to change namespace="Controls" in *namespace="SiteCI_classes.Controls"* or namespace="SiteCI-classes.Controls" and it didn't fix the unknown server tag issue.

I don't know what to do... it's supposed to work, isn't it?

Here is the MailLink.vb that is located in a folder called Controls of the Library project. It's a class I picked on a Microsoft page just for testing purpose.

I would be very grateful if anyone could give me a tip on what to check.

Namespace Controls

< _

AspNetHostingPermission(SecurityAction.Demand, _

Level:=AspNetHostingPermissionLevel.Minimal), _

AspNetHostingPermission(SecurityAction.InheritanceDemand, _

Level:=AspNetHostingPermissionLevel.Minimal), _

DefaultProperty("Email"), _

ParseChildren(True, "Text"), _

ToolboxData("<{0}:MailLink runat=""server""> </{0}:MailLink>") _

> _

Public Class MailLink

Inherits WebControl

< _

Bindable(True), _

Category("Appearance"), _

DefaultValue(""), _

Description("The e-mail address.") _

> _

Public Overridable Property Email() As String

Get

Dim s As String = CStr(ViewState("Email"))

If s Is Nothing Then s = String.Empty

Return s

End Get

Set(ByVal value As String)

ViewState("Email") = value

End Set

End Property

< _

Bindable(True), _

Category("Appearance"), _

DefaultValue(""), _

Description("The text to display on the link."), _

Localizable(True), _

PersistenceMode(PersistenceMode.InnerDefaultProperty) _

> _

Public Overridable Property Text() As String

Get

Dim s As String = CStr(ViewState("Text"))

If s Is Nothing Then s = String.Empty

Return s

End Get

Set(ByVal value As String)

ViewState("Text") = value

End Set

End Property

Protected Overrides ReadOnly Property TagKey() _

As HtmlTextWriterTag

Get

Return HtmlTextWriterTag.A

End Get

End Property

Protected Overrides Sub AddAttributesToRender( _

ByVal writer As HtmlTextWriter)

MyBase.AddAttributesToRender(writer)

writer.AddAttribute(HtmlTextWriterAttribute.Href, _

"mailto:" & Email)

End Sub

Protected Overrides Sub RenderContents( _

ByVal writer As HtmlTextWriter)

If (Text = String.Empty) Then

Text = Email

End If

writer.WriteEncodedText(Text)

End Sub

End Class

End Namespace

网友答案:

I found the problem which seem to have ONLY been caused by the dash ("-") in the name of the Project.

The name of the project is still "SiteCI-Classes" BUT the final code of the WebConfig is different:

<add assembly="SiteCIClasses" namespace="SiteCI2KClasses.Controls" tagPrefix="cc1" />

Note the difference between the two. ASP.Net 3.5 will not recognize a dash in the name of a project and seem to remove it: if you try to set it to SiteCI-Classes it will end up with an error like assembly not found.

分享给朋友:
您可能感兴趣的文章:
随机阅读: