Server.Execute reference created objects

S

Sophos

Hi,

I have a basic aspx page, in the Page_Init procedure I have a
server.execute of another aspx page that adds some html code to my
first page. However it also adds a control (an htmlimage), now my
question is how to I get access to that control from the class in my
first page? Whenever I try to access it I get a "object reference not
set to an instance..." error.

Thank you,
Jonathan
 
T

Tee

Well, I think you should knew the object name of that control, you can use
FindControl function.

the code should be something like this, I giving an example of FindControl
with TextBox


CType(FindControl("MyTextBox"), TextBox).Text = "Hello"

Change the "MyTextBox" to your object name, and "TextBox" to the object
type.


Tee
 
S

Scott Allen

Hi Sophos:

I'm not sure how Server.Execute can return an HtmlImage "control".
Server.Execute renders the page you ask it to execute, and you fetch
the same HTML a client would see if they requested the page with a
browser.

StringWriter writer = new StringWriter();
Server.Execute("otherpage.aspx",writer);
string html = writer.ToString();
writer.Close();

You should have an image tag in the string, an image tag put in by an
HtmlImage control. You could use regular expressions or String.IndexOf
to search for the image tag, but you won't find a control.

HTH,
 
D

DalePres

You can't directly do what it is you're trying to do but here's a work
around:

In your WebForm2, add code like this:
WebForm1 wf1 = (WebForm1)Context.Handler;

That will make WebForm1 available to the page you called with
Server.Execute. Now, in WebForm1, create an image control with its visible
property set to false, or even an array to hold the information you want to
copy. Set the protection of the image control to public.

Create a property to return the image:

public System.Web.UI.WebControls.Image MyImage
{
get { return Image1; }
set { Image1 = value; }
}


If you choose to use an array to get the values into, create a public
property in WebForm1 to return the array value:

private object imagePropertyValues[];

public object ImagePropertyValues[]
{
get { return imagePropertyValues; }
set { imagePropertyValues = value; }
}

In the code of your WebForm2, copy the properties you wish back to the
WebForm1 control or WebForm1 array. You can also copy the image object,
itself:

wf1.MyImage = Image1; // this line is in WebForm2.aspx.cs

That will let you retrieve property values back in WebForm1 but will not let
you dynamically control the values in WebForm2. If you want to dynamically
control the values in WebForm2, then create public properties or objects in
WebForm1 to hold those values prior to calling Server.Execute("WebForm2").
Then in the code for WebForm2, retrieve those values from the WebForm1
object you created in the first line of code above.

It's only slightly convoluted but you can, by using these concepts, set
values in WebForm2 and use those values in WebForm1 after WebForm2 completes
execution.

The one thing you cannot do is to change a value in WebForm2 controls after
WebForm2 returns. Think of WebForm2 as a method, and when it completes
execution, its fields are out of scope and cannot be accessed.

Dale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top