Accessing USER CONTROL which is inside Masterpage through Another USER Control inside normal page.

Accessing USER CONTROL which is inside Masterpage through Another USER Control inside normal page.

Post by Kiran » Tue, 14 Nov 2006 15:16:22


I have created a lable in the InfoMessageBox.ascx control.
This control in used in Master Page.

One Page is designed by making use of this Master Page.
This ASPX is having another user control.

This user control has got one buttong which on click makes database call.

The response returned after clicking the above button, i.e. either Error or Success, the label from InfoMessageBox will be set.

Now I am unable to access the LABEL control Which is inside InfoMessageBox.ASCX which is used inside the Master Page.

Please help me in this.

kiran
 
 
 

Accessing USER CONTROL which is inside Masterpage through Another USER Control inside normal page.

Post by Mike » Tue, 14 Nov 2006 23:35:50

Some may suggest you do a Master.FindControl to get the master pages
label control to set its properties directly.

But maybe put a property in the master page that in turn sets a
property on its user control that then sets the label controls text.

And also have the button web control raise an event when the status
changes and in its pages handler for that call the master pages
property let.

That way the interaction is between the control and the page, the page
and it's master, and the master and it's control, and not directly
between the two controls.


So in the button user control

Public Event StatusChanged(ByVal s As String)
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
RaiseEvent StatusChanged(Guid.NewGuid.ToString)
End Sub

And in the page that contains it.

Protected Sub WebUserControl1_StatusChanged(ByVal s As String)
Handles WebUserControl1.StatusChanged
Dim mp As TheMasterPage = Me.Master
mp.NewStatus = s
End Sub

And in the master page.

Public WriteOnly Property NewStatus() As String
Set(ByVal value As String)
WebUserControl2_1.StatusText = value
End Set
End Property

And in the label user control.

Public WriteOnly Property StatusText() As String
Set(ByVal value As String)
Label1.Text = value
End Set
End Property


It may seem like a lot of code up front but going forward any page can
set the status with just this.

Dim mp As TheMasterPage = Me.Master
mp.NewStatus = "Whatever"

This will have to be changed to use an interface that all master pages
implement if you ever get around to wanting to change the master page
of the site on the fly.