In this article I will you that how you can create "Base Pages" to supplement the rest of your ASP.NET pages in your application.

Introduction:

In this article I will you that how you can create "Base Pages" to supplement the rest of your ASP.NET pages in your application.

What is a base page?

There is nothing special about a base page since it is a simple page which inherits from the System.Web.UI.Page class. What differs is that how you can define various methods and properties inside the base page so that it can be used by other pages.

Creating a base page:

Creating a base page is pretty simple. Just add a Webform which inherits from System.Web.UI.Page class. Remove the Page_Load method since you will only use the base page to provide different functionality to other pages. The code below shows the empty base page.

public partial class BasePage : System.Web.UI.Page

{    }

We will add several methods to our base page later in this article.

Creating a page that inherits from the base page:

Simply, add a new Webform and inherits it from the base page instead of the System.Web.UI.Page. Take a look at the code below:

public partial class _Default : BasePage

{       }

As, you can see in the above code that the Webform, Default inherits from the BasePage instead of the System.Web.UI.Page. Since, BasePage inherited from System.Web.UI.Page class so, the new Webform will automatically get all the features of the Page class.

Using the base page:

Now, let's see a common use of the base page. Let's say you have a GridView control on your Webform. And you to make hyperlinks inside the GridView control in such a way that when you click the Hyperlink it goes to a certain page and carries the id of the record with it. You can easily implement this inside the code behind of your Webform but then it will only be accessible to that particular page. In order to make it accessible by all the pages you can simply put this inside the BasePage class which is inherited by all the pages. By making the method visible to all the pages all pages can use the same method instead of defining their own personal methods.  

Let's see the GridView HTML code so that you will have better idea:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"

ForeColor="#333333" GridLines="None">

<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<Columns>

<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />

<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />

<asp:TemplateField HeaderText="Url">

<ItemTemplate>

<asp:HyperLink ID="hlLink" NavigateUrl='<%# (string) FormatUrl( (int) Eval("CategoryID") ) %>' runat="server">Click here</asp:HyperLink>

</ItemTemplate>

</asp:TemplateField>

</Columns>

<RowStyle BackColor="#EFF3FB" />

<EditRowStyle BackColor="#2461BF" />

<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<AlternatingRowStyle BackColor="White" />

</asp:GridView>

I have made the HyperLink line bold since it contains the FormatUrl method. The purpose of the FormatUrl method is to create a url which is dependent on the CategoryID. It is like sending values to other pages using QueryString.

Here is an example of the url which is generated by the FormatUrl method:

CategoryDetails.aspx?categoryID=23

Now, let's see the FormatUrl method which is defined in the BasePage class.

private readonly string CATEGORY_DETAILS = "CategoryDetails.aspx?categoryID=";

private readonly string ERROR_PAGE = "ErrorPage.aspx";

protected string FormatUrl(int categoryID)

{

if (categoryID > 0)

return CATEGORY_DETAILS + categoryID;

else return ERROR_PAGE;

}

Simple enough! Since the method is marked protected it can be accessed by the derived class which in this case is "Default". You can also use the same technique with User Controls.

What about JavaScript? 

For using JavaScript functions you need to inject the code on the fly into the inherited pages. In your BasePage class you can create a method whose sole purpose is to inject the JavaScript to other pages. Check out the InjectJavaScript method below:

private readonly string scriptString = @"<script language = JavaScript>

function SayHello() { alert('Hello World'); }

</script> ";

protected void InjectJavaScript(){

if (!Page.ClientScript.IsClientScriptBlockRegistered("script"))

{

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", scriptString); }

}

Now, you can inject the JavaScript in your inherited pages by calling the InjectJavaScript method:

protected void Page_Load(object sender, EventArgs e)

{

// Inject the JavaScript

InjectJavaScript();

if (!Page.IsPostBack)

{

BindData();

}

}

You can call the SayHello() method from the client side as shown in the code below:

<input type="button" id="MyButton" value="Press Me" onclick="SayHello();"/>

In this article you learned that how you can create base pages to supplement other pages in your application. Base pages help us to easily maintain the common functionality of the pages and makes our modification task easier.

If you are one of the thousands that visit GridViewGuy for your .NET articles and resources, you might be interested in making a donation. Extra cash helps pay for the hosting services and speed things up around here, and makes this website possible.

Make a Donation

Once, again thank you very much and remember its because of you FINE people that this website is up and running.

 

Export Button is a custom control that let's you export your DataGrid or TextBox data to several different formats. The control is extremely easy to use and also exposes design time features. In this article I will discuss some of the features of the Export Button and how it benefits the developer.

BUY IT NOW