To solve this problem, first understand one thing. masterpage is just a container that will load a lot of web pages, where the Div is placed, and try to let masterpage know. For example, the P1.aspx page contains a Div id of Div1, and P2.aspx also has a Div id of Div1. Masterpage does not know whether the Div1. Div of the page you want to display only needs to be displayed or not. No matter who controls it, it can be controlled by the page or masterpage.
Therefore, you can write an interface with a method and a parameter:
ISetable
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
/// <Summary>
/// Summary description for ISetable
/// </Summary>
Namespace Insus. NET
{
Public interface ISetable
{
Void Setting (bool show );
}
}
If the Div of P1 needs to be controlled, implement this interface in P1.aspx. cs. Of course, if P2.aspx is used, it is the actual interface of P2.aspx. cs.
View Code
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using Insus. NET;
Public partial class P1: System. Web. UI. Page, ISetable
{
Protected void Page_Load (object sender, EventArgs e)
{
}
Public void Setting (bool show)
{
This. Div1.Visible = show;
}
}
Finally, you can determine who controls it. If it is masterpage or page, write it in the button Click event of the container:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using Insus. NET;
Public partial class MasterPage: System. Web. UI. MasterPage
{
Protected void Page_Load (object sender, EventArgs e)
{
}
Protected void button#click (object sender, EventArgs e)
{
ISetable d = (ISetable) this. Page;
D. Setting (true );
}
} C: \ Documents and Settings \ Administrator \ Local Settings \ Temporary Internet Files \ Content. IE5 \ 0UWRZ0I1 \ expandedblockstart1_12.16.gif
Demo: http://www.bkjia.com/uploadfile/2012/0310/20120310083824499.rar
The following content is added on: 10:
The reconstruction is as follows, and the two buttons are integrated into one. effect :.
Click Event:
View Code
Protected void button#click (object sender, EventArgs e)
{
Button btn = (Button) sender;
ISetable d = (ISetable) this. Page;
Switch (btn. Text)
{
Case "open ":
Btn. Text = "off ";
D. Setting (true );
Break;
Case "off ":
Btn. Text = "open ";
D. Setting (false );
Break;
}
}
From Insus. NET