Click here to Skip to main content
15,885,890 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get control name and values of one page (say Page1.aspx) from another page (say page2.aspx) using reflection.

I have followed the code given in http://www.codeproject.com/Articles/17792/Get-Control-Properties-at-runtime-using-Reflection
C#
protected void Button1_Click1(object sender, EventArgs e) 

{     
    DisplyDetails(this);     
} 

In the above code, we invoke the DisplyDetails() method passing the object of control class by "this" reference object in Button Click event handler.
C#
private void DisplyDetails(Control control)     
{     
  foreach (Control controls in this.form1.Controls)     
  {     
     string controlId = controls.ID;     
     Response.Write(controlId);     
     Response.Write("<br>");     
     Type controlType = controls.GetType();     
     PropertyInfo[] properties = controlType.GetProperties();     
     foreach (PropertyInfo controlProperty in properties)     
     {     
        if (controlProperty.Name == "Text" && controlProperty.PropertyType == typeof(String))    
        {    
          string propertyValue = GetPropertyValue(controlProperty.Name, controls);     
          Response.Write(propertyValue);     
          Response.Write("<br><br>");     
        }    
     }     
   }     
 }

It's working fine when i have the method(DisplyDetails)in Page1.aspx but not working while calling from Page2.aspx.

Is there any other way to achieve it. I just want to sent the page name as parameter and it will return its Control name and values.
Posted
Updated 3-Nov-14 7:04am
v2
Comments
Sergey Alexandrovich Kryukov 3-Nov-14 12:36pm    
Why doing so? The purpose, please.
—SA
syed shanu 3-Nov-14 21:47pm    
i think you need to check this line "this.form1.Controls" in foreach loop

1 solution

The method DisplyDetails you provide is private and therefore available only to Page1

If you want to call it from Page2, you should either make it public and instantiate page1 in page2 or make it static (but then you cannot use instance variables).

I would suggest pulling the method out in one of business logic classes and calling it from there:

C#
Public Class DetailManager 
private Control page;

DetailManager(Control page){
    this.page = page;
}
{
    public List<control> DisplayDetails () {}
}</control>


in page(s) code

C#
DetailManager dm = new DetailManager(this)
dm.DisplayDetails()


You should return collection of controls with their properties, rather then writing directly on the output in the page and returning void.

Also, it seems you're simply getting Text from the controls that have that property...why not enumerate the controls and simply ask for their Text property instead of getting all properties. There are recursive functions to go through all your controls and get only those of given type. Like this:[^]

C#
public static IList<T> GetAllControlsRecusrvive<T>(Control control) where T :Control
{
    var rtn = new List<T>();
    foreach (Control item in control.Controls)
    {
        var ctr = item as T;
        if (ctr!=null)
        {
            rtn.Add(ctr);
        }
        else
        {
            rtn.AddRange(GetAllControlsRecusrvive<T>(item));
        }

    }
    return rtn;
}


If this helps, please take time to accept the solution. Thank you.
 
Share this answer
 
Comments
VigneshKumar957 4-Nov-14 10:28am    
It seems good.The real problem is calling DisplayDetails() method. Here, I can't use this keyword passing as argument because I'll call DisplayDetails() of one page from another page. For example I am in Page1.aspx and I want to display controls of Page2 in Page1, so I have to call like DetailManager("Page2.aspx") and also it's not possible i think. Please make me clear.
Sinisa Hajnal 5-Nov-14 1:58am    
You have to pass it the reference of the instance of the class or the type. If you pass it the type you still have to instantiate the page to get its controls. So, don't call DetailManager("Page2.aspx") unless you mean to instantiate by name...instead call DetailManager(new Page2()) - or Page1 or whichever
VigneshKumar957 5-Nov-14 5:41am    
If i call so DetailManager(new Page2()), i'm getting error as there is no context for Page2.
Sinisa Hajnal 5-Nov-14 6:03am    
Sorry, my bad, too much MVC where that would be a class.
Try this: Page page = BuildManager.CreateInstanceFromVirtualPath("Page2.aspx", typeof(Page2) ) as Page;

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