Click to See Complete Forum and Search --> : Master Page Method: Code-Behind for Content Page


kwilliams
05-15-2008, 02:42 PM
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 Sub
End 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 Sub
End 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>

chazzy
05-15-2008, 05:51 PM
I'm not overly familiar with the Master page concept, but I think you shouldn't have a Page_Load declaration in the file. Maybe you're mixing the concept of inheritance (more about code reuse) with Master page (more about layout). Does it work if you remove the master page's code?

kwilliams
05-28-2008, 10:15 AM
Hi Chazzy,

Sorry I didn't get back to you with an answer earlier...I got busy with other tasks.

Concerning your question I'm not overly familiar with the Master page concept, but I think you shouldn't have a Page_Load declaration in the file. Maybe you're mixing the concept of inheritance (more about code reuse) with Master page (more about layout). Does it work if you remove the master page's code?

...a Page_Load declaration is already located within the content's code-behind page (please see my code from previous post). I understand that Master Page's are used primarily for page layouts, but they are also used for scripts that need to be used site-wide (i.e. current date). But I don't want to put every script that's needed on the site within the one master page code-behind page for obvious reasons. So since the content page's code-behind page is supposed to be loaded before the master page's code-behind page, I'm not sure why is it not working. If you or anyone can give me some direction, that would be great. Thanks.