How to refer to a component in an UserControl ?

  • Thread starter Thread starter peter
  • Start date Start date
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 ?
 
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 ?
 
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"
 
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]
 
Or, if all you need is the label text, just expose that as the property,
with a Get and Set which access it.
 
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 ?
 
Back
Top