Wednesday, July 29, 2015

XSLT for converting DataSet to JSON

<xsl:stylesheet version="1.0"

  <xsl:output method="text" indent="no" omit-xml-declaration="yes" />
  <xsl:template match="NewDataSet">
    "Jobs": [
        <xsl:apply-templates select="Table"/>
  <xsl:template match="Table">
            "id": "<xsl:value-of select="jobid"/>",
            "title": "<xsl:value-of select="jobtitle"/>",
                "address": "<xsl:value-of select="jobaddress"/>",
                "city": "<xsl:value-of select="jobcity"/>",
                "state": "<xsl:value-of select="jobstate"/>",
                "zip": "<xsl:value-of select="jobzip"/>"
            "group": {
                "groupcode": "<xsl:value-of select="jobgroup"/>",
                "description": "<xsl:value-of select="jobgroupdescription"/>"
            "otherinformation": "<xsl:value-of select="otherinformation"/>",
            "payload": "<xsl:value-of select="payload"/>"


Convert XML format using XSLT

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

public virtual Stream Transform(Stream payload)
    XPathDocument myXPathDoc = new XPathDocument(payload);
    XslCompiledTransform myXslTrans = new XslCompiledTransform();
    myXslTrans.Load(this.XsltPath); //Path to XSLT used for transform
    Stream formattedRequest = new MemoryStream();
    XmlTextWriter myWriter = new XmlTextWriter(formattedRequest, null);
    myXslTrans.Transform(myXPathDoc, null, myWriter);
    formattedRequest.Seek(0, SeekOrigin.Begin);

    return formattedRequest;    

Friday, May 8, 2015

Image on end of TextBox or DropDownBox

Example of how to have image display at start of DropDownList

  background: #ffffff url(/images/icon-search.gif) no-repeat 3px 50%;

<asp:DropDownList ID="NameDdl" runat="server" CssClass="icon"></asp:DropDownList>

Friday, April 10, 2015

Retain scroll position after PostBack

To keep current scroll position after postback, use some jQuery and an ASP HiddenField.

 1.) Add the HiddenField to the page:
<asp:HiddenField runat="server" ID="ScrollPosition" Value="" />

2.) Add the Javascript (requires jQuery)
       <script type="text/javascript">
        $(function () {
            var sp = $("#<%=ScrollPosition.ClientID%>");
            window.onload = function () {
                var position = parseInt(sp.val());
                if (!isNaN(position)) {

            window.onscroll = function () {
                var position = $(window).scrollTop();

Thursday, March 19, 2015

CSS use left, center and right images for border

Create complex borders that requires three images (left, right, center).

Create div with id/class
<div id="border"></div>

#border {
  width: 100%;
  height: 33px
  margin: 0 auto;
  padding: 0;
  text-align: left;
  position: relative;
  margin-top: 0px;
  max-width: 885px;

/* Comma separated list of border images */#border 
background: url(/images/border-l.jpg) top left no-repeat, url(/images/border-r.jpg) top right no-repeat, url(/images/border-c.jpg) top repeat;

Tuesday, October 21, 2014

Catch WebException from HttpWebRequest POST error response.

A WebException will be thrown if a non 200 status code is returned from the server.  To check the message returned, catch it and read the response.

  byte[] buf = Encoding.UTF8.GetBytes(payload);
  httpReq.ContentLength = buf.Length;
  httpReq.GetRequestStream().Write(buf, 0, buf.Length);
  HttpWebResponse resp = (HttpWebResponse)httpReq.GetResponse();

   if (resp.StatusCode != HttpStatusCode.NoContent && resp.StatusCode != HttpStatusCode.NotFound)
    return resp.GetResponseStream(); //Successful response

catch (WebException wex)
  using (var stream = ex.Response.GetResponseStream())

  using (var reader = new StreamReader(stream))

    Console.WriteLine(reader.ReadToEnd()); //Message returned from client

catch (Exception ex)
    // Something more serious happened
    // like for example you don't have network access
    // we cannot talk about a server exception here as
    // the server probably was never reached