The Poll s System Code and Walk-Throughs

only for RuBoard

The Poll's System Code and Walk-Throughs

This section walks you through all the source code that makes up this application.

The Default.aspx File

The default.aspx is the heart and soul of the polling application; from here, the web server decides what .xsl transformation will take place to produce a UI for the client.

There are three possible pages in the poll's system:

  • A list of polls

  • Voting form

  • Poll results

The default.aspx file also decides what version of the UI to render to the client, either WML for WML-compliant devices or XHTML for all other devices.

To see how this is done, look at Listing 13.7 and the code walk-through , which follows it.

Listing 13.7 default.aspx
 <%@ Import Namespace="System.Text" %>  <%@ Import Namespace="System.Xml" %>  <%@ Import Namespace="System.Xml.Xsl" %>  <script language="c#" runat="server">  private void Page_Load(object sender, System.EventArgs e)  {      string sPage = "";       string sVoteOn = "";       string sVoteItem ="";       XmlDocument xmlDoc = new XmlDocument();       XslTransform xslDoc = new XslTransform();       XmlDocument worker = new XmlDocument();       XsltArgumentList args = new XsltArgumentList();       int iQueryParams = Context.Request.QueryString.Count;       //if less than two params show list of polls       if(iQueryParams < 2)       {      if (isWML()) { sPage="pollsWML.xsl";}       else {sPage="pollsXHTML.xsl";}       // Load the source xml       xmlDoc.Load(Server.MapPath("polls.xml"));       //' load the source xsl       xslDoc.Load(Server.MapPath(sPage));       // Get the resulting output and load it into a new document       worker.Load(xslDoc.Transform(xmlDoc.DocumentElement.CreateNavigator(),null));       // Print the result to the output stream       if (isWML()) { Response.ContentType = "text/vnd.wap.wml"; }       Response.Write(worker.InnerXml);       }       //2 params so display vote page or Results page       if(iQueryParams == 2)       {           sPage = Context.Request.QueryString["displayPage"];       sVoteOn = Context.Request.QueryString["voteon"];       // Load the source xml       xmlDoc.Load(Server.MapPath("polls.xml"));       // load the source xsl       xslDoc.Load(Server.MapPath(sPage));       //setup parameter value       args.AddParam("voteon", string.Empty, sVoteOn);       // Get the resulting output and load it into a new document       worker.Load(xslDoc.Transform(xmlDoc.DocumentElement.CreateNavigator(),args));       // Print the result to the output stream       if (sPage.IndexOf("WML") >=0) { Response.ContentType = "text/vnd.wap.wml"; }       Response.Write(worker.InnerXml);       }       //3 params so update xml file., then redirect and show vote results       if(iQueryParams == 3)       {      sPage = Context.Request.QueryString["displayPage"];       sVoteOn = Context.Request.QueryString["voteon"];       sVoteItem = Context.Request.QueryString["voteitem"];              XmlDocument xDoc = new XmlDocument();       xDoc.Load(Server.MapPath("polls.xml"));       XmlElement xPoll = (XmlElement)xDoc.SelectSingleNode("polls/poll graphics/ccc.gif [@text='"+sVoteOn+"']");       XmlElement xItem = (XmlElement)xPoll.SelectSingleNode("item[@text='" + sVoteItem + graphics/ccc.gif "']");       XmlAttribute xVotes = (XmlAttribute)xItem.Attributes["votes"];       xVotes.Value = Convert.ToString(Convert.ToInt32(xVotes.Value)+1);       xDoc.Save(Server.MapPath("polls.xml"));       if(sPage.IndexOf("WML")>=0){ Response.Redirect("default.aspx?displayPage=ResultWML.xsl&voteon="+HttpUtility.HtmlEncode(sVoteOn)); } graphics/ccc.gif else graphics/ccc.gif {Response.Redirect("default.aspx?displayPage=ResultXHTML.xsl&voteon="+sVoteOn);}       }  }  ///  /// Function used to tell whether the client device is WML  ///  bool isWML()  {      //Get required server variables used by function       string userBrowser =  Context.Request.ServerVariables["HTTP_USER_AGENT"];       //  if MOZ in the string its a XHTML Compliant browser       //  else it is a WML compliant browser       userBrowser = userBrowser.ToUpper();       if(userBrowser.IndexOf("MOZ") >= 0) {           return(false);       }       else {           return(true);       }  }  </script> 

Listing 13.7 is complex, so let's walk-through it a section at a time.

One of the main purposes of the page load event code is to decide what transform file to use with the XML poll data. This is done by checking the query string parameter count, based on the following logic.

The parameters that are passed by query string are as follows:

  • displayPage The page to display (poll, vote, result, or update)

  • Voteon The question to vote on, see statistics for, or to update in the XML file

  • VoteItem Holds the text label of the vote item on which to vote

The following logic is used to select a page for processing:

  1. If no parameters passed,display a list of votes.

  2. If two parameters passed, use the passed displaypage as the XSL file.

  3. If three parameters passed update the XML file with new data from inside default aspx file and then redirect the user to the results page for their browser type.

Now you know what the default.aspx is meant to do logicwise. Take a closer look at the code.

The isWML() Function

The default.aspx also detects what client browser is being used and selects the initial .xsl file based on the client browser type. This is done in the isWML() support function in the default.aspx file. It's also shown in the following code:

 bool isWML()  {      //Get required server variables used by function       string userBrowser =  Context.Request.ServerVariables["HTTP_USER_AGENT"];       userBrowser = userBrowser.ToUpper();       //  if MOZ in the string its a XHTML Compliant browser       //  else it is a WML compliant browser       if(userBrowser.IndexOf("MOZ") >= 0) {           return(false);       }       else {           return(true);       }  } 

The isWML() function returns a boolean value that indicates whether the client browser can support WML or not. This is done by checking the HTTP_USER_AGENT server variable for the value of MOZ . This value is in the user agent string for all desktop-based browsers (Internet Explorer and Netscape, for example).

If the value of MOZ does not exist, the client browser is assumed to be a WML-compliant one.

The Page_Load() Event

The Page_Load event carries out all the processing in the default.aspx file. Its sole purpose is to generate the UI of the poll's system and to update the polls.xml file with new vote information.

To start, declare all the objects that you are going to use in this routine:

  • sPage The current display page (page to render)

  • sVoteOn The current poll on which to vote

  • sVoteItem The item to vote on in a poll

  • xmlDoc The object used to hold the polls.xml file

  • xslTransform The object used to hold the .xsl transformation file

  • worker The object used to hold the transformed XML file results

  • args An Xsl Arg. list for .xsl files that use parameters

  • iQueryParams The total number of QueryString variables in page request

 private void Page_Load(object sender, System.EventArgs e)       string sPage = "";       string sVoteOn = "";       string sVoteItem ="";       XmlDocument xmlDoc = new XmlDocument();       XslTransform xslDoc = new XslTransform();       XmlDocument worker = new XmlDocument();       XsltArgumentList args = new XsltArgumentList(); 

Store the total QueryString parameters in a variable for easy access:

 int iQueryParams = Context.Request.QueryString.Count; 

Next , check the iQueryParams variable to see if less than two QueryString variables have been passed. If so, you know that this is the first access to the page and that you should display a list of polls from which the user can select.

After this check is made, define which page you are going to use by checking to see if the requesting device is WML-compliant by using the isWML() function. If the device can support WML, set the sPage variable to hold the name of the WML .xsl file for the poll's list; otherwise , store the filename of the XHTML version.

There are better ways of doing this, but because the main purpose of this example is to show how to use various .xsl transformations for mobile device development, some values have been hard-coded for simplicity. In a real application, you would create more sophisticated logic for selecting and rendering each device type, probably by using a XML file as a data store of what devices are supported and the names of the pages that they should use.

After these filenames are stored, load the polls.xml file into the xmlDoc object, then load the defined XSL file into the xslDoc object.

Now that the xmlDoc and xslDoc objects are defined, apply a transformation and store the results in the worker object.

The next line checks again whether the rendering device is WML based. If it is, you must change the response content type to text/vnd.wap.wml so that it renders correctly on a mobile device that supports WML.

Finally, render the contents of the worker object (the translated XML document results) to the client browser, as shown here:

 //if less than two params show list of polls  if(iQueryParams < 2)  { if (isWML()) { sPage="pollsWML.xsl";}  else {sPage="pollsXHTML.xsl";}  // Load the source xml  xmlDoc.Load(Server.MapPath("polls.xml"));  //' load the source xsl  xslDoc.Load(Server.MapPath(sPage));  // Get the resulting output and load it into a new document  worker.Load(xslDoc.Transform(xmlDoc.DocumentElement.CreateNavigator(), null));  // Print the result to the output stream  if (isWML()) { Response.ContentType = "text/vnd.wap.wml"; }  Response.Write(worker.InnerXml);  } 

You have now just seen what happens if it is the first access to the page. Now look at the other page requests :

  • Displaying the poll and letting the user vote

  • Displaying the poll results

  • Updating the polls.xml file with updated poll information

The first two of these page requests are dealt with in the next code walk-through and the final one.

If two QueryString variables have been passed, you must be either allowing the user to vote on a poll or displaying a poll's voting results.

The decision on which page to display is driven purely by the displayPage QueryString parameter. This actually holds the name of the .xsl transform file to use for rendering. (This is defined in the .xsl file that displays the polls list; all the .xsl files are explained in the next section.)

The displayPage value is stored in sPage variable for easy reference.

After you know what .xsl page to use for transformation of the XML file, you can store both the polls.xml and the file referenced in the sPage variable in their relevant objects ( xmlDoc and xslDoc ).

Next, create an xslt argument (based on the sVoteOn value) and add it to the args object. This value is used by the .xsl files to select the correct poll from the poll's XMLS file. (This is covered in more detail in the next section where I detail the workings of the .xsl files used by the polls system).

After defining the parameter value for the XSL transformation, you can actually perform the transformation and save it in the worker object prior to rendering its contents to the client devices display.

After this is done, check to see if the .xsl file you're using is for WML. This is done by looking for the value of WML in the sPage variable. If it's found, you are doing an WML-based client and need to set the response type of the client browser to the relevant type.

Now render the results to the browser, as shown here:

 if(iQueryParams == 2)  {     sPage = Context.Request.QueryString["displayPage"];  sVoteOn = Context.Request.QueryString["voteon"];  // Load the source xml  xmlDoc.Load(Server.MapPath("polls.xml"));  // load the source xsl  xslDoc.Load(Server.MapPath(sPage));  //setup parameter value  args.AddParam("voteon", string.Empty, sVoteOn);  // Get the resulting output and load it into a new document  worker.Load(xslDoc.Transform(xmlDoc.DocumentElement.CreateNavigator(), args));  // Print the result to the output stream  if (sPage.IndexOf("WML") >=0) { Response.ContentType = "text/vnd.wap.wml"; }  Response.Write(worker.InnerXml);  } 

The final bit of processing that the default.aspx file does is actually update the polls.xml file with updated information on a poll after voting.

If the QueryString variable has three variables in it, you are updating the poll's XML file, so first you must store the page's sPage , sVoteOn , and sVoteItem variables.

Then, you load the polls.xml file into the xmlDoc object so that it is ready for processing. After this, declare an XmlElement to reference the poll with which you are working. Next, create another XmlElement to reference the item of the poll on which the client has voted.

Now that you have the poll and vote item that you require, you must access the correct attribute of the vote item so that you can increase its vote count by one. This is done by declaring an xmlAttribute object to get the votes attribute of the <item> tag in the polls.xml file.

After you have this value, increment it by one, and save the changed XML file to disk.

Now that the XML file is updated, simply redirect the user to the results page for the specified client browser. This is done by checking the sPage vari-able for the value of WML, just as you did earlier.

That's the processing of the default.aspx . It is not complex after you get familiar with it, but until then, it can be a bit tough.

only for RuBoard


XML and ASP. NET
XML and ASP.NET
ISBN: B000H2MXOM
EAN: N/A
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net