Access User Control property from Main page
-
Hi, Got a user control in my master page. I want to access the prperty of user control from the main page. How can I do that? Thanks
<%@ Register TagPrefix="ucl" TagName="myCntrl" Src="~/UCL.ascx" %> <ucl:myCntrl runat="server" /> Partial Class UCL Inherits System.Web.UI.UserControl Priavte x as String = "" Public Property ConCatStr() As String Get Return x & "Color" End Get Set(ByVal value As String) Dim x As String = value End Set End Property End Class Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' Want to refer controls's ConCatStr property on PostBack End Sub
-
Hi, Got a user control in my master page. I want to access the prperty of user control from the main page. How can I do that? Thanks
<%@ Register TagPrefix="ucl" TagName="myCntrl" Src="~/UCL.ascx" %> <ucl:myCntrl runat="server" /> Partial Class UCL Inherits System.Web.UI.UserControl Priavte x as String = "" Public Property ConCatStr() As String Get Return x & "Color" End Get Set(ByVal value As String) Dim x As String = value End Set End Property End Class Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' Want to refer controls's ConCatStr property on PostBack End Sub
Hi, I'll put that down in C# as I'm not really familiar with VB.NET exact syntax, sorry. In the main page load method:
UserControl myMasterControl = (UserControl)this.Master.FindControl("myCntrl");
string myProperty = myMasterControl.ConCatStr;
-- If this is a post that has been helpful to you, please vote for it. Thank you! "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook
-
Hi, I'll put that down in C# as I'm not really familiar with VB.NET exact syntax, sorry. In the main page load method:
UserControl myMasterControl = (UserControl)this.Master.FindControl("myCntrl");
string myProperty = myMasterControl.ConCatStr;
-- If this is a post that has been helpful to you, please vote for it. Thank you! "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook
I tried doing what you mentioned. Did not work! Do I need to assign an ID to the user control? Without that I am not able to findControl on the page.
<ucl:myCntrl runat="server" id="uclCntrl" /> On page load: Dim ucl As System.Web.UI.UserControl ucl = Me.Master.FindControl("uclCntl") Response.Write("Found User control " & (Not ucl Is Nothing) & " !") ' I get True for this ucl.Test() 'ERROR: Test is not a member of System.Web.UI.UserControl
What is wrong here?