Jump to content

Master Page Method: Code-Behind for Content Page


kwilliams

Recommended Posts

I'm using the Master Page method on my site, but I'm having a problem with using the content page's code-behind doc (VB). If I place a block of code in the Master Page's code-behind file, it works great. But if I place that same block of code in the content page's code-behind page, it doesn't work.I thought that I had everything set up properly, but obviously the content's CB is not being called properly from the content page. Below is my code. Please let me know what I'm doing wrong. Thanks.Master Code-Behind Page(MasterPage.master.vb)

Partial Class MasterPage	Inherits System.Web.UI.MasterPage	Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)		'Assign path to FileInfo Class		Dim strXMLPath_mc As String, strXSLPath_mc As String		strXMLPath_mc = "docs/xml/content.xml" 'xml doc			strXSLPath_mc = "docs/xslt/content.xsl" 'xsl doc		'Assign maincolumn XML/XSLT transformation properties		xslTransform_mc.DocumentSource = strXMLPath_mc		xslTransform_mc.TransformSource = strXSLPath_mc	End SubEnd Class

Master Page (MasterPage.master)

<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" Explicit="True" Debug="True" %><%@ Import Namespace="System" %><%@ Import Namespace="System.IO" %><%@ Import Namespace="System.Xml" %><%@ Import Namespace="System.Xml.XPath" %><%@ Import Namespace="System.Xml.Xsl" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head></head><body>	<div id="content">		<asp:Xml id="xslTransform_mc" runat="server"></asp:Xml>	</div><!-- end content --></body></html>

Content Page (content.aspx)

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" CodeFile="content.aspx.vb" Inherits="content" AutoEventWireup="false" title="Content Page" %>

Content Code-Behind Page (content.aspx.vb)

Partial Class content	Inherits System.Web.UI.Page	Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)		If Not Page.IsPostBack Then			'Create the XsltArgumentList object			Dim args As New XsltArgumentList()			'Declare form variables			Dim category_form As String = Request.Form("txtCategory")			Dim fullname_form As String = Request.Form("txtFullName")			'Declare XsltArguments			If category_form <> "" Then				args.AddParam("xslt_category", "", category_form)			End If			If fullname_form <> "" Then				args.AddParam("xslt_fullname", "", fullname_form)			End If		End If	End SubEnd Class

XML doc (content.xml)

<?xml version="1.0" encoding="ISO-8859-1"?><content>	<field id="category">		<name>txtCategory</name>		<title>Category</title>		<type>text</type>		<maxlength>40</maxlength>		<size>20</size>		<required>false</required>	</field>		<field id="fullname">		<name>txtFullName</name>		<title>Full Name</title>		<type>text</type>		<maxlength>40</maxlength>		<size>20</size>		<required>false</required>	</field></content>

XSLT doc (content.xsl)

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="xml" encoding="UTF-8" indent="yes"/><xsl:variable name="currentpath">http://www.mysite.com/content.aspx</xsl:variable><xsl:param name="xslt_category" select="''" /><xsl:param name="xslt_fullname" select="''" />	<xsl:template match="/">		<form name="formFeedback" action="{$currentpath}" method="post">			<xsl:for-each select="content/field">				<input id="{@id}" name="{name}" title="{title}" type="{type}" maxlength="{maxlength}" size="{size}" value="" /><br />			</xsl:for-each>	</xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

can you give an example of that "block of code" and what errors do you get?
Sure. This is the block of code:
Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)		'Assign path to FileInfo Class		Dim strXMLPath_mc As String, strXSLPath_mc As String		strXMLPath_mc = "docs/xml/content.xml" 'xml doc			strXSLPath_mc = "docs/xslt/content.xsl" 'xsl doc		'Assign maincolumn XML/XSLT transformation properties		xslTransform_mc.DocumentSource = strXMLPath_mc		xslTransform_mc.TransformSource = strXSLPath_mc	End Sub

and no error are returned. What I did is place code in the XSLT doc to display whether or not the XSLT parameters were returned, like this:$xslt_category: <xsl:value-of select="$xslt_category" /><br />$xslt_fullname: <xsl:value-of select="$xslt_fullname" /><br />When I enter "General" into the category form field, and "Kat" info the FullName field, this is what's returned with the Master Page version:$xslt_category: General$xslt_fullname: Kat...but when that same block of code is placed in the Content Page's CB page, no results are returned.$xslt_category:$xslt_fullname:Hopefully that helps to explain things better. Thanks for any help.

Link to comment
Share on other sites

I haven't really had that much of an opportunity to play around with Master pages yet, but I think the problem here is that there's nothing on the Master page indicating that it should be displaying anything from any of the content pages.Take a look at this article:http://msdn.microsoft.com/en-us/library/wtxbf3hh.aspxIt is basically saying that, in your master page, you need something like this:

<asp:ContentPlaceHolder id="Main" runat="server" />

Then, in the content page, you need this:

<asp:Content id="MainContent" ContentPlaceHolderID="Main" runat="server">	This is the content from the content page</asp:Content>

Link to comment
Share on other sites

I haven't really had that much of an opportunity to play around with Master pages yet, but I think the problem here is that there's nothing on the Master page indicating that it should be displaying anything from any of the content pages.Take a look at this article:http://msdn.microsoft.com/en-us/library/wtxbf3hh.aspxIt is basically saying that, in your master page, you need something like this:
<asp:ContentPlaceHolder id="Main" runat="server" />

Then, in the content page, you need this:

<asp:Content id="MainContent" ContentPlaceHolderID="Main" runat="server">	This is the content from the content page</asp:Content>

I was thinking the same thing, but since the codebehind page is being called from the content.aspx page in its page declaration, shouldn't that automatically load the CB code?
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...