-3

Possible Duplicate:
Load an ASP.NET 2.0 aspx page using System.Reflection?

In the following code, I iterate through my project and get file paths for .aspx pages. How do cast them to Page variable. I have tried the following code but its not working. I get the following error message

Invalid cast from 'System.String' to 'System.Web.UI.Page'.

Please help me.

thanks

protected void Page_Load(object sender, EventArgs e)
{

    string[] filePaths = Directory.GetFiles(Server.MapPath("~/"), "*.*", SearchOption.AllDirectories);

    foreach (string filepath in filePaths)
    {
        if (filepath.EndsWith(".aspx"))
        {
            Response.Write(filepath + "<br/>");

            Page page = (Page)Convert.ChangeType(filepath, typeof(Page));

        }
    }


}
Community
  • 1
  • 1
dotnet-practitioner
  • 13,968
  • 36
  • 127
  • 200

1 Answers1

1

As per your last comment "I am trying to go through all the pages in the project and identify Label and Button control through each page.". So I think what you need is this:

  1. Make some base class from which all pages inherit
  2. Put some logic in that base class (probably override PreRender, it is not clear what you want to do with controls) and find all controls (Label and Button) on those pages (you probably need to implement custom FindControl method, because FindControl in ASP.NET is not recursive).

That is much better and cleaner than what you are trying to achieve with loading .aspx directly etc.

Community
  • 1
  • 1
Tomas Voracek
  • 5,886
  • 1
  • 25
  • 41
  • Appreciate your feedback.. but this does not work for what I am doing. I wish I could get an answer purely from technical point of view with out having to disclose all the nitty gritty details on what I am doing. SO forum is very much becoming a very difficult place to have a free flowing discussions. – dotnet-practitioner Jul 10 '12 at 22:56