Referencing a User Control from a page

Last post 05-10-2008 6:36 AM by Dave Sussman. 3 replies.

Sort Posts:

  • Referencing a User Control from a page

    05-09-2008, 11:41 AM
    • Loading...
    • Skipy
    • Joined on 05-09-2008, 11:31 AM
    • Posts 7

    I have a MasterPage and multiple other pages that use it. Also, the MasterPage contains a UserControl defined by me. How can I use/reference that UserControl from a page that uses the MasterPage?

     Many thanks in advance.

  • Re: Referencing a User Control from a page

    05-09-2008, 3:09 PM
    • Loading...
    • BHendry
    • Joined on 01-25-2006, 4:48 PM
    • Posts 203

    Hi Skipy,

    You can use FindControl from your child page to reference controls in the master. Here's an example:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            Dim thisLabel As New Label
    
            thisLabel = Me.Master.FindControl("lblPageHeading")
    
            If IsNothing(thisLabel) = False Then
                thisLabel.Text = "About Us"
            End If
    
        End Sub
     
    Bruce


    Please remember to click "Mark as Answer" on the posts that helped solve your issue.
  • Re: Referencing a User Control from a page

    05-10-2008, 4:24 AM
    • Loading...
    • Skipy
    • Joined on 05-09-2008, 11:31 AM
    • Posts 7

     The problem is I'm not trying to reference/ use a Label or any other standard control, but a User Control I made (let's call it UC).

    And I cannot do something like:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            Dim thisUC As New UC
    
            thisUC = Me.Master.FindControl("myUC")
    
            If IsNothing(thisUC) = False Then
                thisUC.StartMyVeryOwnMethod(47)
            End If
    
        End Sub
     ...simply because it does not see UC as a valid class.
  • Re: Referencing a User Control from a page

    05-10-2008, 6:36 AM
    Answer

    In your content page add the following to the top, underneath the Page directive:

    <%@ Reference Control="~/MyUserControl.ascx" %>

    The content page will then know what the class is.

Page 1 of 1 (4 items)