How to reference a Master Page from a user control? - c#

I'm looking for a way to (preferably) strongly type a master page from a user control which is found in a content page that uses the master page.
Sadly, you can't use this in a user control:
<%# MasterType VirtualPath="~/Masters/Whatever.master" %>
I'm trying to access a property of the master page from the user control and would rather not have to pass the property from the master page to the content page to the user control because multiple content pages use the same user control. One change, one place whatnot.

Try Page.Master.
Whatever whatev = (Whatever)Page.Master;
You'll have to make sure you add the proper using statements to the top of your file, or qualify the Master page type inline.
One potential gotcha is if this control is used by a different page whose master page is NOT the same type. This would only get caught at runtime.

Have you tryed Page.FindControl("name") on the usercontrol?

The best way to do it that I've found is actually to build a custom class that is based off of UserControl, give it a Master property with a get accessor that fishes through the this.Page.Parent until it stops encountering master pages (If you are nesting, this step is unnecessary otherwise) and then return that web control as the type of the master page you want to use. Then, when you add a new user control, change it's base class to the name of your custom class. The .Master property will be accessible and cast properly as the master page you want it to use.

In VB all I needed to do was change this:
Dim lAuthLevel As Integer = Master.MasterContact.AuthenticationLevel
to this:
Dim lAuthLevel As Integer = CType(Me.Page.Master, main).MasterContact.AuthenticationLevel
So all references of Master become Ctype(Me.Page.Master, typeofMaster)
Where is in this case the word "main" - get that from the declaration at the top of the master page. e.g.
So "main" in this case :)

Related

Referencing partial view or another master page inside dotvvm masterpage

I am trying to render something like razor's render partial in dotvvm master page. but found nothing from the documentation but the following:
Master Page Nesting
You can also nest a master page in another master page and so on. Just use the #masterPage directive in the master page to specify parent master page.
Basically I want to render navigation menu which will be defined in another master page in my parent master page.
I think that you are looking for a Markup Control, see the docs for more details: https://www.dotvvm.com/docs/tutorials/control-development-markup-only-controls/2.0
In short, markup control allows you to declare custom control in a dothtml file. You can use all DotVVM features in the markup control, you'll just have to explicitly declare how data from the view model should be passed if you want to use data bindings.
A minimalist markup control may look like this:
<!-- The control must be used when data context is this view model: -->
#viewModel Full.Name.Of.MyViewModelBase
<ul class=menu>
<li>{{value: NameOfSomething}}</li>
<li>...</li>
</ul>
Then, the control must be registered, so DotVVM can find it:
// in DotvvmStartup
config.Markup.AddMarkupControl(tagPrefix: "cc", tagName: "MyMenu", "Views/MyMenu.dotcontrol");
After that, you can use the control anywhere you want (well, recursion works only sometimes 😃):
<cc:MyMenu />
You can also declare properties and then use it inside the control. It may help with reusability of the control since the view model does not have to fit. I'll leave it to the docs

How to access controls on a master page dynamically from another user control, if the web application has nested master page

I have a user control, say control 1, that looks for control2, which is placed on the root master page : Site.Master
And this is how I am getting Control2 from Control 1 now,
MasterPage showMaster = this.Page.Master.Master;
MasterPage siteMaster = showMaster.Master;
Control2= siteMaster.FindControl("Control2");
The above code works fine. But because our application uses nested master pages, I am running into a bit of situation here.
How do I find control 2 dynamically regardless of where I put Control1 in which template? Becasue right now, depends on where I put Control1, and how nested is that template in relation to the Site.Master, I have to change how far up in the chain I get Site.Master in the Control 1 code.
Any good ideas on how I can avoid doing that?
Please advise.
i found a work around with recursively loading the control

How to define a Content Control through C# code

I am writing a webpart and was trying to update the browser title... so, I went into mywebpart.ascx added the following:
<asp:Content ID="contentPageTitle" ContentPlaceholderID="PlaceHolderPageTitle" runat="server">
<%= SPContext.Current.Site.OpenWeb().Title %>
</asp:Content>
I then got this error:
Content controls have to be top-level controls in a content page or a nested master page that references a master page.
So, I am trying to do it programatically in mywebpart.cs by doing:
Content content = new Content();
content.ContentPlaceHolderID = "PlaceHolderPageTitle";
I now need to input this piece: SPContext.Current.Site.OpenWeb().Title
What property in the Content control allows me to do that? If there is a better way to do this, I am open as well. Thanks.
Unfortunately, you cannot place a content control within a user control. As the error message indicates, content controls must be top level controls in a page or master page, and cannot belong in any other kind of control.
An alternate approach might be to customize your page layout or master page to contain the logic you want to provide.
If you have some assurance of the ID of the content control (not the ContentPlaceholderID, mind you), then you can interact with the content control like so:
var content = Page.FindControl("contentPageTitle");
content.Controls.Add(new LiteralControl("Hello, World!"));
--
On an aside, do ensure that any SPWeb opened with OpenWeb() gets disposed properly, or you may face memory management problems down the road.

Master Pages ASP.net in C# adding dynamic flavor

I need to have a button on the master page.
Once that button is clicked I generate a string that represents URL.
test.apx is a content page I use and the string will look like something like this:
Example:
www.blah.com/test.aspx?user=blax&develop=extreme_all
Now all I need is to reload the page while content is redirected to the URL I generated.
I hope this makes more sense.
Thanks guys I am new to asp.net and really appreciate any help
Why dont you use Update Panel?
Have the page postback with the updated query string to change what is in your content area
Assuming your masterpage is set up correctly
within the <asp:content> tag of your aspx page that is using the masterpage you created add code to get the query string
Request.QueryString["key"]
example url: http://www.whatever.com?foo=bar&bar=foo
string tmp = Request.QueryString["foo"]
tmp will become "bar"
Now just check the "postback" option of the asp:control you're using to reload the content page or do whatever you to make the page refresh.
If I understand your question correctly, you want to reuse the same code to parse out your user and develop variables from different content pages that use the same master page.
It sounds like you need a strongly typed master page.
First, put your shared code in your master page. Then, expose the parsed data as properties of the master page. Next, simply add the following directive in your content pages:
<%# MasterType VirtualPath="~/mymasterpage.master" %>
Finally, in your content pages, you can reference your properties as such (assuming you created a property called MyUser):
string user = this.Master.MyUser;
You can also use inheritance if you want a different approach. Simply create class that inherits from Page. Then put your shared code in that class. Finally, make your content pages inherit from your new class, instead of Page.

How to access a user control in a masterpage from a content page?

Lets say that I have a header user control in a master page, and want to change a property of the user control depending on what content page is loaded inside of the master page. How might I go about this?
Thanks!
You can use two methods. The first is by using Page.Master.FindControl('controlID'). Then you can cast it to the type of your user control. The second method is by adding a <%# MasterType VirtualPath=""> OR <%# MasterType TypeName=""%> tag to your aspx page. In the VirtualPath add the virtual path to the master page, or the class in the TypeName. You can then access everything with intellisense.
first find the user control in the masterpage as below.Then find the control you need to access their property.
UserControl uch = Page.Master.FindControl("ucHeader1") as UserControl;
PlaceHolder phProxylist= ucHeader1.FindControl("phProxy") as PlaceHolder;
DropDownList ddlproxylist1 = ucHeader1.FindControl("ddlProxyList") as DropDownList;
phProxylist.Visible = false;
Hope this helps.
There's one other method, and that's by making a public property on the master page that exposes the user control.
Using a public property would work. In the content page's FormLoad method, you could do something like this (VB):
Dim myMaster as MyMasterPage = CType(Page.Master, MyMasterPage)
myMaster.MyUserControl.Text = "Hello!"

Categories

Resources