Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > How to reference UserControl in server code

Reply
Thread Tools

How to reference UserControl in server code

 
 
Mark Friedman
Guest
Posts: n/a
 
      07-09-2003
I can't seem to figure out how to get a reference to a UserControl in the
code-behind for the page that contains the control. All the examples I've
seen show how to pass property values from the containing page's HTML to the
UserControl but nothing I've seen shows how to reference the UserControl's
properties (or subcontrols) from the containing page's server-side code.
Note that I'm not creating the UserControl prgrammatically via LoadControl -
I'm creating the UserControl declaratively in the page's HTML.

Thanks in advance for any help.

-Mark


 
Reply With Quote
 
 
 
 
Kenn Ghannon
Guest
Posts: n/a
 
      07-09-2003
I think you can do it using a
System.Web.UI.HtmlControls.HtmlGenericControl...


"Mark Friedman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I can't seem to figure out how to get a reference to a UserControl in the
> code-behind for the page that contains the control. All the examples I've
> seen show how to pass property values from the containing page's HTML to

the
> UserControl but nothing I've seen shows how to reference the UserControl's
> properties (or subcontrols) from the containing page's server-side code.
> Note that I'm not creating the UserControl prgrammatically via

LoadControl -
> I'm creating the UserControl declaratively in the page's HTML.
>
> Thanks in advance for any help.
>
> -Mark
>
>



 
Reply With Quote
 
 
 
 
Mark Friedman
Guest
Posts: n/a
 
      07-10-2003
I'm not sure what you mean here, Kenn. Could you be a little more specific?

-Mark

"Kenn Ghannon" <(E-Mail Removed)> wrote in message
news:tg0Pa.7215$(E-Mail Removed) .com...
> I think you can do it using a
> System.Web.UI.HtmlControls.HtmlGenericControl...
>
>
> "Mark Friedman" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > I can't seem to figure out how to get a reference to a UserControl in

the
> > code-behind for the page that contains the control. All the examples

I've
> > seen show how to pass property values from the containing page's HTML to

> the
> > UserControl but nothing I've seen shows how to reference the

UserControl's
> > properties (or subcontrols) from the containing page's server-side code.
> > Note that I'm not creating the UserControl prgrammatically via

> LoadControl -
> > I'm creating the UserControl declaratively in the page's HTML.
> >
> > Thanks in advance for any help.
> >
> > -Mark
> >
> >

>
>



 
Reply With Quote
 
Mark Friedman
Guest
Posts: n/a
 
      07-10-2003
How do you know which control is the one you want, since the IDs get
mangled?

-Mark

"Iain" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> "Mark Friedman" <(E-Mail Removed)> wrote in message

news:<(E-Mail Removed)>...
> > I can't seem to figure out how to get a reference to a UserControl in

the
> > code-behind for the page that contains the control. All the examples

I've
> > seen show how to pass property values from the containing page's HTML to

the
> > UserControl but nothing I've seen shows how to reference the

UserControl's
> > properties (or subcontrols) from the containing page's server-side code.
> > Note that I'm not creating the UserControl prgrammatically via

LoadControl -
> > I'm creating the UserControl declaratively in the page's HTML.
> >
> > Thanks in advance for any help.
> >
> > -Mark

>
> I've been having the same problem. To work around it, I have some code
> that iterates through the controls on the page looking for a control
> with a particular id, so I have a function that iterates across
> Page.Controls[1].Controls checking each control to see if it's id is
> the desired one, and returning it when found. It's a really terrible
> way to do things, but it seems to be a suitable placeholder until I
> find out how it's supposed to be done.
>
> -Iain



 
Reply With Quote
 
Mark Friedman
Guest
Posts: n/a
 
      07-10-2003
I discovered the answer to my own question. You just need to add a member
to your page behind class with the same name as the ID of the UserControl
and with a type of your UserControl, which is usually the same as the first
part of the UserControl's ascx file name. For example, if your UserControl
is defined in Foo.ascx and you place it in Bar.aspx as:

<uc1:Foo id="Foo1" runat="server"></uc1:Foo>

then in your code-behind page for Bar.aspx you just need to have:


Public Class Bar
Inherits System.Web.UI.Page
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
...

' This must be put in manually, even though it ought to be done
automatically
' by VS.NET when you put the user control on the page, just like it
did for
' the button above
Protected Foo1 As Foo


Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As
System.EventArgs) Handles Button1.Click
Foo1.MyProperty = "Whatever"
End Sub
...

End Class

and then you can refer to the properties, etc. of the UserControl.

Not my comment above that I think that it's a bug that VS.NET doesn't
automatically put the member variable in there for you the way it does for
other server controls.

-Mark

"Mark Friedman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I can't seem to figure out how to get a reference to a UserControl in the
> code-behind for the page that contains the control. All the examples I've
> seen show how to pass property values from the containing page's HTML to

the
> UserControl but nothing I've seen shows how to reference the UserControl's
> properties (or subcontrols) from the containing page's server-side code.
> Note that I'm not creating the UserControl prgrammatically via

LoadControl -
> I'm creating the UserControl declaratively in the page's HTML.
>
> Thanks in advance for any help.
>
> -Mark
>
>



 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Usercontrol derived from Usercontrol without double Html Code Reik ASP .Net Web Controls 1 04-12-2005 04:34 PM
Can we use a usercontrol inside a usercontrol Rajesh Tiwari ASP .Net 0 08-12-2003 03:56 PM
How to reference UserControl in server code Mark Friedman ASP .Net 4 07-10-2003 05:58 PM
Use LoadControl to load a usercontrol but the webcontrol in the usercontrol can not AutoPostBack huobazi ASP .Net 1 07-03-2003 03:14 AM



Advertisments