473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to reference UserControl in server code

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
Nov 17 '05 #1
4 7948
I think you can do it using a
System.Web.UI.HtmlControls.HtmlGenericControl...
"Mark Friedman" <bi******@yahoo.com> wrote in message
news:eU**************@TK2MSFTNGP12.phx.gbl...
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

Nov 17 '05 #2
I'm not sure what you mean here, Kenn. Could you be a little more specific?

-Mark

"Kenn Ghannon" <ke****@ameritech.net> wrote in message
news:tg********************@newssvr28.news.prodigy .com...
I think you can do it using a
System.Web.UI.HtmlControls.HtmlGenericControl...
"Mark Friedman" <bi******@yahoo.com> wrote in message
news:eU**************@TK2MSFTNGP12.phx.gbl...
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


Nov 17 '05 #3
How do you know which control is the one you want, since the IDs get
mangled?

-Mark

"Iain" <ia*******@optusnet.com.au> wrote in message
news:24**************************@posting.google.c om...
"Mark Friedman" <bi******@yahoo.com> wrote in message

news:<eU**************@TK2MSFTNGP12.phx.gbl>...
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

Nov 17 '05 #4
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" <bi******@yahoo.com> wrote in message
news:eU**************@TK2MSFTNGP12.phx.gbl...
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

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: bill yeager | last post by:
When trying to run my web project, I get the following error: Parser Error Description: An error occurred during the parsing of a resource required to service this request. Please review the...
1
by: Kruno | last post by:
Hi ! if anyone knows the answer I would appreciate it: I have a user control for the header of the page with one label in it..I want the label to change as the pages are changing.... my...
9
by: Moe Sizlak | last post by:
Hi There, I am trying to write the selected value of a listcontrol when a button is clicked and I keep getting the error "object not set to a reference of an object". The libox itself is in a...
2
by: Sascha | last post by:
Hi there, I searched carefully through the web before finally deciding to post this message, because I could not find a solution for my problem. Hopefully someone will have a hint or explanation...
0
by: N. Demos | last post by:
Hello, I have a custom usercontrol, of which I have two instances of in my aspx page. Both the usercontrol and page have codebehind. In the page codebehind, I want a member variable for each...
12
by: Joe | last post by:
Hello All: Do I have to use the LoadControl method of the Page to load a UserControl? I have a class which contains three methods (one public and two private). The class acts as a control...
1
by: aarepasky | last post by:
I list the code below, but the line that has the error is: txtCompanyName.Enabled = false; The error is: Object reference not set to an instance of an object. Default.aspx.cs using System;...
3
by: j-in-uk | last post by:
I have 3 pages Site.master, Album.aspx and ContinentsMenu.ascx. Continents is a usercontrol placed in Albums. In Album.aspx.cs I need to access a property from the Continents usercontrol and is...
1
by: Nathan Sokalski | last post by:
I have a UserControl that I declare programmatically as follows: Dim userctrl as New rightside_portal() The codebehind file for this UserControl looks like the following: Partial Public...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.