Accessing User Control properties from C# Code Behind...

Discussion in 'ASP .Net Building Controls' started by Jon Ratcliffe, Apr 20, 2004.

  1. I am not using (can't use) vs.net to create my asp.net pages - I am
    using dreamweaver MX (essentially using it as a glorified notepad).

    I am using C#.

    Pages involved:
    header.ascx - an in page (ie not a code behind) user control
    default.aspx - an asp.net page
    default.aspx.cs - a code behind page for the aspx page

    I am loading the user control dynamically into the code behind file
    (default.aspx.cs) as follows:

    ---
    control = LoadControl("../_includes/header.ascx");
    phPageHeader.Controls.Add(control);
    ---

    phPageHeader is a simple asp:placeHolder, this all works fine and my
    control is rendered to the page as expected.

    I then try to set a property inside the User Control, first casting
    the control object to its correct type...

    ---
    PageHeader header = (PageHeader)control;
    header.strCrumbtrail = "Hello World!";
    ---

    Running the code then produces an error (CS0246: The type or namespace
    name 'PageHeader' could not be found (are you missing a using
    directive or an assembly reference?))

    Now, I kind of know why this is happening. My Code Behind page has no
    knowledge of what a PageHeader is - it knows of no such class type.

    In any other instance this would be a simple case of putting a "using"
    statement at the top of my code behind page. In this case I cannot do
    that because my User Control is not compiled into any namespace (its
    compiled at runtime in the same way as my aspx pages).

    What do I do to get my code behind page to recognise my User Control
    after I've loaded it?

    Does it involve compiling the control (using command line tools) every
    time I modify it? Something that seems crazy for what could be done in
    seconds whith include files in trad ASP!

    Or is there a way of dynamically using this class type in my code
    behind page.

    After hours of searching, I've seen quite a few postings along these
    lines but absolutely no solutions!

    Any help would be greatly appreciated.


    Jon R
    Jon Ratcliffe, Apr 20, 2004
    #1
    1. Advertising

  2. Jon Ratcliffe wrote:
    > I am not using (can't use) vs.net to create my asp.net pages - I am
    > using dreamweaver MX (essentially using it as a glorified notepad).
    >
    > Pages involved:
    > header.ascx - an in page (ie not a code behind) user control
    > default.aspx - an asp.net page
    > default.aspx.cs - a code behind page for the aspx page
    >
    > ---
    > PageHeader header = (PageHeader)control;
    > header.strCrumbtrail = "Hello World!";
    > ---
    >
    > Running the code then produces an error (CS0246: The type or namespace
    > name 'PageHeader' could not be found (are you missing a using
    > directive or an assembly reference?))


    Hi Jon. What you need to do is the following. In your User Control,
    you have a <%@ Control %> directive, no? Make sure you have the
    ClassName attribute in there, like:

    <%@ Control ... ClassName="PageHeader" %>
    ....


    Then, in your ASP.NET Web page that uses the User Control, add a
    @Reference directive like so:

    <%@ Reference Control="header.ascx" %> (or whatever the path to the
    user control is)

    Then you should be able to do:

    ---
    PageHeader header = (PageHeader)control;
    header.strCrumbtrail = "Hello World!";
    ---

    Hope this does the trick for you! :)


    --

    Scott Mitchell

    http://www.4GuysFromRolla.com
    http://www.ASPFAQs.com
    http://www.ASPMessageboard.com

    * When you think ASP, think 4GuysFromRolla.com!
    Scott Mitchell [MVP], Apr 22, 2004
    #2
    1. Advertising

  3. Thanks for the tips Scott,

    Still not working though :-(

    I have this line in my header.ascx (user control) file:
    <%@ Control Language="C#" ClassName="PageHeader"%>

    And this line in my default.aspx (asp.net) file:
    <%@ Reference Control="../_includes/header.ascx" %>

    ....as per your instructions but still no joy. The application errors
    at the same point in the aspx.cs (code behind) file (the point at
    which I try to cast the Control to type PageHeader). My code behind
    page still has no awareness of what a PageHeader object/class is!!

    Its starting to look like I may have to use vs.net for my coding and
    Dreamweaver for my HTML - a popular solution amongst people in these
    groups it would seem.

    I have no errors when I do user controls in vs.net because
    everythings' compiled up front, but vs.net (or at least the first
    release which we have) doesn't fit very well with our work flow and
    server setup (as I mentioned briefly in my original post). Our
    Workstations, Web Servers and File Servers are all seperated and the
    original version of vs.net only seems to work if you are your entire
    setup is on one machine and your site is hosted in the default
    (C:\inetpub\www_root) folder. Anything more complex seems to baffle it
    into submission!

    The HTML designer in the original vs.net is also shockingly bad and
    quite often destroys code!

    vs.net 2003 is on order as I believe MS sorted a lot of these problems
    out in the new version.

    Thanks anyway Scott.


    Jon R

    "Scott Mitchell [MVP]" <> wrote in message news:<>...
    > Jon Ratcliffe wrote:
    > > I am not using (can't use) vs.net to create my asp.net pages - I am
    > > using dreamweaver MX (essentially using it as a glorified notepad).
    > >
    > > Pages involved:
    > > header.ascx - an in page (ie not a code behind) user control
    > > default.aspx - an asp.net page
    > > default.aspx.cs - a code behind page for the aspx page
    > >
    > > ---
    > > PageHeader header = (PageHeader)control;
    > > header.strCrumbtrail = "Hello World!";
    > > ---
    > >
    > > Running the code then produces an error (CS0246: The type or namespace
    > > name 'PageHeader' could not be found (are you missing a using
    > > directive or an assembly reference?))

    >
    > Hi Jon. What you need to do is the following. In your User Control,
    > you have a <%@ Control %> directive, no? Make sure you have the
    > ClassName attribute in there, like:
    >
    > <%@ Control ... ClassName="PageHeader" %>
    > ...
    >
    >
    > Then, in your ASP.NET Web page that uses the User Control, add a
    > @Reference directive like so:
    >
    > <%@ Reference Control="header.ascx" %> (or whatever the path to the
    > user control is)
    >
    > Then you should be able to do:
    >
    > ---
    > PageHeader header = (PageHeader)control;
    > header.strCrumbtrail = "Hello World!";
    > ---
    >
    > Hope this does the trick for you! :)
    >
    >
    > --
    >
    > Scott Mitchell
    >
    > http://www.4GuysFromRolla.com
    > http://www.ASPFAQs.com
    > http://www.ASPMessageboard.com
    >
    > * When you think ASP, think 4GuysFromRolla.com!
    Jon Ratcliffe, Apr 23, 2004
    #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. adiel
    Replies:
    2
    Views:
    1,023
    adiel
    Jul 2, 2003
  2. Gerald
    Replies:
    1
    Views:
    571
    Chris R. Timmons
    Jul 4, 2003
  3. Luminousc
    Replies:
    4
    Views:
    548
  4. TB
    Replies:
    3
    Views:
    2,097
    =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=
    Jul 12, 2007
  5. Michael Morse

    Accessing Web User Control from code behind

    Michael Morse, Jan 23, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    111
    Michael Morse
    Jan 23, 2004
Loading...

Share This Page