Monday, February 20, 2006

Generic Message & Redirect Page Compat w Master Pages

I made a generic Message display page for the site. None of the examples online worked with the master pages functions of 2.0 since the master page has control of the <head> tags & there's no way to declare them from a child without a codebehind function. My goal was to make it so that I could call one method & pass in the messsages on the page via a parameter list.

In your master page, set the <head> tag to <head runat=server> Do not specify an ID= tag or else things will go all squigly on you in the codebehind.

Here's the source for the generic page. Its just a couple labels.

<%@ Page Language="VB" MasterPageFile="~/Common/Master/JobSeeker.master" AutoEventWireup="false" CodeFile="MessageDisplay.aspx.vb" Inherits="MessageDisplay" title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<asp:Label ID="lblHeadline" runat="server" Text="Label"></asp:Label><br />

<asp:Label ID="lblMessage" runat="server" Text="Label"></asp:Label><br />

<asp:HyperLink ID="hlRedirectLink" runat="server">HyperLink</asp:HyperLink>


Here's the Codebehind. It pulls values in from a strongly typed session variable, but you can adapt the code to pass them in with whatever parameter mechanism you prefer.

Partial Class MessageDisplay

Inherits JSfirm.Basepages.JobSeeker.Open

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Me.Title = MySession.DisplayMessage.PageTitle

lblHeadline.Text = MySession.DisplayMessage.Headline

lblMessage.Text = MySession.DisplayMessage.Message

hlRedirectLink.Text = "<a href='" + MySession.DisplayMessage.RedirectURL + "'>Click here</a> to continue if your browser does not automatically direct you."

'Get the htmlHead your aspx page is using (from the Master page)

'/Master page must include the runat server attribute in the head tag: <head runat="server">

'Do not give an ID to the head...

Dim head As HtmlHead = CType(Header, System.Web.UI.HtmlControls.HtmlHead)

'Create a htmlMeta object

Dim meta As HtmlMeta = New HtmlMeta()

'Specify meta attributes

meta.HttpEquiv = "REFRESH"

Dim strContentString As String = "5;URL="

strContentString += MySession.DisplayMessage.RedirectURL

meta.Content = strContentString

'Add the meta object to the htmlhead's control collection


End Sub

End Class


Now in your basepage (you know... the one you should inherit all pages in your project from instead of the, insert this function or something similar. Note that the mysession business is just my strongly typed instance of a session object.

Public Function DisplayMessage(ByVal PageTitle As String, ByVal Headline As String, ByVal Body As String, ByVal redirecturl As String)

MySession.DisplayMessage.PageTitle = PageTitle

MySession.DisplayMessage.Headline = Headline

MySession.DisplayMessage.Message = Body

MySession.DisplayMessage.RedirectURL = redirecturl


Return Nothing

End Function

Voila! Now in any page where you want to redirect you just have one line to call it all. Say for instance, you have a "delete user" button.... Instead of having a boring text... you can do this from your child pages.

DisplayMessage("Delete Message", "User Successfully Deleted!", "lorem ipsum dolar sit mieum paragraph", "/JobSeeker/Default.aspx")

And it will display a message to the user in a new page... but since it transfer's, the url stays the same until the redirect to "/jobseeker/default" occurs.

This is a pretty basic example.... The design could be expanded to have a few overloads for excluding text or redirect links, specifying redirec timeout etc...

This was loosely based off of

as it appered in the Feb 2005 article in Code Magazine. However there are a few large differences between this & theirs. Their implementation does not support meta refresh in a master pages scenerio with a callback to the master page with the redirect url. It was authored before master pages. Theirs is in c#, and theirs has a bunch of coding garbage to help out with relative css links and a very awkward handling of the displaypage method in the template page. Its just a bunch of labels in mine. If you feel that you need to work with the context object to pass the values in, then more power to you... but in my case, i just added a class to my session like this & pass it around.

Public Class MessageInfo

Private _PageTitle As String

Public Property PageTitle() As String


Return _PageTitle

End Get

Set(ByVal value As String)

_PageTitle = value

End Set

End Property

Private _Headline As String

Public Property Headline() As String


Return _Headline

End Get

Set(ByVal value As String)

_Headline = value

End Set

End Property

Private _Message As String

Public Property Message() As String


Return _Message

End Get

Set(ByVal value As String)

_Message = value

End Set

End Property

Private _RedirectURL As String

Public Property RedirectURL() As String


Return _RedirectURL

End Get

Set(ByVal value As String)

_RedirectURL = value

End Set

End Property

End Class


Post a Comment

Subscribe to Post Comments [Atom]

<< Home