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) to create my 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 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 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. Advertisements

  2. 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

    * When you think ASP, think!
    Scott Mitchell [MVP], Apr 22, 2004
    1. Advertisements

  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 ( file:
    <%@ Reference Control="../_includes/header.ascx" %> 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 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 because
    everythings' compiled up front, but (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 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 is also shockingly bad and
    quite often destroys code! 2003 is on order as I believe MS sorted a lot of these problems
    out in the new version.

    Thanks anyway Scott.

    Jon R
    Jon Ratcliffe, Apr 23, 2004
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.