Load an ASP.NET 2.0 aspx page using System.Reflection?
Asked Answered
B

4

1

Can I load an stand alone aspx page in another stand alone aspx page using System.Reflection?

I am using the ASP.NET 2.0 Web site project model.

Byrne answered 17/12, 2008 at 23:11 Comment(2)
I checked the definition for "scaling issues" and found this.Electronic
I know this is not the best practice, but I am am trying to update parent aspx page from a child aspx page. I am not finding any ideas. Also see: #376477Byrne
C
5

Try using BuildManager.CreateInstanceFromVirtualPath. Sample usage:

Page p = BuildManager.CreateInstanceFromVirtualPath("~/Default.aspx", typeof(Page))

This answers this specific question, although, based on your comments, I'm not sure that this is what you really want.

Chagres answered 18/12, 2008 at 1:7 Comment(0)
L
0

Don't know about doing it using Reflection, which may well be possible, but you can capture the output of an aspx or asp page to a string writer using HttpContext.Server.Execute().
I have used this for rendering some complex email templates, but don't know if that is quite what you are after.

Lavalley answered 18/12, 2008 at 0:3 Comment(0)
B
0

I implemented the following solution and it is what I exactly want to do:

using System.Reflection;
using System.Web.Compilation;

Page p = BuildManager.CreateInstanceFromVirtualPath("~/mypage.aspx", typeof(Page)) as Page;
MethodInfo MyMethod = p.GetType().GetMethod("MyMethod");
MyMethod.Invoke(p, null);
Byrne answered 18/12, 2008 at 1:42 Comment(0)
V
0

If you have a inherited class from UI.Page for your code behind page, you could use this way: set CONTEXT to your current http context

Dim hndlr As IHttpHandler = PageParser.GetCompiledPageInstance("~/mypage.aspx", context.Server.MapPath("~/mypage.aspx"), CONTEXT)
Dim ipage As DerivedPage = DirectCast(hndlr, DerivedPage)
ipage.YourProperty= "Hello"
ipage.DoIt()

So you can have strong typed values and, if you'll change the sign of a method you'll be warned.

Variscite answered 18/12, 2008 at 20:51 Comment(1)
This will throw an error because the compiler will not find the hand variable.Byrne

© 2022 - 2024 — McMap. All rights reserved.