11/20/2007

strongly typed master pages in asp.net 2.0

Introduction:
ASP.NET 2.0 supports master pages to allow for the Templated creation of webpages. In this technique we will create a masterpage which will contain the basic template of all the webpages in that website. This template will contain the content placeholders. These content placeholders will be filled by the containing aspx pages.
In order to refer master page inside an aspx page we will place <%@ MasterType VirtualPath="~/MasterPage.master" %> at the top of the aspx page.
This will work only if we have single master page for the website since we are hardcoding the virtual path. What if we need to change the master page dynamically? In these situations we can use strongly-typed master pages. In that way we could define all the properties and methods available in all the master pages in the basemasterpage class and access those properties in the content pages without any issues.

Following is the description of how we could do that.
The following code is self explanatory.

//step1 create the class BaseMasterPage.vb in the App_code folder of the website
Public Class BaseMasterPage
Inherits MasterPage
Public Overridable Property CompanyName() As String
Get
Return String.Empty
End Get
Set(ByVal value As String)

End Set
End Property
End Class

//step2 create the master page that inherits from the base masterpage.
// mark up MasterPage.master
<%@ Master Language="VB" Inherits="MasterPage" CodeFile="~/masterPage.master.vb" %>



// code behind
Partial Class MasterPage
Inherits BaseMasterPage

Public Overrides Property CompanyName() As String
Get
Return hCompanyName.Value
End Get
Set(ByVal value As String)
hCompanyName.Value = value
End Set
End Property
End Class

//step3 create content page
//MasterPageTest.aspx
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="MasterPageTest.aspx.vb" Inherits="MasterPageTest" title="Untitled Page" %>
<%@ MasterType TypeName="BaseMasterPage" %>

Test


//code behind

Partial Class MasterPageTest
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
//access masterpage property CompanyName
Dim Company As String = Master.CompanyName
End Sub
End Class

11/10/2007

crystal reports in asp.net 2.0 website

In ASP.NET 1.1 when we add a crystal report to the web application, it will create a class file for that report. so when we deploy the web application this class file is included in the dll. so whenever we need to make changes to the crystal report we have to deploy the whole application.This is not the case with asp.net 2.0 website model. In asp.net 2.0 website model the crystal report is treated as an external file. The reason is to be able to deploy the changes to the crystal report with out deploying the whole site.
Following is the code used to display the crystal report in asp.net 2.0 website.

Dim myReport as CrystalDecisions.CrystalReports.Engine.ReportDocument
myReport = new CrystalDecisions.CrystalReports.Engine.ReportDocument()
myReport.Load(Server.Mappath("testreport.rpt"))
CrystalReportViewer1.ReportSource = myReportDocument
'get the data from database.
Dim dt as datatable = GetData()
myReport.SetDataSource(dt)
myReport.ReportSource = myReport
CrystalReportViewer1.DataBind()
CrystalReportViewer1.DisplayToolbar = false