Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » Samples » Downloads
 
Add your own
alternative version
Go to top

Manage ASP.NET Server Controls, Event Handlers, and Server-Side Validation Using XML and XSLT

, 18 Apr 2008
An article on managing ASP.NET server controls, event handlers, and server-side validation using XML and XSLT.
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.XPath;
using System.Xml.Xsl;
using System.IO;
using System.Xml;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        ParseControls();
        BindInfo();
    }
    private void ParseControls()
    {
        BindDDL();
        //create argument list
        XsltArgumentList xslArg = new XsltArgumentList();
        xslArg.AddParam("employeeId", "", ddlEmployee.SelectedValue);

        //load the data
        XPathDocument xdoc = new XPathDocument(Server.MapPath("Address.xml"));
        //load Xslt
        XslCompiledTransform transform = new XslCompiledTransform();
        transform.Load(Server.MapPath("DynamicControls.xslt"));
        StringWriter sw = new StringWriter();
        //transform it
        transform.Transform(xdoc, xslArg, sw);
        string result = sw.ToString();

        //remove namespace
        result = result.Replace("xmlns:asp=\"remove\"", "");
        //parse control
        Control ctrl = Page.ParseControl(result);
        phEmployeeAddress.Controls.Add(ctrl);
    }
    private void BindInfo()
    {
        lblMessage.Text = "";
        
        //find dropdown control and update datasource
        try
        {
            DropDownList ddlCountry1 = phEmployeeAddress.FindControl("ddlCountry1") as DropDownList;
            string selectedValue1 = ddlCountry1.Items[0].Value.Trim();
            ddlCountry1.DataSource = GetCountryList();
            ddlCountry1.DataBind();

            DropDownList ddlCountry2 = phEmployeeAddress.FindControl("ddlCountry2") as DropDownList;
            string selectedValue2 = ddlCountry2.Items[0].Value.Trim();
            ddlCountry2.DataSource = GetCountryList();
            ddlCountry2.DataBind();

            //select the selected value
            ddlCountry1.SelectedValue = selectedValue1;
            ddlCountry2.SelectedValue = selectedValue2;
        }
        catch 
        { 
            //ex
            throw;
        }
        //find control to add event handler
        /*===============================================================
        Note: Event handler adding process should be after parsing 
         * the controls since controls will be available after parsing
        =================================================================*/
        Button btnSaveAddress = (Button)phEmployeeAddress.FindControl("btnSaveAddress");
        btnSaveAddress.Click += new EventHandler(btnSaveAddress_Click);
    }
    private void BindDDL()
    {
        if (Context.Request.QueryString["id"] != null)
        {
            ddlEmployee.SelectedValue = Context.Request.QueryString["id"];
        }
    }
    protected void btnLoadData_Click(object sender, EventArgs e)
    {
        string qryString = "?id=" + ddlEmployee.SelectedValue;
        Response.Redirect("Default.aspx" + qryString);
    }
    protected void btnSaveAddress_Click(object sender, EventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("Address.xml"));
        XmlNodeList nodeList= doc.SelectNodes("/root/Employee");
        int ind1=1;
        int ind2=1;
        foreach (XmlNode parentNode in nodeList)
        {
            if (parentNode.Attributes.GetNamedItem("Id").Value.Equals(ddlEmployee.SelectedValue))
            {
                XmlNode sectionItem = parentNode.SelectSingleNode("Address");
                foreach (XmlNode node in sectionItem.ChildNodes)
                {
                    ind2 = 1;
                    foreach (XmlNode subNode in node.ChildNodes)
                    {
                        string ctrlId = subNode.Attributes.GetNamedItem("Caption").Value + "_" + ind1 + ind2;
                        ctrlId = ctrlId.Replace(' ', '_');
                        string ctrlType = subNode.Attributes.GetNamedItem("Type").Value;
                        if (ctrlType.Equals("CountryDDL"))
                        {
                            object ctrl = phEmployeeAddress.FindControl("ddlCountry"+ind1);
                            DropDownList ddlCountry = ctrl as DropDownList;
                            subNode.InnerText = ddlCountry.SelectedValue;
                        }
                        else
                        {
                            object ctrl = phEmployeeAddress.FindControl(ctrlId);
                            TextBox txtbox = ctrl as TextBox;
                            subNode.InnerText = txtbox.Text;
                        }
                        ind2 += 1;
                    }
                    ind1 += 1;
                }
            }
        }
        doc.Save(Server.MapPath("Address.xml"));
        lblMessage.Text = "Data Saved Successfully";
    }
    private ArrayList GetCountryList()
    {
        ArrayList al = new ArrayList();
        al.Add(new ListItem("Bangladesh", "BD"));
        al.Add(new ListItem("Japan", "JP"));
        al.Add(new ListItem("United States", "USA"));
        return al;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ehsanul Haque
Software Developer (Senior) Desme Inc
Bangladesh Bangladesh
Ehsanul Haque is a Software Engineer, who is dedicated to Microsoft .NET based development. He is involved with several US based software projects from his country. Already he has completed few projects, which are being used by several users of different countries, such as USA, Japan, and Bangladesh. While developing and managing a team, he maintains a set of well defined engineering practices developed by him and other online developer community. Beside software development, he has also written several technical articles.
 
When not engaged with technical stuffs, he likes to pass time with his friends, and family members, listens music, watching TV, travel or by reading books.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 18 Apr 2008
Article Copyright 2008 by Ehsanul Haque
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid