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
----------
using System.Web.Compilation;
Page page = (Page)BuildManager.CreateInstanceFromVirtualPath("~/A.aspx", typeof(Page));
page.ProcessRequest(HttpContext.Current);
LoopingControls(page);
Definition of LoopingControls method:
Declare global ArrayList object - oArrayList;
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:
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:
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,