alice7
alice7

Reputation: 3884

How to Instantiate User control in Master page cs file

I have added uc in contentplace holder of master page but how do you instantiate the user control in master page .cs file to make it visible when the master page loads

Upvotes: 3

Views: 4226

Answers (3)

Pat Hermens
Pat Hermens

Reputation: 904

If I read this correctly, you have a control in a Master page and need to reference it from the child pages to change it's visibility? Or have I got things around the wrong way?

Anyway, here's how I commonly do it in VB .Net, it shouldn't be too hard to port ;-)

In the aspx page:

<%@ Reference Control="~/path/to/my/customControl.ascx" %>

In the code-behind:

Dim customControl As ASP.customcontrol_ascx = Master.FindControl("customControl")
If customControl IsNot Nothing Then
    ...
End If

Upvotes: 0

Chris Lees
Chris Lees

Reputation: 2210

I'm not positive but I think you need to use the Page.LoadControl() to officially load a user control to a page at run time.

Upvotes: 0

Mark Brackett
Mark Brackett

Reputation: 85665

If you add it to a ContentPlaceHolder, and a child page adds content to that ContentPlaceHolder - you're user control will no longer appear. IOW, the controls you add are only the default.

Just add it outside of the ContentPlaceHolder if you want it always visible.

Upvotes: 3

Related Questions