.NETGURU
Dynamic User Control properties...
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngreuse' list.


Tim Royal
-- Moved from [aspngfreeforall] to [aspngreuse] by Tony Stark <Click here to reveal e-mail address> --

howdy howdy.

I've been searching through the afternoon looking for a solution to this
problem.

I have a DLL with a base page class that I inherit all pages on my web site
from... this DLL has the base class and other classes that I use within the
site (authentication, etc.).

So far, it works well. Every page derives from this base page, so I have
authentication, header and footers, etc. all built in from the start.

The problem I'm having lies in the ascx controls. For the header, for
example, I want to load the header and set properties on it. I could do this
statically in the .aspx page, like:

<Huville:Menubar id="HuVilleMenu"
Link1Src="<< Back"
Link1UrlSrc="javascript:history.back(1)"
Link2Src="<< Huville Main"
Link2UrlSrc="../main.asp"
Link3Src="* Create PPT Report"
Link3UrlSrc="/Admin/PPT_Report.asps"
runat="server"/>

And that's fine. But on some pages, these links need to be set dynamically,
not hard coded into the .aspx page. I'm having a beastly time trying to get
TO the info I need. I could load the ascx dynamically in the code behind of
the page itself, but I still had trouble getting TO the properties, even
when dynamically loading the user controls.

I don't want to use reflection because it seems to me that wouldn't be wise
from a performance perspective. Ultimately, I just want to be able to access
the user control on the page_load, set the properties, and then be on my
merry way.

I can do this:

    UserControl uc = LoadControl("Footer.ascx");

    But I can't access the property I need, and I can't typecast it to the real
class (Footer). I tried:

    uc.FreePage = true; // didn't work
    uc["FreePage"] = true; // didnt' work

    But got the error that this indexing is not valid for type "UserControl".

    In the end, I just want to @Register a user control on my page, and then
set properties on that user control in my code behind page. All the examples
I've seen come close, but don't seem to quite apply to what I've got.

Any hints, or guidance, appreciated.

Tim

Reply to this message...
 
    
Kailas Tare
I am using dynamic user control loading everywhere in my project and it
works great. Try doing this:

If you have a user control named 'myControl' with myControl.ascx and
myControl.ascx.cs files, following code should work:

myControl objMyControl =3D (myControl)LoadControl("myControl.ascx");
myControl.someProperty =3D "something"

Regards,
Kailas.

-----Original Message-----
From: Tim Royal [mailto:Click here to reveal e-mail address]
Sent: Sunday, July 14, 2002 4:55 PM
To: aspngreuse
Subject: [aspngreuse] Dynamic User Control properties...

-- Moved from [aspngfreeforall] to [aspngreuse] by Tony Stark
<Click here to reveal e-mail address> --

howdy howdy.

I've been searching through the afternoon looking for a solution to this
problem.

I have a DLL with a base page class that I inherit all pages on my web
site
from... this DLL has the base class and other classes that I use within
the
site (authentication, etc.).

So far, it works well. Every page derives from this base page, so I have
authentication, header and footers, etc. all built in from the start.

The problem I'm having lies in the ascx controls. For the header, for
example, I want to load the header and set properties on it. I could do
this
statically in the .aspx page, like:

<Huville:Menubar id=3D"HuVilleMenu"
Link1Src=3D"<< Back"
Link1UrlSrc=3D"javascript:history.back(1)"
Link2Src=3D"<< Huville Main"
Link2UrlSrc=3D"../main.asp"
Link3Src=3D"* Create PPT Report"
Link3UrlSrc=3D"/Admin/PPT_Report.asps"
runat=3D"server"/>

And that's fine. But on some pages, these links need to be set
dynamically,
not hard coded into the .aspx page. I'm having a beastly time trying to
get
TO the info I need. I could load the ascx dynamically in the code behind
of
the page itself, but I still had trouble getting TO the properties, even
when dynamically loading the user controls.

I don't want to use reflection because it seems to me that wouldn't be
wise
from a performance perspective. Ultimately, I just want to be able to
access
the user control on the page_load, set the properties, and then be on my
merry way.

I can do this:

    UserControl uc =3D LoadControl("Footer.ascx");

    But I can't access the property I need, and I can't typecast it
to the real
class (Footer). I tried:

    uc.FreePage =3D true; // didn't work
    uc["FreePage"] =3D true; // didnt' work

    But got the error that this indexing is not valid for type
"UserControl".

    In the end, I just want to @Register a user control on my page,
and then
set properties on that user control in my code behind page. All the
examples
I've seen come close, but don't seem to quite apply to what I've got.

Any hints, or guidance, appreciated.

Tim

| [aspngreuse] member Click here to reveal e-mail address =3D YOUR ID
| http://www.aspfriends.com/aspfriends/aspngreuse.asp =3D JOIN/QUIT

Reply to this message...
 
    
Tim Royal
Hmm, this syntax does not work for me. This is what I've got:

(Footer) objfooter = (Footer) LoadControl("~/Common/Footer.ascx");
objfooter.FreePage = false;

Says:
CS0246: The type or namespace name 'Footer' could not be found (are you
missing a using directive or an assembly reference?)

Here's my source for footer (footer.cs)
-----------------------------------
using System;
using System.Web;
using System.Web.UI;

public class Footer : System.Web.UI.UserControl
{
private Boolean _IsFreePage;

public Boolean FreePage
{
get
{
return this._IsFreePage;
}
set
{
this._IsFreePage = value;
}
}
}

and footer.aspx
-----------------------------------
<%@ Control Language="C#" Inherits="Footer" Src="Footer.cs" %>
<DIV CLASS="NAVOPTIONS">
    <A
HREF="javascript:document.location='<%=Application["SITE_ROOT"]%>Employee/Se
tHomePage.asp?Page=' + document.location">
            <IMG ALT="Set this page as your Start Page for the
intranet..."
             BORDER="0"
             SRC="<%=Application["SITE_ROOT"]
%>images/huville_homepage.gif"></A>
</DIV>
        <DIV NAME="DYNFOOTER" ID="DYNFOOTER"></DIV>
</BODY></HTML>

->-----Original Message-----
->From: Kailas Tare [mailto:Click here to reveal e-mail address]
->Sent: Sunday, July 14, 2002 6:51 PM
->To: aspngreuse
->Subject: [aspngreuse] RE: Dynamic User Control properties...
->
->
->I am using dynamic user control loading everywhere in my
->project and it
->works great. Try doing this:
->
->If you have a user control named 'myControl' with myControl.ascx and
->myControl.ascx.cs files, following code should work:
->
->myControl objMyControl = (myControl)LoadControl("myControl.ascx");
->myControl.someProperty = "something"
->
->
->Regards,
->Kailas.
->
->
Reply to this message...
 
    
Trevor Pinkney
Try

(Common.Footer) objfooter =
(Common.Footer)LoadControl("~/Common/Footer.ascx");

-----Original Message-----
From: Tim Royal [mailto:Click here to reveal e-mail address]
Sent: Tuesday, July 16, 2002 12:56 PM
To: aspngreuse
Subject: [aspngreuse] RE: Dynamic User Control properties...

Hmm, this syntax does not work for me. This is what I've got:

(Footer) objfooter = (Footer) LoadControl("~/Common/Footer.ascx");
objfooter.FreePage = false;

Says:
CS0246: The type or namespace name 'Footer' could not be found (are you
missing a using directive or an assembly reference?)

Here's my source for footer (footer.cs)
-----------------------------------
using System;
using System.Web;
using System.Web.UI;

public class Footer : System.Web.UI.UserControl
{
private Boolean _IsFreePage;

public Boolean FreePage
{
get
{
return this._IsFreePage;
}
set
{
this._IsFreePage = value;
}
}
}

and footer.aspx
-----------------------------------
<%@ Control Language="C#" Inherits="Footer" Src="Footer.cs" %>
<DIV CLASS="NAVOPTIONS">
    <A
HREF="javascript:document.location='<%=Application["SITE_ROOT"]%>Employee/Se
tHomePage.asp?Page=' + document.location">
            <IMG ALT="Set this page as your Start Page for the
intranet..."
             BORDER="0"
             SRC="<%=Application["SITE_ROOT"]
%>images/huville_homepage.gif"></A>
</DIV>
        <DIV NAME="DYNFOOTER" ID="DYNFOOTER"></DIV>
</BODY></HTML>

->-----Original Message-----
->From: Kailas Tare [mailto:Click here to reveal e-mail address]
->Sent: Sunday, July 14, 2002 6:51 PM
->To: aspngreuse
->Subject: [aspngreuse] RE: Dynamic User Control properties...
->
->
->I am using dynamic user control loading everywhere in my
->project and it
->works great. Try doing this:
->
->If you have a user control named 'myControl' with myControl.ascx and
->myControl.ascx.cs files, following code should work:
->
->myControl objMyControl = (myControl)LoadControl("myControl.ascx");
->myControl.someProperty = "something"
->
->
->Regards,
->Kailas.
->
->

Reply to this message...
 
    
Alex Dresko
Try...

Footer objfooter = (Footer) LoadControl("~/Common/Footer.ascx");

Alex Dresko
Three Point Oh!

-----Original Message-----
From: Tim Royal [mailto:Click here to reveal e-mail address]
Sent: Tuesday, July 16, 2002 12:56 PM
To: aspngreuse
Subject: [aspngreuse] RE: Dynamic User Control properties...

Hmm, this syntax does not work for me. This is what I've got:

(Footer) objfooter = (Footer)
LoadControl("~/Common/Footer.ascx");
objfooter.FreePage = false;

Says:
CS0246: The type or namespace name 'Footer' could not be found (are you
missing a using directive or an assembly reference?)

Here's my source for footer (footer.cs)
-----------------------------------
using System;
using System.Web;
using System.Web.UI;

public class Footer : System.Web.UI.UserControl
{
private Boolean _IsFreePage;

public Boolean FreePage
{
get
{
return this._IsFreePage;
}
set
{
this._IsFreePage = value;
}
}
}

and footer.aspx
-----------------------------------
<%@ Control Language="C#" Inherits="Footer" Src="Footer.cs" %>
<DIV CLASS="NAVOPTIONS">
    <A
HREF="javascript:document.location='<%=Application["SITE_ROOT"]%>Employe
e/SetHomePage.asp?Page=' + document.location">
            <IMG ALT="Set this page as your Start Page for
the intranet..."
             BORDER="0"
             SRC="<%=Application["SITE_ROOT"]
%>images/huville_homepage.gif"></A>
</DIV>
        <DIV NAME="DYNFOOTER" ID="DYNFOOTER"></DIV>
</BODY></HTML>

->-----Original Message-----
->From: Kailas Tare [mailto:Click here to reveal e-mail address]
->Sent: Sunday, July 14, 2002 6:51 PM
->To: aspngreuse
->Subject: [aspngreuse] RE: Dynamic User Control properties...
->
->
->I am using dynamic user control loading everywhere in my
->project and it
->works great. Try doing this:
->
->If you have a user control named 'myControl' with myControl.ascx and
->myControl.ascx.cs files, following code should work:
->
->myControl objMyControl = (myControl)LoadControl("myControl.ascx");
->myControl.someProperty = "something"
->
->
->Regards,
->Kailas.
->
->

Reply to this message...
 
    
Tim Royal
Thanks, I tried it. Gave me the same message (Could not find Common. are you
sure you're using the namespace, bla bla bla...)

It's quite weird. I've looked all morning at examples on the best .net
sites. I've tried different ways over and over to get it so the code behind
page of my aspx could load an .ascx file and reference it.

* tried the (Footer_ascx) footer syntax
* tried using some @reference directive in the page
* tried wrapping it in a unique namespace in case it was getting confused
not having one
* added ClassName="Footer" to the .ascx file itself to avoid any ambiguities

I tried many things to simply be able to load a control from an .ascx file
in the same web app and cast it as the type (not just Control or
UserControl).

Not sure what to try next. The things I tried above seem to be working for
folks, so it must be something I'm doing wrong.

Thanks for the suggestion.

Tim

->-----Original Message-----
->From: Trevor Pinkney [mailto:Click here to reveal e-mail address]
->Sent: Tuesday, July 16, 2002 11:48 AM
->To: aspngreuse
->Subject: [aspngreuse] RE: Dynamic User Control properties...
->
->
->Try
->
->(Common.Footer) objfooter =
->(Common.Footer)LoadControl("~/Common/Footer.ascx");
->
->-----Original Message-----
->From: Tim Royal [mailto:Click here to reveal e-mail address]
->Sent: Tuesday, July 16, 2002 12:56 PM
->To: aspngreuse
->Subject: [aspngreuse] RE: Dynamic User Control properties...
->
->Hmm, this syntax does not work for me. This is what I've got:
->
-> (Footer) objfooter = (Footer)
->LoadControl("~/Common/Footer.ascx");
-> objfooter.FreePage = false;
->
->Says:
->CS0246: The type or namespace name 'Footer' could not be
->found (are you
->missing a using directive or an assembly reference?)
->
->Here's my source for footer (footer.cs)
->-----------------------------------
->using System;
->using System.Web;
->using System.Web.UI;
->
->public class Footer : System.Web.UI.UserControl
->{
-> private Boolean _IsFreePage;
->
-> public Boolean FreePage
-> {
-> get
-> {
-> return this._IsFreePage;
-> }
-> set
-> {
-> this._IsFreePage = value;
-> }
-> }
->}
->
->and footer.aspx
->-----------------------------------
-><%@ Control Language="C#" Inherits="Footer" Src="Footer.cs" %>
-><DIV CLASS="NAVOPTIONS">
->    <A
->HREF="javascript:document.location='<%=Application["SITE_ROOT"
->]%>Employee/Se
->tHomePage.asp?Page=' + document.location">
->            <IMG ALT="Set this page as your Start
->Page for the
->intranet..."
->             BORDER="0"
->             SRC="<%=Application["SITE_ROOT"]
->%>images/huville_homepage.gif"></A>
-></DIV>
->        <DIV NAME="DYNFOOTER" ID="DYNFOOTER"></DIV>
-></BODY></HTML>
->
->
->->-----Original Message-----
->->From: Kailas Tare [mailto:Click here to reveal e-mail address]
->->Sent: Sunday, July 14, 2002 6:51 PM
->->To: aspngreuse
->->Subject: [aspngreuse] RE: Dynamic User Control properties...
->->
->->
->->I am using dynamic user control loading everywhere in my
->->project and it
->->works great. Try doing this:
->->
->->If you have a user control named 'myControl' with myControl.ascx and
->->myControl.ascx.cs files, following code should work:
->->
->->myControl objMyControl = (myControl)LoadControl("myControl.ascx");
->->myControl.someProperty = "something"
->->
->->
->->Regards,
->->Kailas.
->->
->->
->
->| [aspngreuse] member Click here to reveal e-mail address = YOUR ID
->| http://www.aspfriends.com/aspfriends/aspngreuse.asp = JOIN/QUIT
->
->

Reply to this message...
 
    
Trevor Pinkney

Hmmm... Try it w/o the ~, and w/o the brackets around the first
Common.Footer (I didn't mean to put them there).

Common.Footer objfooter =
(Common.Footer)Page.LoadControl("/Common/Footer.ascx");

The error you are getting means it can't find the object. So... when you
type Common. Your intellisence should pop up and say Footer. Remember that
everything is case sensitive too.

-Trev

-----Original Message-----
From: Tim Royal [mailto:Click here to reveal e-mail address]
Sent: Tuesday, July 16, 2002 3:17 PM
To: aspngreuse
Subject: [aspngreuse] RE: Dynamic User Control properties...

Thanks, I tried it. Gave me the same message (Could not find Common. are you
sure you're using the namespace, bla bla bla...)

It's quite weird. I've looked all morning at examples on the best .net
sites. I've tried different ways over and over to get it so the code behind
page of my aspx could load an .ascx file and reference it.

* tried the (Footer_ascx) footer syntax
* tried using some @reference directive in the page
* tried wrapping it in a unique namespace in case it was getting confused
not having one
* added ClassName="Footer" to the .ascx file itself to avoid any ambiguities

I tried many things to simply be able to load a control from an .ascx file
in the same web app and cast it as the type (not just Control or
UserControl).

Not sure what to try next. The things I tried above seem to be working for
folks, so it must be something I'm doing wrong.

Thanks for the suggestion.

Tim

->-----Original Message-----
->From: Trevor Pinkney [mailto:Click here to reveal e-mail address]
->Sent: Tuesday, July 16, 2002 11:48 AM
->To: aspngreuse
->Subject: [aspngreuse] RE: Dynamic User Control properties...
->
->
->Try
->
->(Common.Footer) objfooter =
->(Common.Footer)LoadControl("~/Common/Footer.ascx");
->
->-----Original Message-----
->From: Tim Royal [mailto:Click here to reveal e-mail address]
->Sent: Tuesday, July 16, 2002 12:56 PM
->To: aspngreuse
->Subject: [aspngreuse] RE: Dynamic User Control properties...
->
->Hmm, this syntax does not work for me. This is what I've got:
->
-> (Footer) objfooter = (Footer)
->LoadControl("~/Common/Footer.ascx");
-> objfooter.FreePage = false;
->
->Says:
->CS0246: The type or namespace name 'Footer' could not be
->found (are you
->missing a using directive or an assembly reference?)
->
->Here's my source for footer (footer.cs)
->-----------------------------------
->using System;
->using System.Web;
->using System.Web.UI;
->
->public class Footer : System.Web.UI.UserControl
->{
-> private Boolean _IsFreePage;
->
-> public Boolean FreePage
-> {
-> get
-> {
-> return this._IsFreePage;
-> }
-> set
-> {
-> this._IsFreePage = value;
-> }
-> }
->}
->
->and footer.aspx
->-----------------------------------
-><%@ Control Language="C#" Inherits="Footer" Src="Footer.cs" %>
-><DIV CLASS="NAVOPTIONS">
->    <A
->HREF="javascript:document.location='<%=Application["SITE_ROOT"
->]%>Employee/Se
->tHomePage.asp?Page=' + document.location">
->            <IMG ALT="Set this page as your Start
->Page for the
->intranet..."
->             BORDER="0"
->             SRC="<%=Application["SITE_ROOT"]
->%>images/huville_homepage.gif"></A>
-></DIV>
->        <DIV NAME="DYNFOOTER" ID="DYNFOOTER"></DIV>
-></BODY></HTML>
->
->
->->-----Original Message-----
->->From: Kailas Tare [mailto:Click here to reveal e-mail address]
->->Sent: Sunday, July 14, 2002 6:51 PM
->->To: aspngreuse
->->Subject: [aspngreuse] RE: Dynamic User Control properties...
->->
->->
->->I am using dynamic user control loading everywhere in my
->->project and it
->->works great. Try doing this:
->->
->->If you have a user control named 'myControl' with myControl.ascx and
->->myControl.ascx.cs files, following code should work:
->->
->->myControl objMyControl = (myControl)LoadControl("myControl.ascx");
->->myControl.someProperty = "something"
->->
->->
->->Regards,
->->Kailas.
->->
->->
->
->| [aspngreuse] member Click here to reveal e-mail address = YOUR ID
->| http://www.aspfriends.com/aspfriends/aspngreuse.asp = JOIN/QUIT
->
->

| [aspngreuse] member Click here to reveal e-mail address = YOUR ID
| http://www.aspfriends.com/aspfriends/aspngreuse.asp = JOIN/QUIT

Reply to this message...
 
 
System.Web.UI.Page
System.Web.UI.UserControl
System.Windows.Forms.UserControl




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification