Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Related

Development

More Development >

Latest Network Security More Network Security >

Latest Internet Technology More Internet Technology >

Latest Development More Development >

Latest Database More Database >

Latest Servers More Servers >

Latest Mobile Phone More Mobile Phone >

Latest Android Software More Android Software >

Latest Apple Software More Apple Software >

Latest Computer Software News More Computer Software News >

Latest IT Information More IT Information >

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report