Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Sorry about the dumbness of this question. I am doing my first asp.net application and want to do something similar to my current asp web sites which is design a template and use this for every page. I would normally just code an include file so that I can change all pages by changing just the one file but I cant see how this works in asp.net.

 

Basically I am changing my current site http://www.cellmedia.co.uk which only has content changed on the right hand side of the page and the header and content index stay the same through out the site.

 

Could someone just point me in the right direction please.

 

PB

Posted
You could try using this: just replace the filename with the name of your include file :)

 

Response.WriteFile ("Yourfile.inc")

 

Could it really be that simple. I dont know if other people suffer this but I find that I try to use all the new content that asp.net offers rather than the stuff I can easily write like javascript and standard asp. The overlap is the bit that gets me and I think that I should be writing totally new code rather than stuff that I would normally write. A typical example of this is trying to use the .net drag and drop components for page layout when I can easily write the html code instead.

 

PB

Posted

Yeah i get like that too, im writing a project in asp.net at the moment - as part of my companies "do everything in .net from now on" initiative. But sometimes, i find myself just wanting to do the classic asp thing! But i was like that with vb.net and now i love it so maybe time will tell!!!

 

I do like the .net drag and drop though, it was getting my head round the vb or c# code behind page, i kept forgetting to use it!!

Visit: VBSourceSeek - The VB.NET sourcecode library

 

 

"A mere friend will agree with you, but a real friend will argue."
Posted

I've got two different ways that I've done templates. First is similar to ASP includes, and the second is using inheritence.

 

For the first method, I created a custom control with all my navigation elementals. Depending on the user's level, I fill the wrapper with different items, like recent edits, admin links, etc. Then on every page, I just registered the wrapper control, and every page had a span called Main-Body which I positioned with CSS to allow room for the side and header to wrap around it. This worked well, but there was a lot of repetitive code.

 

Now I've started using inheritence to create page templates. It took me a bit of playing around to get working, but now that I have it set up, its a breeze.

 

I created a PageBase class (PageBase.vb) that adds all the header and footer template information, including Body tags and Form tags, then loads the controls from the content page as it gets built (myForm). I built my previous wrapper control into it (myWrapper control), but you could just as easily create the wrapper items within the PageBase class itself.

 

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls

Public Class PageBase
   Inherits System.Web.UI.Page

   Private myForm As HtmlForm
   Private myWrapper as Wrapper

   Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
       BuildPage()
       MyBase.OnInit(e)
   End Sub

   Protected Sub BuildPage()
       Dim newPlaceHolder As PlaceHolder
       Dim iCurrControlNum As Integer = 0
       Dim i as Integer

       myForm = New HtmlForm
       myForm.ID = "objForm"

       'Wrapper
       myForm.Controls.Add(New LiteralControl("<span class='noprint'>"))
       iCurrControlNum += 1
       myForm.Controls.Addat(iCurrControlNum, LoadControl("~/Resource/Global/Wrapper.ascx"))
       iCurrControlNum += 1
       myForm.Controls.Add(New LiteralControl("</span>"))
       iCurrControlNum += 1

       'Pull Content from the requested page
       myForm.Controls.Add(New LiteralControl("<span class='main-body'>"))
       iCurrControlNum += 1
       For i = 0 To Me.Controls.Count - 1
           myForm.Controls.AddAt(iCurrControlNum, Me.Controls(0))
           iCurrControlNum += 1
       Next
       myForm.Controls.Add(New LiteralControl("</span>"))
       iCurrControlNum += 1

       Me.Controls.Clear()

       'HEADER
           Me.Controls.Add(New LiteralControl( _
               "<html> " & vbCrLf & _
               "   <body ID='PageBody'>" & vbCrLf))

       'CONTENT FORM
           Me.Controls.Add(myForm)

       'FOOTER
           Me.Controls.Add(New LiteralControl( _
               "   </body>" & vbCrLf & _
               "</HTML>"))
   End Sub

End Class

 

Now, creating a new page using the template is a snap. All I have to do is make each page inherit the PageBase class instead of System.Web.UI.Page, and it automatically applies the template. The content page needs only the content itself, no form tags, no body tags, no headers.

Posted

Thanks for the reply, Its really strange but I never thought about inheritance in a web form although I probably code stuff like it everyday for a living in my normal programs.

 

Cheers

 

PB

Posted

One drawback that comes to mind w/ the inheritance is you lose the wysiwyg. I also dislike the usage of the codebehinds because the application must be recompiled to make changes where as if it was inlined, the changes are instant. Anyways, an alternative to the inheritance approach, other than using/waiting for asp.net 2.0 is the usage of controls

 

 

e.x.: A login page.

<%@ Register TagPrefix="MyCodeLib" TagName="Header" Src="Header.ascx" %>
<%@ Register TagPrefix="MyCodeLib" TagName="Header" Src="Footer.ascx" %>
<%@ Assembly Name="MyCodeLib" %>
<%@ import Namespace="MyCodeLib"%>
<%@ import Namespace="System.Web.Security"%>
<%@ Page language="c#"%>
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
_errorMessage.Visible = false;
_loginBtn.ToolTip = "Login";
_username.ToolTip = "Enter your username here.";
_password.ToolTip = "Enter your password here.";
}

private void PerformLogin(object sender, System.EventArgs e)
{
if(MyCodeLib.User.Exists(_username.Text, _password.Text))
{
	FormsAuthentication.RedirectFromLoginPage(_username.Text, true);
}
else
{
	_errorMessage.Text = "Invalid username or password.";
	_errorMessage.Visible = true;
}
}
</script>
<html>
<body>
	<form id="Form1" method="post" runat="server">
		<MyCodeLib:Header id="Header" runat="server" />
		<table align="center">
			<tr>
				<td>
					<asp:Label id="_errorMessage" runat="server" visible="false" ForeColor="Red"></asp:Label>
				</td>
			</tr>
		</table>
		<TABLE align="center">
			<tr>
				<td>User Name:</td>
				<td><asp:textbox id="_username" runat="server" MaxLength="255"></asp:textbox></td>
			</tr>
			<tr>
				<td>Password:</td>
				<td><asp:textbox id="_password" runat="server" MaxLength="255" TextMode="Password"></asp:textbox></td>
			</tr>
		</TABLE>
		<table align="center">
			<tr>
				<td>
					<asp:button id="_loginBtn" runat="server" OnClick="PerformLogin" Text="Login"></asp:button></td>
			</tr>
		</table>
		<MyCodeLib:Footer id="Footer" runat="server" />
	</form>
</body>
</html>

 

When the page is rendered, the controls will be displayed.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...