ASCX without @Register Broken?

R

richard.tallent

ASP.NET 2.0 has a way to centrally register user controls (ASCX) in the
web.config file, but it is poorly documented and appears to be broken.
Can anyone confirm the issue or suggest a fix?

The SDK documentation examples have bad XML case and attribute names.
The following example allows the application to start without errors:

<configuration><system.web><pages><controls>
<add tagPrefix="RST" tagName="Header" src="~/controls/header.ascx"
/>
</controls></pages></system.web></configuration>

After that, any ASPX page can create an instance of the control via
ASP.NET markup without having to have an "@Register" directive.
Example:

<RST:Header runat="server" id="MyHeader" />

The problem comes when an ASPX page does not instantiate the control by
markup, but rather by direct code:

Public Sub Page_Load()
Dim o As New ASP.Header ' ClassName="Header" in the header.ascx
End Sub

This results in the following error:

Type 'ASP.MyHeader' is not defined.

Oddly enough, if at least one instance of the control is created
through markup, any number of other instances can be created in code
using code like the example above.

Is this a bug? If not, where did I go wrong?

(Apologies to readers of mpdfa, I posted a similar message a few days
ago there, got no useful response, and then saw this group and decided
it would be a better fit.)
 
T

Teemu Keiski

Hi,

basically adding control registration via web.config does nothing more but
removes the need to declare them on every page. There's still the step to
instantiate the UC. And it would require mapping the ascx with the class
file (code-behind) which Page parser does when it sees

<RST:Header runat="server" id="MyHeader" />

on declarative syntax. If you want to instantiate UC's in code you could do

Dim c As Control=Page.LoadControl("~/controls/header.ascx")

or you could do:

Dim c As Control=Page.ParseControl("<RST:Header runat=""server""
id=""MyHeader"" />")

These are the ways in code-.behind which map UC markup to its code-behind
class. The other way around, there's not the association (what you see when
one control is created on markup , it's kind of "done already" mapping and
therefore works)
 
T

Teemu Keiski

And to add,

in case you really want to make sure Page has that type info available, use
@Reference directive.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top