Jump to content

MasterPage Method and Codebehind


kwilliams

Recommended Posts

I'm using the ASP.NET MasterPage method for my new site, that includes a XML/XSLT transformation. Now I'm trying to create a form with XSLT and ASP.NET that uses a XSLT paramater to change the displayed data. I found a MSDN Library item "XSLT Parameters" at http://msdn2.microsoft.com/en-us/library/dfktf882.aspx, and it works great statically. Now I'm trying to implement the same concept on my MasterPage site, but when I put this code into the content page's codebehind doc, the parameter isn't being passed back from the ASP.NET content page to the XSLT doc.I'm hoping that I won't have to move the transformation to the content page, as that would defeat the point in me creating a dynamic site by using the MasterPage method. Below I'm including the code that I have so far. Also, here are some questions that I haven't been able to answer yet:Q: How should I go about using this method with a MasterPage method (including outputting the data in the <body> tag of the document)?If I have to move the transformation from the master page to the content page, ...Q: ...is there a way to create ONE central doc that contains the transformation setup for ease of future maintenance?Q: ...is there a way to have arguments on the content's codebehind page while keeping the XML/XSL transformation on the master page?If anyone can let me know where I'm going wrong, that would be great. Thanks.**********Working static version code**********XML doc (default.xml):

<?xml version="1.0" encoding="UTF-8"?><default>	<item id="top">Hello</item>	<item id="middle">World</item>	<item id="bottom">!</item></default>

XSL doc (default.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:template match="/">		<xsl:for-each select="default">			<xsl:value-of select="item" /><br />		</xsl:for-each>	</xsl:template></xsl:stylesheet>

ASP.NET doc (default.aspx):

<%@ Page Language="VB" %><%@ 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.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">	Partial Class _default		Inherits System.Web.UI.Page		Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)		Dim strThisPage_xml As String = "/docs/xml/default.xml"		Dim strThisPage_xsl As String = "/docs/xslt/default.xsl"		'Load recordId		Dim recordId_qs As String = Request.QueryString("record_id")		'Create the XPathDocument object		Dim doc As New XPathDocument(Server.MapPath(strThisPage_xml))		'Create the XslTransform object		Dim xslDoc As New XslCompiledTransform()		xslDoc.Load(Server.MapPath(strThisPage_xsl))		'Create the XsltArgumentList object		Dim args As New XsltArgumentList()		args.AddParam("record_id", "", recordId_qs)		'Perform the transformation - pass in the parameters in the XsltArgumentList		xslDoc.Transform(doc, args, Response.Output)		End Sub	End Class</script><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"></head><body>	<form id="form1" runat="server">	<div>		</div>	</form></body></html>

HTML output:

<?xml version="1.0" encoding="utf-8"?>	Hello<br />	World<br />	!<br /><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head></head><body>	<form name="form1" method="post" action="default.aspx" id="form1"><div><input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTUxMzcyMjQyN2RkH2aryQjMtUhd8XjhSLZBSbhs5W8=" /></div>	<div>		<!-- ***THIS IS WHERE I WANT THE OUTPUT TO BE*** -->	</div>	</form></body></html>

**********Non-working dynamic version code**********XML doc (default.xml):

<?xml version="1.0" encoding="UTF-8"?><default>	<item id="top">Hello</item>	<item id="middle">World</item>	<item id="bottom">!</item></default>

XSL doc (default.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:template match="/">		<script>			<![CDATA[			function MM_jumpMenu(targ,selObj,restore){ //v3.0				eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");				if (restore) selObj.selectedIndex=0;			}			]]>		</script>		<select name="selectcategory" onChange="MM_jumpMenu('parent',this,0)"><option value="" selected="true">- Select a Category -</option>			<xsl:for-each select="default">				<option value="{@id}"><xsl:value-of select="item" /></option>			</xsl:for-each>		<select>	</xsl:template></xsl:stylesheet>

Master Page codebehind (MasterPage.master.vb):

Partial Class MasterPage	Inherits System.Web.UI.MasterPage	Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)		'Assign maincolumn XML/XSLT transformation properties		xslTransform_mc.DocumentSource = "/docs/xml/default.xml"		xslTransform_mc.TransformSource = "/docs/xslt/default.xsl"	End SubEnd Class

Master Page (MasterPage.master):

<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" 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 id="Head1" runat="server"></head><body>	<div id="content">		<!-- Dynamic MainColumn -->		<asp:Xml id="xslTransform_mc" runat="server"></asp:Xml>		</div><!-- end content --></body></html>

ASP.NET codebehind doc (default.aspx.vb):

Partial Class _default	Inherits System.Web.UI.Page	Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)		'Create the XsltArgumentList object		Dim args As New XsltArgumentList()		args.AddParam("record_id", "", recordId_qs)	End SubEnd Class

ASP.NET doc (default.aspx):

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

HTML output: nothing shows up. (I would hope that selecting "default.xml?record_id=top" from the drop-down menu would pull up "Hello".)

Link to comment
Share on other sites

Well, I googled "XsltArgumentList Transform Source", and I came up with a lot of great resources for what I was looking for. The one that worked for me is titled "Create ASP.NET pages from XML data", and it's located at http://articles.techrepublic.com.com/5100-22_11-1050062.html. So I just added this bit of code to the default.aspx page:'Assign maincolumn XML/XSLT transformation propertiesxslTransform_mc.DocumentSource = strThisPage_xmlxslTransform_mc.TransformSource = strThisPage_xslxslTransform_mc.TransformArgumentList = args...and the arguments were applied to the XML/XSL transformation. Next, I'll be working on implementing this solution within the MasterPage method. If anyone can give me any pointers on that, that would be great. Thanks again.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...