Sunday, January 30, 2011

Dynamically accessing contols on page using XML in asp.net

1.First create the XML (Page1.xml) like this
<?xml version="1.0" encoding="utf-8" ?>
<RoleMaster>
  <Role RoleType="Admin">
    <Control>
    <ID ControlType="TextBox" AccessType="R">
      txtnname
    </ID>
    </Control>
    <Control>
      <ID ControlType="TextBox" AccessType="W">
        txtpwd
      </ID>
    </Control>
    <Control>
      <ID ControlType="TextBox" AccessType="R">
        txtAdd
      </ID>
    </Control>
    <Control>
      <ID ControlType="TextBox" AccessType="W">
        txtCounty
      </ID>
    </Control>
    <Control>
      <ID ControlType="TextBox" AccessType="W">
        txtPhone
      </ID>
    </Control>
    <Control>
      <ID ControlType="DDL" AccessType="W">
        ddlState
      </ID>
    </Control>
  </Role>
  <Role RoleType="Agent">
    <Control>
      <ID ControlType="TextBox" AccessType="W">
        txtnname
      </ID>
    </Control>
    <Control>
      <ID ControlType="TextBox" AccessType="R">
        txtAdd
      </ID>
    </Control>
    <Control>
      <ID ControlType="TextBox" AccessType="W">
        txtCounty
      </ID>
    </Control>
    <Control>
      <ID ControlType="TextBox" AccessType="R">
        txtPhone
      </ID>
    </Control>
    <Control>
      <ID ControlType="TextBox" AccessType="W">
        ddlState
      </ID>
    </Control>
  </Role>
</RoleMaster>

2.Add the following code in aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Page1.aspx.cs" Inherits="Page1" %>
<!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>Test Page1</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
  <asp:Panel ID="pantest" runat="server">       
    <table cellpadding="0" cellspacing="0" border="0" width="50%" align="center">
        <tr>
            <td>
                User Name</td>
            <td>
                <asp:TextBox ID="txtnname" runat="server" Visible="False"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Password</td>
            <td>
                <asp:TextBox ID="txtpwd" runat="server" Visible="False"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Address</td>
            <td>
                <asp:TextBox ID="txtAdd" runat="server" Visible="False"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                County</td>
            <td>
                <asp:TextBox ID="txtCounty" runat="server" Visible="False"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Phone</td>
            <td>
                <asp:TextBox ID="txtPhone" runat="server" Visible="False"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                State</td>
            <td>
                <asp:DropDownList ID="ddlState" runat="server" Visible="False">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
    </asp:Panel>
   
    </div>
    </form>
</body>
</html>
3. Code for accesing XML and making contol visible or false using XML.In the same way you can dymanically create contol on page.

 protected void Page_Load(object sender, EventArgs e)
    {
        // Create instance of XmlDocument class
        XmlDocument XMLRead = new XmlDocument();
        // Load Xml file  
        XMLRead.Load(Server.MapPath("Page1.xml"));                       
        XmlNodeList nodes = XMLRead.SelectNodes(@"RoleMaster/Role");
                foreach (XmlNode node in nodes)
                {  
                    XmlNodeList nodes2 = node.ChildNodes;
                    string RoleTypeCheck = node.Attributes["RoleType"].Value;
                    if (RoleTypeCheck == "Admin")
                    {
                        for (int i = 0, ii = nodes2.Count; i < ii; i++)
                        {
                            XmlNode n = nodes2.Item(i);
                            XmlNodeList n2 = n.ChildNodes;
                            foreach (XmlNode n2a in n2)
                            {
                                string ControlID = n2a.InnerText.Trim();
                                string ControlType = n2a.Attributes["ControlType"].Value;
                                string AccessMode = n2a.Attributes["AccessType"].Value;
                                AddControl(ControlType, ControlID, AccessMode);
                            }
                        }
                    }
                }
    }

  // send any random ID "ControlId",Control Dtype
    public void AddControl(string controlType, string ControlId ,string AccessMode)
    {
        if (controlType == "Label")
        {
            //Label Mynewlabel = new Label();
            //Mynewlabel.ID = ControlId;
            //Mynewlabel.Visible = true;
            //pantest.Controls.Add(Mynewlabel);
            Label LBL = (Label)Page.FindControl(ControlId);
            LBL.Visible = true;
        }

        if (controlType == "TextBox")
        {
            TextBox TXBox =(TextBox) Page.FindControl(ControlId);
            TXBox.Visible = true;
            if (AccessMode=="R")
            {
                TXBox.ReadOnly = true;
            }
            else
            {
                TXBox.ReadOnly = false;
            }
        }
        if (controlType == "DDL")
        {
            DropDownList Mydropdown = (DropDownList)Page.FindControl(ControlId);
            Mydropdown.Visible = true;
        }
    }
}

No comments:

Post a Comment