Get the App
SLTechnology News&Howtos  ›  Development  › 

How to access the resources of a master page for a content page in an ASP.NET web page template

Shulou Source: shulou.com Published: 2022-06-02 07:52:08 10月02日 Update

This article mainly introduces "how to access the resources of the master page on the content page in the ASP.NET web page template", in daily operation, I believe many people have doubts about how to access the resources of the master page on the content page in the ASP.NET web page template, small make up all kinds of information, sort out simple and easy to use operation method, hope to answer "how to access the resources of the master page on the content page in the ASP.NET web page template" doubts helpful! Next, please follow the small series to learn together!

Master Resource Page Code

〈%@ Master Language="C#" %〉 〈! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/ xhtml1/DTD/xhtml1-transitional.dtd"〉 〈script runat="server"〉 public void SetCurNavItem(int itemIndex) { nav.Items[itemIndex].Attributes.CssStyle. Add("color", "#FF0000"); } 〈/script〉 〈html xmlns="http://www.w3.org/1999/xhtml" 〉 〈head runat="server"〉 〈title〉Untitled Page〈/title〉 〈/head〉 〈body〉 〈form id="form1" runat="server"〉 〈div〉 〈asp:ContentPlaceHolder id="m_header" runat="server"> 〈asp:ContentPlaceHolder ID="m_content" runat="server"$> in 〈asp:ContentPlaceHolder ID="m_footer" runat="server"$>

Copyright

ASP.NET Web Template Content Page Code

< %@PageLanguage="C#"MasterPageFile=" MasterPage1.master"Title="Home"%> protectedvoidPage_Load (objectsender,EventArgse) { (ASP.masterpage1_master)Master). SetCurNavItem(0); }

Here is the content of the first page

effect

As you can see, the *** item in the navigation bar is highlighted.

better way

ASP.masterpage1_master is a cast, which seems inconvenient to use because we have to write this code repeatedly every time we use Master, which we can do with the MasterType directive.

< %@PageLanguage="C#"MasterPageFile=" MasterPage1.master"Title="Home Page"%>< %@MasterTypeVirtualPath="MasterPage1.master"%> protectedvoidPage_Load(objectsender,EventArgse) { Master.SetCurNavItem(0); }

Here is the content of the Home Page

On the previous page, we described two ways to access master resources in ASP.NET Web templates. The second method is simple and quick, but in some cases it is equally ineffective.

For example: a page needs three layout styles, which may require three templates, the visitor decides which template to use, that is, when the page is developed, it is not known which template will be used, and the @ MasterType VirtualPath cannot determine the type of MasterPage.

We can do this:

Create a base class.

Template pages derive from this base class.

The content page determines which template to use through MasterPageFile, and handles the resources of these templates using the base class type through @ MasterTypeName.

Here's how it works (just one template file code):

Base class file code:

usingSystem; usingSystem.Web.UI; namespaceMasterPageNameSpace { publicabstractclassMasterPageClass: MasterPage { publicabstractvoidSetCurNavItem (intitemIndex); } }

Note the namespaces used and the modifiers for classes and methods.

Template file code:

〈%@MasterLanguage="C#"Src="m1.cs" Inherits="MasterPageNameSpace.MasterPageClass"%〉 〈! DOCTYPEhtmlPUBLIC"-//W3C// DTDXHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/ xhtml1-transitional.dtd"〉 〈scriptrunat="server"〉 publicoverridevoidSetCurNavItem(intitemIndex) { nav.Items[itemIndex].Attributes.CssStyle.Add ("color","#FF0000"); } 〈/script〉 〈htmlxmlns="http://www.w3.org/1999/xhtml"〉 〈headrunat="server"〉 〈title〉UntitledPage〈/title〉 〈/head〉 〈body〉 〈formid="form1"runat="server"〉 〈div〉 in

Copyright

We can also compile the base class file into DLL and put it in bin directory through CSC, or put the.cs file in App_Code directory, and omit Src from the instruction.

ASP.NET Web Template Content File Code:

< %@PageLanguage="C#"MasterPageFile=" MasterPage1.master"Title="Home Page"%>< %@ MasterTypeName =" MasterPageNameSpace.MasterPageClass"%> protectedvoidPage_Load(objectsender,EventArgse) { Master.SetCurNavItem(0); }

Here is the content of the home page

At this point, the study on "How to access the resources of the master page for the content page in the ASP.NET web page template" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Tags: Templates web pages resources code content files home pages methods navigation internal templates learning types web templates directives more copyright all rights reserved directories Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi NVidia Microsoft MySQL vpn