473,414 Members | 1,947 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,414 software developers and data experts.

How to refer to a component in an UserControl ?

I have created an UserControl (MywindowFooter) which
contains a label-component :
<asp:label id="lblStatusBar" runat="server"></asp:label>

How can I refer to this label, from the code-behind file, to
set its Text-property ?

I already figured out that I probably should 'get' the
label-component by doing something like :

dim statusBar as System.Web.UI.Control =
MywindowFooter.FindControl("lblStatusBar")

But what to do next ? I guess I should cast statusBar
into a label-component, but I don't know how ...

Who can help me out ?
Nov 18 '05 #1
5 1404
I would not call it a good object oriented design.
The User Control should expose all necessary properties and your page should use them to manipulate the control.

Then the user control in reponse to the "SetLabelText" should call the lblStatusBar.Text = ....

George
My Site - Body Jewelry
<pe***@notmail.net> wrote in message news:pm********************************@4ax.com...
I have created an UserControl (MywindowFooter) which
contains a label-component :
<asp:label id="lblStatusBar" runat="server"></asp:label>

How can I refer to this label, from the code-behind file, to
set its Text-property ?

I already figured out that I probably should 'get' the
label-component by doing something like :

dim statusBar as System.Web.UI.Control =
MywindowFooter.FindControl("lblStatusBar")

But what to do next ? I guess I should cast statusBar
into a label-component, but I don't know how ...

Who can help me out ?
Nov 18 '05 #2
You could expose your label control through a public property in your user
control.
Something like this:
Public Readonly Property StatusBarLabel as Label
Get
Return lblStatusBar
End Get
End Property

Then from your page you can use code like this:
MyUserControl.StatusBarLabel.Text="whatever"

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
<pe***@notmail.net> wrote in message
news:pm********************************@4ax.com...
I have created an UserControl (MywindowFooter) which
contains a label-component :
<asp:label id="lblStatusBar" runat="server"></asp:label>

How can I refer to this label, from the code-behind file, to
set its Text-property ?

I already figured out that I probably should 'get' the
label-component by doing something like :

dim statusBar as System.Web.UI.Control =
MywindowFooter.FindControl("lblStatusBar")

But what to do next ? I guess I should cast statusBar
into a label-component, but I don't know how ...

Who can help me out ?

Nov 18 '05 #3
Hi Peter,

You're nearly there. In your .aspx page, you need to get a reference to the
user control within the page and within the user control get a reference to
the label. Once all that is done, you set the text:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim statusBar As UserControl
Dim lblFooter As Label
statusBar = Page.FindControl("MywindowFooter1")
If Not statusBar Is Nothing Then
lblFooter = statusBar.FindControl("lblStatusBar")
If Not lblFooter Is Nothing Then
lblFooter.Text = "Hi Peter"
End If
End If
End Sub

<form id="Form1" method="post" runat="server">
<uc1:MywindowFooter id="MywindowFooter1"
runat="server"></uc1:MywindowFooter>
</form>

Does this help?

Ken
Microsoft MVP [ASP.NET]
<pe***@notmail.net> wrote in message
news:pm********************************@4ax.com...
I have created an UserControl (MywindowFooter) which
contains a label-component :
<asp:label id="lblStatusBar" runat="server"></asp:label>

How can I refer to this label, from the code-behind file, to
set its Text-property ?

I already figured out that I probably should 'get' the
label-component by doing something like :

dim statusBar as System.Web.UI.Control =
MywindowFooter.FindControl("lblStatusBar")

But what to do next ? I guess I should cast statusBar
into a label-component, but I don't know how ...

Who can help me out ?


Nov 18 '05 #4
Or, if all you need is the label text, just expose that as the property,
with a Get and Set which access it.

"Steve C. Orr [MVP, MCSD]" <St***@Orr.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
You could expose your label control through a public property in your user
control.
Something like this:
Public Readonly Property StatusBarLabel as Label
Get
Return lblStatusBar
End Get
End Property

Then from your page you can use code like this:
MyUserControl.StatusBarLabel.Text="whatever"

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
<pe***@notmail.net> wrote in message
news:pm********************************@4ax.com...
I have created an UserControl (MywindowFooter) which
contains a label-component :
<asp:label id="lblStatusBar" runat="server"></asp:label>

How can I refer to this label, from the code-behind file, to
set its Text-property ?

I already figured out that I probably should 'get' the
label-component by doing something like :

dim statusBar as System.Web.UI.Control =
MywindowFooter.FindControl("lblStatusBar")

But what to do next ? I guess I should cast statusBar
into a label-component, but I don't know how ...

Who can help me out ?


Nov 18 '05 #5
Thanks guys, for all your replies.

You're nearly there. In your .aspx page, you need to get a reference to the
user control within the page and within the user control get a reference to
the label. Once all that is done, you set the text:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim statusBar As UserControl
Dim lblFooter As Label
statusBar = Page.FindControl("MywindowFooter1")
If Not statusBar Is Nothing Then
lblFooter = statusBar.FindControl("lblStatusBar")
If Not lblFooter Is Nothing Then
lblFooter.Text = "Hi Peter"
End If
End If
End Sub

<form id="Form1" method="post" runat="server">
<uc1:MywindowFooter id="MywindowFooter1"
runat="server"></uc1:MywindowFooter>
</form>

Does this help?

Ken
Microsoft MVP [ASP.NET]
<pe***@notmail.net> wrote in message
news:pm********************************@4ax.com.. .
I have created an UserControl (MywindowFooter) which
contains a label-component :
<asp:label id="lblStatusBar" runat="server"></asp:label>

How can I refer to this label, from the code-behind file, to
set its Text-property ?

I already figured out that I probably should 'get' the
label-component by doing something like :

dim statusBar as System.Web.UI.Control =
MywindowFooter.FindControl("lblStatusBar")

But what to do next ? I guess I should cast statusBar
into a label-component, but I don't know how ...

Who can help me out ?


Nov 18 '05 #6

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

Similar topics

12
by: Chien Lau | last post by:
I had a situation occur today that's happened a number of times before and I'd like to get your take on it: Imagine... You're developing a WinForms app for a client that includes the use charts....
0
by: Anonymous | last post by:
Hello, i have developed a web componenet library project, which access the usercontrol that resides in another web application. In my component, I load the control like this: Dim uCtrl As...
6
by: TnCoder | last post by:
Hi; I just moved from vb 6.0 to vb.net. I am trying to add MS Calendar control to my toolbox. After the control is added, it is not enabled. Is there dll I have to add? Can anyone who could give...
3
by: Tim::.. | last post by:
Can someone please tell me how I refer to a Hyperlink object in a usercontrol from a sub in another aspx page??? I want to change the visibile attribute of a hyperlink in a user control from an...
4
by: SamSpade | last post by:
If I have a class that inherits from, say RichTextBox and in the derived class I refer to Handle, does it refer to the RichTextBox class or the derived class? I'm wondering if the derived class...
3
by: BluDog | last post by:
Hi I have a component that inherits from TreeView, my project is a standard exe. How do i get the component into the toolbox for use within the project? I find that only controls based on...
1
by: Dave | last post by:
hi, I am creating a usercontrol in .net that inherits from the panel control. I want to display it in the component tray -- like the timer control -- and not on the form. thanks, dave
2
by: AMDRIT | last post by:
Hello everyone, I have created a custom component and one of its properties is a class object with it's own properties. During runtime, I can assign values to the class object properties just...
3
by: Frank Rizzo | last post by:
In the WinForms context.
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.