Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get control list from aspx.

Status
Not open for further replies.

SBGibson

Programmer
Apr 18, 2001
125
IT
Hi guys,

I've developed a web application divided in different areas, every area have its own namespace and many aspx pages.
In one of these areas now I've to display a list of every pages contained in the other areas and the list of the controls every page contains.
I know I can have a list using the controls collection but my problem is how to create the "webform1" object in order to have the control list.
I can't make a table containing all the webpages and I can't force the object name to be the same of the aspx filename.
How I can show to the user a list containing "webform1.aspx, webform2.aspx,..." and when a user select webform1.aspx create the object Myform1 in order to use Myform1.Controls collection?

I hope to have explained the problem, thank you in advance.

Stevie B. Gibson
 
Hi Stevie

Hopefully this should set you on your way. You can use the Directory class' static methods GetFiles and GetDirectories to find all the aspx something like this
Code:
protected DropDownList lst;
		
private void Page_Load(object sender, System.EventArgs e){
	if(!Page.IsPostBack){
		string root = Server.MapPath("/");
		string[] folders = Directory.GetDirectories(root);
		string[] rootfiles = Directory.GetFiles(root, "*.aspx");
		AddFiles(rootfiles, root);
		foreach(string folder in folders){
			string[] files = Directory.GetFiles(folder, "*.aspx");
			AddFiles(files, root);
		}
	}
}

public void AddFiles(string[] files, string root){
	foreach(string file in files){
		string text = file.Remove(0, root.Length);
		lst.Items.Add(new ListItem(text, text.Remove(text.LastIndexOf("."), text.Length - text.LastIndexOf("."))));
	}
}
This will add an item to a dropdownlist for each aspx page. You can then capture the SelectedIndexChanged event and instantiate an instance of the form like this
Code:
private void lst_SelectedIndexChanged(object sender, EventArgs e) {
	Type type = Type.GetType("ApplicationNamespace." + lst.SelectedValue.Replace("/", "."));
	Page page = (Page) System.Activator.CreateInstance(type);
}
The code assumes that the filenames of the webforms will be the same as the class names of the codebehind files. This will generally be the case when creating forms in VS.NET unless you use a fullstop (period) in your filename in whcih case VS will replace this with an underscore. The slashes in the filepath are replace with fullstops for object syntex when creating the type.

You will have to replace ApplicationNamespace in the Type.GetType call with the namespace of your application.

Have a play around and you should be able to get something working from the above.

Hope this helps :)

Rob

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
It's a great example Rob. It was what I was thinking to do and you do it :) great!
Unfortunately filenames of the webforms are not the same as the class names of the codebehind files but, after a few tries, I'm thinking to parse the selected aspx file searching for "inherits=" and retrieve the namespace and class.
Btw your code snippet is great, thanks.

Stevie B. Gibson
 
After some days I solved the problem working on your solution.
Code:
private void lst_SelectedIndexChanged(object sender, EventArgs e) {
    Type type = Type.GetType("ApplicationNamespace." + lst.SelectedValue.Replace("/", "."));
    System.Reflecion.FieldInfo[] fi = GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
}

with this code in the fi[] array there's the list of controls contained in the webform.
For the class name I parsed the .aspx file, avoiding the definition of a naming convention.

Stevie B. Gibson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top