Link to home
Start Free TrialLog in
Avatar of trevoray
trevoray

asked on

how can I write or include an aspx page to an ASP label on my page dynamically?

i need to do something equivalent of Classic include file in my .NET aspx page. I need to assign a file to a label dynamically in my code behind. Can someone please show me how I can assign the value of a file to a label?

something like:  Label1.Text = Response.WriteFile("myfile.aspx")

but that obviously doesn't work.
Avatar of aki4u
aki4u

Label1.Text ="myfile.aspx"
Avatar of trevoray

ASKER

that doesn't work. it just writes out that text on the page. so instead of actually displaying the contents from that webpage, it displays 'myfile.aspx'

also, i should clarify that it is pointing to an external page, so it will be like http://www.thissite.com/myfile.aspx
If you need to display content of another page inside yours use:
<body onload="location.href='http://www.yourwebsite.com/'">
ok, but i need it to be in code behind page. so a body tag wouldn't work. i checked the C# link and that is for displaying XML, i don't see how i could use all of that code for what i am trying to do. i don't need to parse anything, i just want to display the full HTML from another page.

Can this help:
Response.Redirect("YOUR_URL_HERE")

or

string strJS =      "<script language='javascript'>" +
            "window.location.href='" + YOUR_URL_HERE + "';" +
            "</script>";

Page.RegisterStartupScript("YourKey",strJS);
well, i don't want to send the user to another page, i just need the HTML generated from the external page displayed in the middle of my content page. i need it to be displayed where my ASPX label is.

perhaps this isn't possible
Hi trevoray,

use server.Execute() to execute the request and get the result to write in a label as belows.

===============================================
System.IO.StringWriter textWriter = new System.IO.StringWriter();
Server.Execute("webform2.aspx",textWriter);
Label1.Text = textWriter.ToString();
===============================================

But this will work only if the target page, i.e., the page to be executed is still in the same application.  If the page is in different application, then you should go for webrequest.

HTH,
Sam
ASKER CERTIFIED SOLUTION
Avatar of SystemExpert
SystemExpert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DBAduck - Ben Miller
If I were you I would create the stuff that you want in the label and put it in an .ascx (User Control) file.

Then in the code you can change the Label to a Placeholder or even just leave it a label.  Then you can do this:

label1.Controls.Add(Page.LoadControl("pathto.ascx"))

This will put the contents of the User Control in the Label.  Now this will work to get this in the label, but if you want to retrieve the contents later on, then you would have to do some other things, but for just outputting and including, this is the way ASP.NET Works.

Ben Miller