Server.Execute reference created objects

Discussion in 'ASP .Net' started by Sophos, Jul 9, 2004.

  1. Sophos

    Sophos Guest

    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
     
    Sophos, Jul 9, 2004
    #1
    1. Advertising

  2. Sophos

    Tee Guest

    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



    "Sophos" <> wrote in message
    news:...
    > 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
     
    Tee, Jul 10, 2004
    #2
    1. Advertising

  3. Sophos

    Scott Allen Guest

    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,

    --
    Scott
    http://www.OdeToCode.com


    On 9 Jul 2004 12:12:33 -0700, (Sophos) wrote:

    >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
     
    Scott Allen, Jul 10, 2004
    #3
  4. Sophos

    DalePres Guest

    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


    "Sophos" <> wrote in message
    news:...
    > 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
     
    DalePres, Jul 10, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. suntekysp
    Replies:
    0
    Views:
    1,550
    suntekysp
    Aug 17, 2003
  2. jimjim
    Replies:
    12
    Views:
    1,907
    Ron Natalie
    Jun 3, 2005
  3. 7stud
    Replies:
    11
    Views:
    711
    Dennis Lee Bieber
    Mar 20, 2007
  4. Savas Ates
    Replies:
    1
    Views:
    294
    Patrice
    Aug 17, 2004
  5. babu17
    Replies:
    4
    Views:
    102
    Thomas 'PointedEars' Lahn
    Mar 27, 2006
Loading...

Share This Page