Click here to Skip to main content
15,867,141 members
Articles / Web Development / ASP.NET
Article

Creating a Common Toolbar in ASP.NET 2.0 using MasterPage, Delegate and Events.

Rate me:
Please Sign up or sign in to vote.
4.37/5 (35 votes)
28 Aug 2006CPOL3 min read 120.5K   1.1K   60   26
How to create a common toolbar for using with all pages in an asp.net application.

Introduction

This article describes how to create a common toolbar for all pages in a website using master page. If a web application has common set of actions in many web pages such as Save, Export or Print we can use a common tool bar across all the pages. It gives consistency in look and feel as well as implementation. Before going to deep of the application it is worth to look into new asp.net 2.0 Object hierarchies of page framework when using with MasterPage. Following diagram shows object hierarchies would look like at runtime.

Sample screenshot

At this point, the page and master page are two separate objects, each with their own children. When it comes time for the master page to do its job, the master page replaces the page’s children with itself.

Sample screenshot
The master page’s next step is to look for Content controls in the controls formerly associated with the page. When the master page finds a Content control that matches a ContentPlaceHolder, it moves the controls into the matching ContentPlaceHolder. In our sample program, the master page will find a match for ContentPlaceHolder1, and copy over the Label. Now, master page is like any other control in the page. When request for this page, first the page load of content page will fire and then the master page’s load event. This is the key point in common toolbar application.

Now let us continue with our application. We can go forward step by step.

Step 1: Creating a master page and designing toolbar with events

a) Start with new website in Visual Studio 2005 and add new master page as ToolBarMasterPage.master and add a web page as Page1.aspx with masterpage.

Sample screenshot
b) Place three buttons in the master page as shown below;

Sample screenshot

c) Declare a delegate for handle all the events from common toolbar.

C#
public delegate void ToolBarDelegate(object sender, EventArgs e); 

This declaration defines a delegate named ToolBarDelegate, which will encapsulate any method that takes object and EventArgs as two parameters and returns nothing.

d) Declare events and delegate for each toolbar triggers

C#
public event ToolBarDelegate SaveEvent;
ToolBarDelegate SaveFun; 
C#
public event ToolBarDelegate ExportEvent;
ToolBarDelegate ExportFun; 
C#
public event ToolBarDelegate PrintEvent;
ToolBarDelegate PrintFun;
The above code declares the events and delegates instances for each controls in the toolbar.

e) Raise Events from  Master page.

C#
protected void btnSave_Click(object sender, EventArgs e)
{
if (SaveFun!= null)
SaveFun(sender, e);
}
C#
protected void btnExport_Click(object 
sender, EventArgs e)
{
if (ExportFun != null)
ExportFun(sender,e);
}
C#
protected void btnPrint_Click(object sender, EventArgs e)
{
if 
(PrintFun != null)
PrintFun(sender, e); 
}

The above code demonstrates how to raise events from master page when common toolbar items trigger an event.

Step 2 : Create a Content page to use Common toolbar.

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#
ToolBarMasterPage toolbarMasterPage = (ToolBarMasterPage)this.Master; 

c) Add handler to the Mastepage events.

C#
toolbarMasterPage.SaveEvent += new ToolBarDelegate(SaveData);
C#
toolbarMasterPage.ExportEvent += new ToolBarDelegate(ExportData);
C#
toolbarMasterPage.PrintEvent += new ToolBarDelegate(PrintData);

d) Implement the handler methods.

C#
private void SaveData(object sender, EventArgs e)
{
//Write Code for saving data.
lblInfo.Text = "Saving.....";
lblInfo.Visible = true;
}
C#
private void ExportData(object sender, EventArgs e)
{
//Write code for Exporting data.
lblInfo.Text = "Exporting.....";
lblInfo.Visible = true;
}
C#
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.

Sample screenshot

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.

Sample screenshot

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.

C#
toolbarMasterPage.ExportEvent += new ToolBarDelegate(ExportData); 

The below diagram shows the toolbar with disabled ‘Export’ button.

Sample screenshot

This happens automatically because of this line of code in the paga_load of masterpage.

C#
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
KPMG
United States United States
My passion is to learn, analyze, design (OOAD), architect, code and work with all team members to create competitive products in the market using efficient ways.

Interested area are .NET, MVC, client-side development, OData, SQL Server BI Solutions( SSAS & SSRS) ,PHP, MySQL, Visual Studio Automation&Performance Testing, Agile development and architect Frameworks and application integration solutions

Comments and Discussions

 
Generalget an error message Pin
Md. Mahfujul28-Dec-10 22:30
Md. Mahfujul28-Dec-10 22:30 
GeneralUse the code attached to the article Pin
Alvin Geo18-May-12 7:50
Alvin Geo18-May-12 7:50 
GeneralA page can have only one server-side Form tag Pin
rajeshswami2728-Jul-09 22:42
rajeshswami2728-Jul-09 22:42 
GeneralEnable disable buttons Pin
Member 41966329-Jul-09 21:40
Member 41966329-Jul-09 21:40 
Generalreturning null Pin
Member 41966328-Jul-09 20:21
Member 41966328-Jul-09 20:21 
Questionhow to use with User Controls Pin
Member 27598111-Oct-08 6:51
Member 27598111-Oct-08 6:51 
GeneralExellent article Pin
middo23-Sep-08 4:07
middo23-Sep-08 4:07 
GeneralGood and Very useful article Pin
MuraliNatarajan21-Sep-08 21:11
MuraliNatarajan21-Sep-08 21:11 
GeneralExcellent Code Pin
Jothikumar.M10-Jan-08 19:05
Jothikumar.M10-Jan-08 19:05 
Generalusing masterpage on mono 2.0 [modified] Pin
brynhood26-Nov-07 0:53
brynhood26-Nov-07 0:53 
QuestionInstead of putting button in Master Page, if i am using UC then What? Pin
Joshi, Rushikesh29-May-07 19:32
professionalJoshi, Rushikesh29-May-07 19:32 
GeneralDelegate is returning null to me Pin
Pradeep K V20-Jan-07 5:10
Pradeep K V20-Jan-07 5:10 
GeneralCheck your code again. Pin
Alvin George21-Jan-07 17:24
Alvin George21-Jan-07 17:24 
GeneralVB Pin
turseppaja22-Nov-06 5:23
turseppaja22-Nov-06 5:23 
GeneralRe: VB Pin
Ather Ali Shaikh30-Jan-07 2:46
professionalAther Ali Shaikh30-Jan-07 2:46 
AnswerRe: VB Pin
Ather Ali Shaikh30-Jan-07 3:27
professionalAther Ali Shaikh30-Jan-07 3:27 
GeneralRe: VB Pin
Alvin George5-Apr-07 22:08
Alvin George5-Apr-07 22:08 
QuestionHow to use postbackurl with this technique? Pin
sonnyjim1-Nov-06 13:54
sonnyjim1-Nov-06 13:54 
AnswerRe: How to use postbackurl with this technique? [modified] Pin
Alvin George1-Nov-06 17:19
Alvin George1-Nov-06 17:19 
QuestionRe: How to use postbackurl with this technique? Pin
mmengel1-May-07 15:57
mmengel1-May-07 15:57 
GeneralGood Pin
Asween25-Aug-06 0:22
Asween25-Aug-06 0:22 
GeneralHelped me a lot Pin
StefanSc24-Aug-06 2:30
professionalStefanSc24-Aug-06 2:30 
GeneralRe: Helped me a lot Pin
Alvin George24-Aug-06 4:15
Alvin George24-Aug-06 4:15 
Generalhai Pin
tintokthomas200516-Aug-06 22:30
tintokthomas200516-Aug-06 22:30 
GeneralGood article Pin
Magnus Salgo22-Aug-06 21:54
Magnus Salgo22-Aug-06 21:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.