Click here to Skip to main content
15,891,646 members
Articles / Programming Languages / XML

How to Create Dynamic Fields Form Since XML File

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
11 Nov 2014CPOL 11.8K   3  
How to create dynamic fields form since XML file

Depending on the customer, sometimes you don´t know the controls that you must show, load or render in your .NET application.

A good way is having an XML file depending on the customer and you will load your page dynamically and your controls (this example in C#).

This is an example of XML configuration file where there is only one customer (Customer1):

XML
<Centers>
    <Center Name="Customer1>">
        <Field Name="Customer Number" 
        Tipo="String"  Id="CustomerNumber"></Campo>
        <Field Name="Customer City" 
        Tipo="String"  Id="CustomerCity"></Campo>
    </Center>
</Centers>

You will need and empty Panel object in your aspx page.

ASP.NET
<asp:Panel runat="server" ID="panelControls" Width="250px">
    </asp:Panel>

In my example, I have Telerik components, but you can use ASP.NET component without problems.
The method you can use in your code behind (load event, init event … ) could be something like that:

C#
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;  // if you work with Telerik
....
protected void BuildPanel()
{
    controls = new List<Control>();

    XElement xmlControls = XElement.Load(MapPath("~/XML/ImportacionHosp.xml"));

    XElement fieldsCenter = xmlControls.Elements("Center").SingleOrDefault
				(e => e.Attribute("Name").Value == "Customer1");

    HtmlGenericControl auxTable = new HtmlGenericControl("table");
    auxTable.Style.Add("text-align", "left");

    foreach (XElement field in fieldsCenter.Elements())
    {
        HtmlGenericControl auxTr = new HtmlGenericControl("tr");
        HtmlGenericControl auxTh = new HtmlGenericControl("th");
        HtmlGenericControl auxTd = new HtmlGenericControl("td");

        switch (field.Attribute("Tipo").Value)
        {
            case "Int32":
                RadNumericTextBox auxNum = new RadNumericTextBox();
                auxNum.MinValue = 0;
                auxNum.MaxValue = 99999;
                auxNum.NumberFormat.DecimalDigits = 2;
                auxNum.DataType = Type.GetType("Int32");
                auxNum.ID = field.Attribute("Id").Value;

                controls.Add(auxNum);
                auxTd.Controls.Add(auxNum);
                break;

            case "String":
                RadTextBox auxText = new RadTextBox();
                auxText.MaxLength = int.Parse(field.Attribute("MaxLength").Value);
                auxText.ID = field.Attribute("Id").Value;

                controls.Add(auxText);
                auxTd.Controls.Add(auxText);
                break;

            case "DateTime":
                RadDateInput auxDate = new RadDateInput();
                auxDate.ID = field.Attribute("Id").Value;

                controls.Add(auxDate);
                auxTd.Controls.Add(auxDate);
                break;

            default:
                 // launch exception.
        }

        auxTr.Controls.Add(auxTh);
        auxTr.Controls.Add(auxTd);
        auxTable.Controls.Add(auxTr);
    }
  panelControls.Controls.Add(auxTable);
}

You can improve it adding in XML file parameters as MaxLength, MaxValue, … and controlling in BuildPanel() method.

If this post helped you, please leave a comment.

Filed under: .NET, CodeProject, Telerik, Visual Studio

License

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


Written By
Software Developer (Senior)
Spain Spain
My name is Víctor Sumozas. I am an Industrial engineer and passionate exponent and enthusiast in software development and a keen interest in continuing evolution of .Net technologies. Certificated in Developing ASP.NET 4.5 MVC 4 Web Applications.
You can read the original post at Sumozas Null Pointer
Any comment will be highly appreciated. Thanks!

Comments and Discussions

 
-- There are no messages in this forum --