View all answers

Oct 19, 2007 at 7:57 PM
The code shown below shows a way to scroll through all the answers to a survey. It basically consists of:
1) WebSurveyAnswers.aspx - File with a FormView connected to an XmlDataSource and an nested GridView which displays each question and answer per row. Paging through the gridview allows you to veiw all responses.

2) XSL_LookupQuestion.xsl - xls Transformation file which creates a new answer file (answersXSLT.xml) with the Question Stament looked up from the survey.xml file (and CR/LF, leading & trailing spaces removed).

3) WebSurveyAnswers.aspx.cs - Code behind page to do the transformation in the page load event. I was unable to do the tramsformation with the TransformFile section of the xmldatasource (see the unused "XmlDataSource1"). If someone figures out why and how ti fix it please respond!!

This assumes the files are in a directory "~/Data" adjust the XmlDataSource if needed. Hopefully this will be useful for some folks. There are many ways to improve on this.

Steve, feel free to include this in the source code project if you feel it is worth it.

Matt
========================================================================
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebSurveyAnswers.aspx.cs" Inherits="WebSurveyAnswers" %>

<!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 id="Head1" runat="server">
<title>Survey All Answers Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FormView ID="Formview1" runat="server" DataSourceID="XmlDataSource4" AllowPaging="True"
BorderWidth="1px" BorderColor="#CC9966" BorderStyle="None" BackColor="Blue">
<RowStyle ForeColor="#3333FF" BackColor="#CCCCCC"></RowStyle>
<PagerStyle ForeColor="#CC3300" HorizontalAlign="Center" BackColor="#FFFFCC"></PagerStyle>
<ItemTemplate>
Survey Results: <b> <br /> <br />

<asp:GridView runat="server" ID="ItemsSummary" DataSource='<%# XPathSelect("Answer") %>'
AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
<Columns>

<asp:TemplateField HeaderText="Question">
<ItemTemplate>
<asp:Label runat="server" ID="Question" Text='<%#XPath("@Id")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Answer">
<ItemTemplate>
<asp:Label runat="server" ID="NameLabel" Text='<%#XPath(".")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
</asp:GridView>



</ItemTemplate>
</asp:FormView>
</div>


</form>

<asp:XmlDataSource id="XmlDataSource1" runat="server"
DataFile="~/Data/Answers.xml"
XPath="Answers/AnswerSet"
TransformFile="XSL_LookupQuestion.xsl" />



<asp:XmlDataSource id="XmlDataSource4" runat="server"
DataFile="~/Data/AnswersXSLT.xml"
XPath="Answers/AnswerSet"
/>

</body>
</html>
========================================================================
========================================================================
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:variable name ="statements" select="document('data/survey.xml')" />
<xsl:key name="statement" match="Question" use="@id"/>


<xsl:template match="Answers">
<Answers>
<xsl:apply-templates select="AnswerSet"/>
</Answers>
</xsl:template>

<xsl:template match="AnswerSet">
<AnswerSet>
<xsl:apply-templates select="Answer"/>
</AnswerSet>
</xsl:template>

<xsl:template match="Answer">
<Answer>

<xsl:attribute name="questionId">
<xsl:value-of select="@questionId" />
</xsl:attribute>

<xsl:variable name="questId" select="@questionId" />

<xsl:attribute name="Id">
<xsl:for-each select="$statements">

<xsl:variable name="quest" select="key('statement', $questId)" />

<!--get Statement for this questionID and strip off CR/LF-->
<xsl:variable name="Statement" select="translate($quest/Statement,'&#xD;&#xA;','')"/>

<!--call function to rim leading, trailing space-->
<xsl:call-template name="trim">
<xsl:with-param name="s" select="$Statement" />
</xsl:call-template>
<xsl:value-of select="Statement"/>

</xsl:for-each>

</xsl:attribute>

<xsl:value-of select="."/>

</Answer>
</xsl:template>


<!--this function performs a left trim-->
<xsl:template name="left-trim">
<xsl:param name="s" />
<xsl:choose>
<xsl:when test="substring($s, 1, 1) = ''">
<xsl:value-of select="$s"/>
</xsl:when>
<xsl:when test="normalize-space(substring($s, 1, 1)) = ''">
<xsl:call-template name="left-trim">
<xsl:with-param name="s" select="substring($s, 2)" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$s" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<!--this function performs a right trim-->
<xsl:template name="right-trim">
<xsl:param name="s" />
<xsl:choose>
<xsl:when test="substring($s, 1, 1) = ''">
<xsl:value-of select="$s"/>
</xsl:when>
<xsl:when test="normalize-space(substring($s, string-length($s))) = ''">
<xsl:call-template name="right-trim">
<xsl:with-param name="s" select="substring($s, 1, string-length($s) - 1)" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$s" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<!--this function performs a right trim, then left trim-->
<xsl:template name="trim">
<xsl:param name="s" />
<xsl:call-template name="right-trim">
<xsl:with-param name="s">
<xsl:call-template name="left-trim">
<xsl:with-param name="s" select="$s" />
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:template>

</xsl:stylesheet>
========================================================================

========================================================================
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Xml;
using System.Xml.Xsl;

public partial class WebSurveyAnswers : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string xslFile = Server.MapPath("XSL_LookupQuestion.xsl");
string xmlFile = Server.MapPath("data/answers.xml");
string newFile = Server.MapPath("data/answersXSLT.xml");

XslCompiledTransform transf = new XslCompiledTransform();
XsltSettings settings = new XsltSettings();
settings.EnableDocumentFunction = true;

// Create an XmlUrlResolver with default credentials.
XmlUrlResolver resolver = new XmlUrlResolver();

transf.Load(xslFile, settings, resolver);
transf.Transform(xmlFile, newFile);

}
}
========================================================================



Coordinator
Nov 5, 2007 at 5:25 AM
Thanks!

This seems cool. I will see about possibly including this in a future version so others can receive benefit from it as well.

-Steve
Sep 19, 2010 at 5:03 AM

Thanks to Steve for creating this control and Matt82 for your addition.

This is my first attempt at using XML and also C#.  So while challenging, a good learning experience with a great control and application.

TRU