User Controls: Accessing Properties (a slightly different point of view) 
Author Message
 User Controls: Accessing Properties (a slightly different point of view)

Several people have asked in this Newsgroup what the catch is with
accessing User Controls in code, to set properties or retrieve values.
Responders have suggested the following type of code in the
code-behind page:

Control c = LoadControl("MyControl.ascx");
MyControl myControl = (MyControl)c;
myControl.myProperty = "Test";

Well I tried and tried to get that to work but. I kept getting this
error:

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

I don't normally work in Visual Studio so I decided to roll that User
Control and the code-behind for setting the property into a VS project
to see what would happen. Lo and behold it worked!

It seems that Visual Studio generates a reference for MyControl so
that it can used in code. When you work outside of Visual Studio, that
reference is missing.

I also discovered, through the Framework SDK Documentation, that using


in the .aspx page containing the User Control will work providing you
don't use code-behind - all the code has to be in the same file as the
html. This is getting half-way there. The Reference directive
generates the neccesary reference so that your code can find the User
Control and set its property. But again, put your code in a
code-behind page and this stops working again.

So here is my question:
Is there anyway to generate the reference to the UserControl in a
code-behind page without using Visual Studio?

Thanks.
David Hodges



Tue, 04 Jan 2005 01:37:28 GMT  
 User Controls: Accessing Properties (a slightly different point of view)
David,

    Not really.  The reason for this is that the ascx page actually extends
the code behind page.  This ASCX page is compiled as needed (as a resource)
into an assembly which is subsequently loaded.

    However, you don't have to use VS.NET, you can always edit the file in
notepad to create the reference.

    Needless to say, the directive at the top of the page is what sets the
reference to other controls/pages when your page is dynamically compiled.

    Hope this helps.

--
               - Nicholas Paldino [.NET MVP]


Quote:
> Several people have asked in this Newsgroup what the catch is with
> accessing User Controls in code, to set properties or retrieve values.
> Responders have suggested the following type of code in the
> code-behind page:

> Control c = LoadControl("MyControl.ascx");
> MyControl myControl = (MyControl)c;
> myControl.myProperty = "Test";

> Well I tried and tried to get that to work but. I kept getting this
> error:

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

> I don't normally work in Visual Studio so I decided to roll that User
> Control and the code-behind for setting the property into a VS project
> to see what would happen. Lo and behold it worked!

> It seems that Visual Studio generates a reference for MyControl so
> that it can used in code. When you work outside of Visual Studio, that
> reference is missing.

> I also discovered, through the Framework SDK Documentation, that using


> in the .aspx page containing the User Control will work providing you
> don't use code-behind - all the code has to be in the same file as the
> html. This is getting half-way there. The Reference directive
> generates the neccesary reference so that your code can find the User
> Control and set its property. But again, put your code in a
> code-behind page and this stops working again.

> So here is my question:
> Is there anyway to generate the reference to the UserControl in a
> code-behind page without using Visual Studio?

> Thanks.
> David Hodges



Tue, 04 Jan 2005 01:46:42 GMT  
 User Controls: Accessing Properties (a slightly different point of view)
Hi Nicholas,

I guess what I'm saying is, I don't know what to put in the code
behind page to create the reference. I must be missing something. What
statement or directive should I be using?

Thanks,
David

On Thu, 18 Jul 2002 13:46:42 -0400, "Nicholas Paldino [.NET/C# MVP]"

Quote:

>David,

>    Not really.  The reason for this is that the ascx page actually extends
>the code behind page.  This ASCX page is compiled as needed (as a resource)
>into an assembly which is subsequently loaded.

>    However, you don't have to use VS.NET, you can always edit the file in
>notepad to create the reference.

>    Needless to say, the directive at the top of the page is what sets the
>reference to other controls/pages when your page is dynamically compiled.

>    Hope this helps.



Tue, 04 Jan 2005 02:04:05 GMT  
 User Controls: Accessing Properties (a slightly different point of view)


Quote:
> Several people have asked in this Newsgroup what the catch is with
> accessing User Controls in code, to set properties or retrieve values.
> Responders have suggested the following type of code in the
> code-behind page:

> Control c = LoadControl("MyControl.ascx");
> MyControl myControl = (MyControl)c;
> myControl.myProperty = "Test";

> Well I tried and tried to get that to work but. I kept getting this
> error:

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

> I don't normally work in Visual Studio so I decided to roll that User
> Control and the code-behind for setting the property into a VS project
> to see what would happen. Lo and behold it worked!

> It seems that Visual Studio generates a reference for MyControl so
> that it can used in code. When you work outside of Visual Studio, that
> reference is missing.

> I also discovered, through the Framework SDK Documentation, that using


> in the .aspx page containing the User Control will work providing you
> don't use code-behind - all the code has to be in the same file as the
> html. This is getting half-way there. The Reference directive
> generates the neccesary reference so that your code can find the User
> Control and set its property. But again, put your code in a
> code-behind page and this stops working again.

> So here is my question:
> Is there anyway to generate the reference to the UserControl in a
> code-behind page without using Visual Studio?


register the tag.

I planed on using this tag 3 times in my ASPX page.

In my code-behind, I created 3 protected properties that had the
same or similar names to the "id" of the three tags I used in the
page.

In my ASPX, I would use the tag, then have a code snippet that
set the protected member of the code behind with the instance of
the tag. For example:

<Foo:Bar id="blargh" runat="server" name="Foo!"/>
<% base.BlarghControl = blargh; %>

<Foo:Bar id="another" runat="server" name="Another!"/>
<% base.AnotherControl = anotherTag; %>

Then, in my code behind, in the Page_Load or whatever,
I could:

if( BlarghControl.Name == "Foo!" )...

I think that's what you're asking for, no?

If you had a dynamic list of tags (say, in a repeater or something)
you could create an arraylist for the controls and add each
control/tag instance to the arraylist in the code-behind class.

The thing you need to remember is that the code-behind is
the parent class to the page. That is, the page inherits from
the code-behind. So, anything you can do with a standard
class-subclass setup, you can do with pages and code-behinds.

-c



Tue, 04 Jan 2005 02:02:48 GMT  
 User Controls: Accessing Properties (a slightly different point of view)
Thanks, that looks like a promising approach. I am trying to implement
it but running into a snag.
In my code behind page I put, in part:

public class MainClass1 : Page
{      
        protected  UserControl _MyControlRef;
        public UserControl MyControlRef {
        set { _MyControlRef = value; }
        }

In my .aspx page I register the control and then I have:

<Whatever:SomeName id="MyControl1" Name="Control1" runat="server" />
 <% MainClass1.MyControlRef = MyControl1; %>

That last line gives me the error:
 "Reference to a non-shared member requires an object reference."

 Any ideas?
Thanks,
David

On Thu, 18 Jul 2002 13:02:48 -0500, "Chad Myers"

Quote:


>register the tag.

>I planed on using this tag 3 times in my ASPX page.

>In my code-behind, I created 3 protected properties that had the
>same or similar names to the "id" of the three tags I used in the
>page.

>In my ASPX, I would use the tag, then have a code snippet that
>set the protected member of the code behind with the instance of
>the tag. For example:

><Foo:Bar id="blargh" runat="server" name="Foo!"/>
><% base.BlarghControl = blargh; %>

><Foo:Bar id="another" runat="server" name="Another!"/>
><% base.AnotherControl = anotherTag; %>

>Then, in my code behind, in the Page_Load or whatever,
>I could:

>if( BlarghControl.Name == "Foo!" )...

>I think that's what you're asking for, no?

>If you had a dynamic list of tags (say, in a repeater or something)
>you could create an arraylist for the controls and add each
>control/tag instance to the arraylist in the code-behind class.

>The thing you need to remember is that the code-behind is
>the parent class to the page. That is, the page inherits from
>the code-behind. So, anything you can do with a standard
>class-subclass setup, you can do with pages and code-behinds.

>-c



Tue, 04 Jan 2005 03:01:47 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Property with different access modifiers for get and set

2. Label in different User Control File

3. Problem accessing function in different view....

4. accessing objects in different views

5. how to access objects in different views

6. Sending messages to ActiveX user interface controls from a different thread

7. Changing control properties on different form in C#

8. dialog, form view, and control layout on different system

9. dialog, form view, and control layout on different systems

10. dialog, form view, and control layout on different systems

11. User Control Properties in Runtime

12. Properties of a user control displayed in designer

 

 
Powered by phpBB® Forum Software