) Nested master page

Source: Internet
Author: User
Introduction
A master page is equivalent to a template page, which is quite simple and has nothing to say. Common functions based on the master page include transferring information between the master page and the content page, and finding the control in the content page using the findcontrol method on the Content Page. In addition, the master page can be nested.

Key
Add a strong reference of the master page to the header of the Content Page.
<% -- Create a strong type reference to the master page and specify the virtual path to the master page -- %>
<% @ Mastertype virtualpath = "~ /Masterpage. Master "%>
1. pass data on the content page to the master page-create a public method on the master page, and then call this public method through "Master. Method" on the Content Page

2. pass data on the master page to the content page-create a public event on the master page to transfer data, and then process the event on the Content Page.

3. On the content page, use the findcontrol method to find the control on the Content Page. Use "Master. findcontrol (" contentplaceholder1 "). findcontrol (" the Control ID you want to find ")" to find the control.

4. nesting the master page-it is difficult to say it. Check the source code.

Example
Main dashboard page
Site. Master
<% @ Master language = "C #" autoeventwireup = "true" codefile = "site. master. cs" inherits = "Site" %>
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "head1" runat = "server">
<Title> repeat ASP. NET 2.0 (C #) </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: contentplaceholder id = "contentplaceholder1" runat = "server">
</ASP: contentplaceholder>
</Div>
</Form>
</Body>
</Html>

Secondary dashboard page
Masterpage/masterpage. Master
<% @ Master language = "C #" masterpagefile = "~ /Site. Master "autoeventwireup =" true"
Codefile = "masterpage. master. cs" inherits = "masterpage_masterpage" %>
<Asp: Content ID = "content1" contentplaceholderid = "contentplaceholder1" runat = "server">
<P>
I am a nested master page
</P>
<P>
Content on the master page
<Asp: dropdownlist id = "ddlmaster" runat = "server" performanceid = "xmlperformance1" datatextfield = "text"
Datavaluefield = "value" autopostback = "true" onselectedindexchanged = "ddlmaster_selectedindexchanged">
</ASP: dropdownlist> <asp: xmldatasource id = "xmlperformance1" runat = "server" datafile = "~ /Config/dropdownlistdata. xml ">
</ASP: xmldatasource>
</P>
<P>
Content on the Content Page
<Asp: contentplaceholder id = "CPH" runat = "server"/>
</P>
</ASP: content>

Masterpage/masterpage. master. CS
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

Public partial class masterpage_masterpage: system. Web. UI. masterpage
{
Protected void page_load (Object sender, eventargs E)
{

}

/** //


// set the selected index of ddlmaster
// this method is called on the content page
/ ///
///
Public void setddlmaster (INT index)
{< br> ddlmaster. selectedindex = index;
}

Protected void ddlmaster_selectedindexchanged (Object sender, eventargs E)
{
// The selectedindexchanged_ddlmaster event is triggered after the selected index of the ddlmaster is changed.
Selectedindexchanged_ddlmaster (this, new commandeventargs (ddlmaster. selecteditem. Text, ddlmaster. selectedvalue ));
}

// Declare a public time event for the content page to use
Public event commandeventhandler selectedindexchanged_ddlmaster;
}

Content Page
Masterpage/test. aspx
<% @ Page Language = "C #" masterpagefile = "~ /Masterpage. Master "autoeventwireup =" true"
Codefile = "test. aspx. cs" inherits = "masterpage_test" Title = "masterpage test" %>

<% -- create a strong type reference to the master page and specify the virtual path to the master page -- %>
<% @ mastertype virtualpath = "~ /Masterpage. master "%>

datavaluefield = "value" autopostback = "true" onselectedindexchanged = "success">



masterpage/test. aspx. CS
using system;
using system. data;
using system. configuration;
using system. collections;
using system. web;
using system. web. security;
using system. web. ui;
using system. web. UI. webcontrols;
using system. web. UI. webcontrols. webparts;
using system. web. UI. htmlcontrols;

Public partial class masterpage_test: system. web. UI. page
{< br> protected void page_load (Object sender, eventargs E)
{< br> // on the Content Page, use the findcontrol method to find the control on the content page
dropdownlist DDL = new dropdownlist ();
DDL = Master. master. findcontrol ("contentplaceholder1 "). findcontrol ("CPH "). findcontrol ("ddlpage") as dropdownlist;
master. master. findcontrol ("contentplaceholder1 "). findcontrol ("CPH "). controls. add (New literalcontrol ("
the clientid of dropdownlist on the Content Page is:" + DDL. clientid);

// Add an event for processing. This event is a public event defined on the master page.
Master. selectedindexchanged_ddlmaster + = new commandeventhandler (master_selectedindexchanged_ddlmaster );
}

Void master_selectedindexchanged_ddlmaster (Object sender, commandeventargs E)
{
// Commandeventargs has been specified in the public event "selectedindexchanged_ddlmaster" on the master page
String selectedtext = E. commandname;
String selectedvalue = E. commandargument. tostring ();

Ddlpage. selectedvalue = selectedvalue;
}

protected void ddlpage_selectedindexchanged (Object sender, eventargs e)
{< br> // call the master page
master. setddlmaster (ddlpage. selectedindex);
}< BR >}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.