ASCX without @Register Broken?

Discussion in 'ASP .Net Building Controls' started by richard.tallent@gmail.com, Jan 31, 2006.

  1. Guest

    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.)
    , Jan 31, 2006
    #1
    1. Advertising

  2. Teemu Keiski Guest

    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)

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke


    <> wrote in message
    news:...
    > 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.)
    >
    Teemu Keiski, Feb 6, 2006
    #2
    1. Advertising

  3. Teemu Keiski Guest

    And to add,

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

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke

    <> wrote in message
    news:...
    > 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.)
    >
    Teemu Keiski, Feb 10, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Quentin
    Replies:
    1
    Views:
    468
    Quentin
    Jul 29, 2003
  2. Jason Shohet
    Replies:
    1
    Views:
    462
    Kevin Spencer
    Nov 10, 2003
  3. Holger (David) Wagner
    Replies:
    2
    Views:
    1,915
    Holger (David) Wagner
    Jul 3, 2004
  4. Joe
    Replies:
    1
    Views:
    352
    Jeffrey Tan[MSFT]
    Feb 16, 2004
  5. Replies:
    2
    Views:
    520
Loading...

Share This Page