Saturday, March 31, 2012

server side includes in .net

I'm having a hard time figuring out the .net way to emulate server side includes. I'm aware of the fact that you need to work with user controls via .ascx files. This works fine for me as long as my included file (.ascx) is straight html. My problem I assume is in my page directives. I have an index.aspx page, a index.vb code-behind page, and a header.ascx page. Here's what I have:

index.aspx - top of page:
<%@dotnet.itags.org. Page language="vb" Inherits="index" src="http://pics.10026.com/?src=index.vb" %>
<%@dotnet.itags.org. Register TagPrefix="uctl1" TagName="header" src="http://pics.10026.com/?src=includes\header.ascx" %>
index.aspx -body:
<uctl1:header runat="server" /
header.ascx - top of page:
<%@dotnet.itags.org. Control Language="VB" EnableViewState="False" %>
header.ascx - body:
<asp:label id="todaysDate" runat="server" /
index.vb - top of page:
nothing (as far as page directives go. I am importing appropriate vb classes, fyi)
code that pertains to date calc:

protected withEvents lblTodaysDate as label

Sub Page_Load ( s as Object, e as EventArgs )
Dim currentDate as DateTime
currentDate=DateTime.Now
lblTodaysDate.text=currentDate.AddHours(3).toString("MMMM d")

End Sub

my errors pertain to the header & code-behind pages not recognizing one another as far as the date label go. Should i be inheriting one page from the other or something?You need to make the todaysDate label public or provide access to it through a public property. See this article:

http://aspalliance.com/articleViewer.aspx?aId=67&pId=-1
It's not your page that should have this codebehind, it's your user control.
Remove the src of your page (except if you intend to put some code there later) and transfer your codebehind to header.ascx.vb. Set the src attribute of your <%@. Control %> directive to header.ascx.vb.
Finally, give the label the same ID that you have in the ascx file (and not todaysDate in one and lblTodaysDate in the other).

No comments:

Post a Comment