asp.net转换asp.net页面到页面变量 [英] asp.net convert asp.net page into Page variable

查看:102
本文介绍了asp.net转换asp.net页面到页面变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/376404/load-an-asp-net-2-0-aspx-page-using-system-reflection\">Load一个ASP.NET 2.0 aspx页面使用的System.Reflection?


在以下code,我从我的项目进行迭代,并获得.aspx页文件路径。如何将它们转换成页变量。我曾尝试以下code,但它不工作。我收到以下错误消息

 从'System.String'到'System.Web.UI.Page'无效的演员。

请帮我。

感谢

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{    串[]文件路径= Directory.GetFiles(使用Server.Mappath(〜/),*。*,SearchOption.AllDirectories);    的foreach(在文件路径字符串文件路径)
    {
        如果(filepath.EndsWith(ASPX))
        {
            的Response.Write(文件路径+&LT; BR /&gt;中);            一页一页=(页)Convert.ChangeType(文件路径的typeof(页));        }
    }
}


解决方案

根据你最后的评论:我试图去通过该项目的所有页面,并确定通过每个页面标签和按钮控制。所以,我认为你需要的是这样的:


  1. 请一些基类,所有页面继承

  2. 把一些逻辑基类(可能覆盖preRender,目前尚不清楚你想要控制做什么),发现所有的控件在这些网页(Label和Button)(您可能需要实现自定义的FindControl方法,因为在ASP.NET 的FindControl不是递归)。

这比你正在试图实现与装载有什么更好的和更清洁的.aspx直接等。

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));

        }
    }


}

解决方案

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.

这篇关于asp.net转换asp.net页面到页面变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆