Click here to Skip to main content
15,921,250 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to List all the Web Server Controls available on each and every Page with in an Web Based Application / Web Solution based on user Page selection?

For Instance : Select Any Page Name from Dropdown. (Suppose Dropdown is filled with all Page Names available in a web solution) and It will show all the Controls available on specified page.

Page Name : AboutUs.aspx
Controls Available on Page : AboutUs.aspx
-----------------------
Controls ID
-----------------------
Textbox : txtUserName
CheckBox : chkMyStatus

Now Change the Page Name from Dropdown :

Page Name : Home.aspx
Controls Available on Page : Home.aspx
-----------------------
Controls ID
-----------------------
Textbox Id : txtAge
Gridview : Gridview1


Also It Should show all the Properties of Specified Controls.

Thanks in Adv.
Posted

Steps:
1. Based on the PageName selected, Get that page
2. Loop through all the controls of that page
foreach (Control c in Page.Controls)
{       
  // control c
  // c.ID;
  // c.GetType
  // all control properties accessible
  // store this in a table
}

3. Maintain a table. Store all the things needed.
4. display the table in a Datagrid.


You have to design your website such that the pages are derived from some BasePageClass such that having a page reference is easy for you.
 
Share this answer
 
v2
Step 1. All the Page Names in Dropdown

Add ddlPageNames Dropdown on aspx Page

C#
protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           string[] filePaths = Directory.GetFiles(@"D:\WebSite1\", "*.aspx");
           foreach (String item in filePaths)
           {
               char c = '\\';
               String[] Filter = item.Split(c);
               ddlPageNames.Items.Add(Filter[2]);
           }
       }
   }



Step 2. Create a Sample Page where you can load another page with in using iFrame Object and Collect all the Control Properties and Save them into Session.

XML
<p>
    <iframe id="I1" runat="server" name="I1" width="0" height="0" src="Default.aspx" visible="true">
    </iframe>
   <asp:DropDownList ID="ddlPageNames" runat="server" AutoPostBack="true" onselectedindexchanged="ddlPageNames_SelectedIndexChanged"></asp:DropDownList>
        <asp:Button ID="btnListControls" runat="server" Text="Button" onclick="Button1_Click" />
        <asp:Label ID="TotalControls" runat="server"></asp:Label>
        <asp:GridView ID="GridView1" runat="server"
        onpageindexchanged="GridView1_PageIndexChanged"
        onpageindexchanging="GridView1_PageIndexChanging"
        onrowdatabound="GridView1_RowDataBound" >
        </asp:GridView>
</p>



C#
protected void btnListControls_Click(object sender, EventArgs e)
   {
       DataView dt;
       dt = (DataView)Session["dtData"];
       if (dt != null)
       {
           GridView1.DataSource = dt;
           GridView1.DataBind();
       }
   }
   protected void ddlPageNames_SelectedIndexChanged(object sender, EventArgs e)
   {
       I1.Attributes["src"] = Convert.ToString(ddlPageNames.SelectedValue);

   }


Step 3. Now Bind the Stored Control Names into Gridview to List all.

For Instance :

Suppose You Want to List all Controls Available on Default.aspx Page then Follow
C#
 DataTable dt = new DataTable();
 DataView objDV = new DataView();


protected void Page_Load(object sender, EventArgs e)
    {
        
        
        dt.Columns.Add("Ctrl", typeof(string));
        dt.Columns.Add("Name", typeof(string));
        foreach (Control ParentMain in Page.Controls)
        {
            #region //CurrentPage
            Page objpage = new Page();
            string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
            System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
            string sRet = oInfo.Name;
            //txtControlsList.Text = Convert.ToString(txtControlsList.Text) + sRet + "\n";
            //txtControlsList.Text = Convert.ToString(txtControlsList.Text) + ParentMain.ID+"\n";
            foreach (Control Child1 in ParentMain.Controls)
            {
                //txtControlsList.Text = Convert.ToString(txtControlsList.Text) + Child1.ID + "\n";
                FillControlList(Child1);
                foreach (Control Child2 in Child1.Controls)
                {
                    //txtControlsList.Text = Convert.ToString(txtControlsList.Text) + Child2.ID + "\n";
                    FillControlList(Child2);
                    if (Convert.ToString(Child2.ID) == "GridView1")
                    {
                        GridView myGridView1 = (GridView)Page.FindControl("GridView1");
                    }
                    #region
                    foreach (Control Child3 in Child2.Controls)
                    {
                        //txtControlsList.Text = Convert.ToString(txtControlsList.Text) + Child3.ID + "\n";
                        FillControlList(Child3);
                        if (Convert.ToString(Child3.ID) == "Button1")
                        {
                            Child3.Visible = false;
                        }
                        foreach (Control Child4 in Child3.Controls)
                        {
                            FillControlList(Child4);
                            if (Convert.ToString(Child4.ID) == "Button1")
                            {
                                Child4.Visible = false;
                            }
                            foreach (Control Child5 in Child4.Controls)
                            {
                                FillControlList(Child5);
                                if (Convert.ToString(Child5.ID) == "Button1")
                                {
                                    Child5.Visible = false;
                                }
                            }
                        }
                    }
                    #endregion
                }
            }
            #endregion
        }
        objDV = dt.DefaultView;
        Session["dtData"] = objDV;
}


Next 

 public void FillControlList(Control Ctrl)
    {
        ListItem li = new ListItem();
        li.Text = Convert.ToString(Ctrl.ID);
        li.Value = Convert.ToString(Ctrl.ID);
        if (!string.IsNullOrEmpty(Convert.ToString(li.Text)))
        {
            DataRow row = dt.NewRow();
            row["Ctrl"] = Ctrl.ID;
            dt.Rows.Add(row);
            dt.Rows[dt.Rows.Count - 1][1] = Ctrl.GetType().Name;
        }
    } 
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900