您的位置:首页 > 编程语言 > ASP

使用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.

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: