Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to get all control's ID from a page in another page.

suppose two pages A.aspx and B.aspx. I want want fetch all controls from page B.aspx into A.aspx page


C#
public partial clas PageA:System.Web.UI.Page
{
    //all serverside controls of PageB must be listed here.
}

public partial clas PageB:System.Web.UI.Page
{

}


What I have tried:

I have no clues how can I fix this problem,help needed.
Posted
Updated 29-Feb-16 1:15am
Comments
Pankaj_Verma 29-Feb-16 5:01am    
Make your question clear i.e. whether you need all control ID's only or the controls defined in B.aspx to be used in page A.aspx?

1 solution

Hi,

To achieve this you need to load that page to whom you need to traverse all the controls.

In your case you want to find all the controls of Page "A.aspx" from Page "B". So, on Page "B" you need to load Page "A.aspx", so that all the controls of the page must be rendered and you can find the control ids.

Code:

B.aspx.cs
----------
C#
using System.Web.Compilation;


C#
Page page = (Page)BuildManager.CreateInstanceFromVirtualPath("~/A.aspx", typeof(Page));
            page.ProcessRequest(HttpContext.Current);



LoopingControls(page);



Definition of LoopingControls method:

Declare global ArrayList object - oArrayList;

C#
public void LoopingControls(Control oControl)
        {
            oArrayList = new ArrayList();
            foreach (Control frmCtrl in oControl.Controls)
            {
                if (frmCtrl is TextBox)
                {
                    oArrayList.Add(new UtilityObj(frmCtrl.ID, ((TextBox)frmCtrl).Text));
                }
                if (frmCtrl.HasControls())
                {
                    LoopingControls(frmCtrl);
                }
            }
        }



I have created Entity class to capture - Control ID and Text:

C#
public class UtilityObj
    {
        private string _name;
        private string _value;
        public UtilityObj(string Name, string Value)
        {
            _name = Name;
            _value = Value;
        }
        public string Name
        {
            get { return _name; }
            set { _name = Name; }
        }
        public string Value
        {
            get { return (_value); }
            set { _value = value; }
        }
    }




Final Full Code:


C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Compilation;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
    public partial class GetPageControls : System.Web.UI.Page
    {
        ArrayList oArrayList;
        protected void Page_Load(object sender, EventArgs e)
        {
            Page page = (Page)BuildManager.CreateInstanceFromVirtualPath("~/A.aspx", typeof(Page));
            page.ProcessRequest(HttpContext.Current);

            LoopingControls(page);
        }

        public void LoopingControls(Control oControl)
        {
            oArrayList = new ArrayList();
            foreach (Control frmCtrl in oControl.Controls)
            {
                if (frmCtrl is TextBox)
                {
                    oArrayList.Add(new UtilityObj(frmCtrl.ID, ((TextBox)frmCtrl).Text));
                }
                if (frmCtrl.HasControls())
                {
                    LoopingControls(frmCtrl);
                }
            }
        }
    }

    public class UtilityObj
    {
        private string _name;
        private string _value;
        public UtilityObj(string Name, string Value)
        {
            _name = Name;
            _value = Value;
        }
        public string Name
        {
            get { return _name; }
            set { _name = Name; }
        }
        public string Value
        {
            get { return (_value); }
            set { _value = value; }
        }
    }
}



Thanks,
 
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