使用MasterPage,Event,Delegate在ASP.NET2.0 创建公用工具栏
2006-08-24 10:40
387 查看
介绍:
这是一篇描述在一个Web站点如何使用MasterPage来创建一个所有页面公用的工具栏。如果一个Web应用程序在许多个页面都用通用的操作,例如保存,导出,打印等,我们就需要工具栏。这样就能给人一种亲密的感觉。在深入这个程序之前,值得我们花点时间来研究一下在使用MasterPage时新的ASP.NET2.0 Page框架的对象层次。接下来的图表展示了运行时的兑现层次。
在这点上,Page和Master Page是连个单独的对象,有各自的子对象。当用Master Page来做他的工作的时候,Master Page就取代了Page的子对象以及它自己。
Master Page的下一步是寻找在以前和Page关联的对象。当Master Page找到合适的匹配ContentPlaceHolder的控件,就把控件移动到ContentPlaceHolder上。在例子中,Master Page将包含 ContentPlaceHolder1,以及一个Label。现在MasterPage像Page中的其他控件一样,当请求这个网页的时候,首先加载内容页面,然后加载Master Page的事件。在公共工具栏应用程序中这是关键点。
现在我们继续我们的程序,一步一步来
STEP 1 创建一个Master Page和设计带事件的工具栏
a)用VS200创建一个Web站点,添加一个名为ToolBarMasterPage的MasterPage,添加一个名为Page1.ASPX的网页(带母板)
b)在母板上添加三个按钮,如下:
c)生命一个Delegate来处理公用工具栏的事件
public delegate void ToolBarDelegate(object sender, EventArgs e);
这里声明了以俄国名为ToolBarDelegate的代理,处理任何包含Object和EventArgs两个参数和五返回值得方法
d)为每个工具栏触发器声明时间和代理
public event ToolBarDelegate SaveEvent;
ToolBarDelegate SaveFun;
public event ToolBarDelegate ExportEvent;
ToolBarDelegate ExportFun;
public event ToolBarDelegate PrintEvent;
ToolBarDelegate PrintFun
e)从Master Page抛出事件
protected void btnSave_Click(object sender, EventArgs e)
{
if (SaveFun!= null)
SaveFun(sender, e);
}
protected void btnExport_Click(object
sender, EventArgs e)
{
if (ExportFun != null)
ExportFun(sender,e);
}
protected void btnPrint_Click(object sender, EventArgs e)
{
if
(PrintFun != null)
PrintFun(sender, e);
}
STEP2:使用公用工具栏创建一个内容页
a) Place a label control in the Pagel.aspx and give value to the ID as lblInfo
b) In the page load method get the MasterPage object of the content page.
c) Add handler to the Mastepage events.
d) Implement the handler methods.
The above methods are actual implementaion of toolbar actions.
b) Run the application and Click the ‘Save’ Button of the common tool bar on the master page area. This will display ‘Saving... ’ text in the content page area. Picture below shows this.
The below diagram shows the toolbar with disabled ‘Export’ button.
This happens automatically because of this line of code in the paga_load of masterpage.
I hope this article will help you to create a useful toolbar with common actions for your ASP.NET 2.0 applications.
这是一篇描述在一个Web站点如何使用MasterPage来创建一个所有页面公用的工具栏。如果一个Web应用程序在许多个页面都用通用的操作,例如保存,导出,打印等,我们就需要工具栏。这样就能给人一种亲密的感觉。在深入这个程序之前,值得我们花点时间来研究一下在使用MasterPage时新的ASP.NET2.0 Page框架的对象层次。接下来的图表展示了运行时的兑现层次。
在这点上,Page和Master Page是连个单独的对象,有各自的子对象。当用Master Page来做他的工作的时候,Master Page就取代了Page的子对象以及它自己。
Master Page的下一步是寻找在以前和Page关联的对象。当Master Page找到合适的匹配ContentPlaceHolder的控件,就把控件移动到ContentPlaceHolder上。在例子中,Master Page将包含 ContentPlaceHolder1,以及一个Label。现在MasterPage像Page中的其他控件一样,当请求这个网页的时候,首先加载内容页面,然后加载Master Page的事件。在公共工具栏应用程序中这是关键点。
现在我们继续我们的程序,一步一步来
STEP 1 创建一个Master Page和设计带事件的工具栏
a)用VS200创建一个Web站点,添加一个名为ToolBarMasterPage的MasterPage,添加一个名为Page1.ASPX的网页(带母板)
b)在母板上添加三个按钮,如下:
c)生命一个Delegate来处理公用工具栏的事件
public delegate void ToolBarDelegate(object sender, EventArgs e);
这里声明了以俄国名为ToolBarDelegate的代理,处理任何包含Object和EventArgs两个参数和五返回值得方法
d)为每个工具栏触发器声明时间和代理
public event ToolBarDelegate SaveEvent;
ToolBarDelegate SaveFun;
public event ToolBarDelegate ExportEvent;
ToolBarDelegate ExportFun;
public event ToolBarDelegate PrintEvent;
ToolBarDelegate PrintFun
e)从Master Page抛出事件
protected void btnSave_Click(object sender, EventArgs e)
{
if (SaveFun!= null)
SaveFun(sender, e);
}
protected void btnExport_Click(object
sender, EventArgs e)
{
if (ExportFun != null)
ExportFun(sender,e);
}
protected void btnPrint_Click(object sender, EventArgs e)
{
if
(PrintFun != null)
PrintFun(sender, e);
}
STEP2:使用公用工具栏创建一个内容页
a) Place a label control in the Pagel.aspx and give value to the ID as lblInfo
b) In the page load method get the MasterPage object of the content page.
ToolBarMasterPage toolbarMasterPage = (ToolBarMasterPage)this.Master;
c) Add handler to the Mastepage events.
toolbarMasterPage.SaveEvent += new ToolBarDelegate(SaveData);
toolbarMasterPage.ExportEvent += new ToolBarDelegate(ExportData);
toolbarMasterPage.PrintEvent += new ToolBarDelegate(PrintData);
d) Implement the handler methods.
private void SaveData(object sender, EventArgs e) { //Write Code for saving data. lblInfo.Text = "Saving....."; lblInfo.Visible = true; }
private void ExportData(object sender, EventArgs e) { //Write code for Exporting data. lblInfo.Text = "Exporting....."; lblInfo.Visible = true; }
private void PrintData(object sender, EventArgs e) { //Write code for Printing Data. lblInfo.Text = "Printing....."; lblInfo.Visible = true; }
The above methods are actual implementaion of toolbar actions.
Step 3 : Run the application.
a) Right click the Page1.aspx and click set as start page option. This will display the page as shown in the following picture.b) Run the application and Click the ‘Save’ Button of the common tool bar on the master page area. This will display ‘Saving... ’ text in the content page area. Picture below shows this.
Step 4 : Disabling a Toolbar control in any page.
a) if we remove or comment below line from the contente page page_load method the ‘Export’ button of in the tool bar will automatically disabled.toolbarMasterPage.ExportEvent += new ToolBarDelegate(ExportData);
The below diagram shows the toolbar with disabled ‘Export’ button.
This happens automatically because of this line of code in the paga_load of masterpage.
if (ExportFun == null) btnExport.Enabled = false;
I hope this article will help you to create a useful toolbar with common actions for your ASP.NET 2.0 applications.
相关文章推荐
- 鸡啄米vc++2010系列36(工具栏的创建、停靠与使用)
- libevent学习笔记【使用篇】——2. 获取(创建)event_base
- delegate和event的使用方法(收藏笔记)
- 使用C#创建自定义背景色/形状的菜单栏与工具栏
- go语言使用go-sciter创建桌面应用(六) Element元素操作和Event事件响应
- [原]使用Win API创建工具栏
- Func与Action, delegate, event, var, dynamic, 匿名方法,lambda, 大量的关键都使用相同功能,大大增加C#复杂性
- 【Unity3D技巧】在Unity中使用事件/委托机制(event/delegate)进行GameObject之间的通信 (二) : 引入中间层NotificationCenter
- C#中Event和Delegate的混合使用
- 在Unity中使用事件/委托机制(event/delegate)进行GameObject之间的通信
- [置顶] 【设计模式】使用unity实现观察者模式(delegate,event)
- mysql 数据库Event定时任务使用详解(Navicat 及直接SQL语句创建)
- 使用Win API创建工具栏
- ASP.NET2.0快速入门--使用母版页创建布局
- 【Unity3D技巧】在Unity中使用事件/委托机制(event/delegate)进行GameObject之间的通信 (二) : 引入中间层NotificationCenter
- Unity3D技巧在Unity中使用事件/委托机制(event/delegate)进行GameObject之间的通信
- 第十二章 使用工具栏创建简单的多视图应用程序
- 使用Win API创建工具栏
- 在 Azure 中使用公用 IP 创建多 NIC VM
- ASP.NET2.0快速入门--使用母版页创建布局