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

J

Jon Ratcliffe

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
 
S

Scott Mitchell [MVP]

Jon said:
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
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 
J

Jon Ratcliffe

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
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top