Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > Accessing User Control properties from C# Code Behind...

Reply
Thread Tools

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

 
 
Jon Ratcliffe
Guest
Posts: n/a
 
      04-20-2004
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 asplaceHolder, 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
 
Reply With Quote
 
 
 
 
Scott Mitchell [MVP]
Guest
Posts: n/a
 
      04-22-2004
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.velocityreviews.com/forums/(E-Mail Removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 
Reply With Quote
 
 
 
 
Jon Ratcliffe
Guest
Posts: n/a
 
      04-23-2004
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]" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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
> (E-Mail Removed)
> http://www.4GuysFromRolla.com
> http://www.ASPFAQs.com
> http://www.ASPMessageboard.com
>
> * When you think ASP, think 4GuysFromRolla.com!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Designer Support: How to have user control's properties appear in IDE's properties window? Max2006 ASP .Net 2 07-13-2007 01:45 PM
Accessing USER CONTROL which is inside Masterpagethrough Another USER Control inside normal page. Kiran More ASP .Net Web Controls 2 11-14-2006 12:58 PM
Accessing Objects properties in User Control tshad ASP .Net 0 08-03-2006 06:24 PM
Accessing Datagrid properties within a user control from the outside Craig G ASP .Net 3 11-30-2004 02:51 PM
accessing the readonly properties of a listmenu with a user control? Moe Sizlak ASP .Net 4 07-17-2004 06:01 PM



Advertisments