How to set default Master Page dynamically - asp.net

I have two master pages. When the user logs in it should set first master page as default master page. If other login it should set second master page.

You can specify the default master page to use in the web.config file, using the masterPageFile attribute of the pages element:
<system.web>
<pages masterPageFile="~/DefaultMaster.master" />
<!-- more configuration goes here... -->
</system.web>
The page will use that master page unless it has a MasterPageFile specified.

As #RoBYCoNTe pointed out in the comments, you can set the MasterPageFile property on your pages.
If each page inherits from a common BaseClass, you can set the MasterPageFile property in your BaseClass to avoid having to do it on each individual page.

Related

Preventing Pages theme from being imposed on each aspx page directive during compilation

in my web.config i use the following pages declaration
<pages maintainScrollPositionOnPostBack="true" Theme="Theme1">
with the intention of being able to use different themes on a web application to customize the way it displays for a few different clients(some of which want their own branding on the web application's pages)
if i leave out the theme directive in my development code, it doesn't display properly when debugging, so i want to have the Theme defined in the web.config... however when it is defined as above and i go to publish my solution every .aspx page declaration goes from this:
<%# Page Language="VB" CodeFile="MRSHome.aspx.vb"%>
to this:
<%# page language="VB" inherits="App_Web_peiwi24o" theme="Theme1" maintainScrollPositionOnPostBack="true" %>
and page level theme declarations override the ones set in the web.config file, i need that to remain the case since some pages programmatically switch to a print friendly theme when the user needs it.
How do i prevent the web.configs pages settings from being appended to each page's page declaration during compilation?
Instead of declaring the theme in web.config page declaration, you can add a appsettings in webconfig:
<appSettings>
<add key="MyCustomTheme" value="Theme1" />
</appSettings>
And in form PreInit you can set the theme for the page:
Protected Sub WebForm1_PreInit(sender As Object, e As EventArgs) Handles Me.PreInit
Dim myTheme As String = ConfigurationManager.AppSettings("MyCustomTheme")
Page.Theme = myTheme
End Sub
So, when you deploy, your page declaration will not contain any theme. In runtime it will pickup the correct theme defined in config.

Accessing ASP Master Page Properties

I have an ASP.net page which uses a master page. The master page has a public property. Does anybody know how can I access the given property from a content page that uses the master page?
Look into Strongly Typing your Master Page link
Check out the <%# MasterType virtualPath="~/MasterPage.master"%> tag.
This enables you to directly have access to the public properties/methods.

what is the masterpage on this asp.net webformpage?

I have a asp.net webformpage without a MasterPageFile property at the top but I have asp:content tags in there. I assume this page has a masterpage what is the masterpage in this case?
Check web.config maybe there is a global master page define in the <page> element there.
Also, check the code-behind. Maybe it's declared in code.

scroll go to the top page

net use web methods to save data from JavaScript when i press save the all page go the top how i can save state position after post back
You can set it programmatically
Page.MaintainScrollPositionOnPostBack = true;
In the page declaration
<%# Page MaintainScrollPositionOnPostback="true" %>
Or in the web.configs <system.web> section.
<pages maintainScrollPositionOnPostBack="true" />
The reason the page is returning to the top, is because it is being Submitted and hence reloaded.
If you want to keep the position of the page you have 2 options;
1) Use a Anchor (See: http://www.hypergurl.com/anchors.html)
2) Post your form using AJAX and then you do not need to reload the page
Option 2 is the prefered, if you are using webforms you could use an AJAX Update Panel, or if you are using MVC you can do it using JQuery.
try to setting the following page directive in your page
<%# Page MaintainScrollPositionOnPostback="true" %>
I solved the question, and my method of doing this was the following:
I put a javascript:void(0) in an a tag, as shown here:
<a href="javascript:void(0)">

Are themes visible at design time?

This is my first experience with themes.
I've created a skin file for a textbox and the defined theme is only visible at runtime.
Is this the way theme works or I am missing something?
You should use StylesheetTheme property that can be set on page level or globally
Page Level
<%# Page Language="C#" StylesheetTheme="MyTheme" %>
Global (web.config)
<system.web>
<pages styleSheetTheme="MyTheme" />
<system.web>

Resources