How to refer to a component in an UserControl ?

P

peter

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 ?
 
G

George

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
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 ?
 
S

Steve C. Orr [MVP, MCSD]

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"
 
K

Ken Cox [Microsoft MVP]

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]
 
R

Rick Spiewak

Or, if all you need is the label text, just expose that as the property,
with a Get and Set which access it.
 
P

peter

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]


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 ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top