Is there a way to instantiate an aspx page and walk through it's control collection outside of a web request
Some background on this question. I've built a webapp inside of SharePoint that walks a user through a series of forms. Each form is a ASP.NET page with a number of SharePoint and custom controls. As part of a new feature request(details not relevant here), I need to know what fields are on each form. I'm planning to maintain this data inside of a SharePoint list that ot开发者_如何学Cher pieces of my code will access programatically.
Rather than maintaining this list by hand, I'd like to populate it in my deployment scripts (written in C#). What I'm hoping is possible is the create a new Page object for each one of my Pages, and then walk through it's Controls collection using some extension methods I've already built to find out which fields are on each form. Is there a good way to instantiate the Page object in this context? To be completely specific, the deployment code runs inside of a SharePoint event receiver, but if you're not familiar with SharePoint, it would be similar trying to accomplish the same thing inside of a console app.
Well, as long as you have access to your web application's assembly you can make the following using Reflection:
1) Load the assembly into memory.
Assembly asm = Assembly.LoadFrom(@"Your assembly path");
2) Query the types for each one of your pages:
var types = asm.GetTypes();
3) For each of your Type instances, get the constructor, invoke it, get the page's instance and then call your Helper methods.
t = types[0];
t.GetConstructor(new Type[] { });
t.GetMethod("Your Method").Invoke(t, object[] {});
精彩评论