Tags

, , ,


Let we want to add a control to the page and that content will came from xslt transform form webservice.

To do this we need the following things:
1) The page where we will show the user Control (.ascx)

2) Create User Control

i)  Convert XML using xslt and add the conversion output to as the User Control layout Html.

To add the conversion output as usercontrol layout text we need better control on the conversion output.

So what we will do, we will get the output as string and add a label as only control in the User control and then add the conversion output string (which is eventually HTML) as label text.

So the Solution Structure:

image

Here in the default page i am showing the control and ProgressIndicatorForAR1.ascx is the user Control that will serve our intended control.

Default.aspx CODE:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ProgressIndicatorXsltForAR1._Default" %>
<%@ Register Src="~/ProgressIndicatorControlForAR1.ascx" TagName= "uc" TagPrefix="uc" %>
<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </div>
    </form>
</body>
</html>

Code Behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProgressIndicatorXsltForAR1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ProgressIndicatorControlForAR1 progressIndicatorControl = (ProgressIndicatorControlForAR1)Page.LoadControl(@"ProgressIndicatorControlForAR1.ascx");
            PlaceHolder1.Controls.Add(progressIndicatorControl);
        }
    }
}

User Control Code:

as you can see this control only has a label and we will add this label text property value as the xsltransform output.

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ProgressIndicatorControlForAR1.ascx.cs" Inherits="ProgressIndicatorXsltForAR1.ProgressIndicatorControlForAR1" %>
<asp:Label ID="lblProgressIndicatorForAR1Content" runat="server" Text=""></asp:Label>

Code Behind of the ascx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Xsl;
using System.Text;
using System.IO;

namespace ProgressIndicatorXsltForAR1
{
    public partial class ProgressIndicatorControlForAR1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(MapPath("ProgressIndicatorAndInfoPanelXmlForAR1.xml"));
            string xsltPath = Server.MapPath("P1_ProgressIndicatorXsltForAR1.xslt");
            XslCompiledTransform xlstCompiledTransform = new XslCompiledTransform();
            xlstCompiledTransform.Load(xsltPath);         

            StringBuilder strB = new StringBuilder();
            TextWriter txWriter = new StringWriter(strB);
            xlstCompiledTransform.Transform(xmlDoc, null, txWriter);
            lblProgressIndicatorForAR1Content.Text = strB.ToString();
        }
    }
}

Now we have a nice output:

Also For the XSLT and XML i am using to get the control you can see the previous post.

Advertisements