Problem with webcontrol state using PlaceHolder within ASCX file

Discussion in 'ASP .Net Web Controls' started by benoitc@gmail.com, Feb 28, 2008.

  1. Guest

    Hello, I've been trying to solve this problem for a while now, I
    wonder if anyone can help. Here is the scenario. Forgive me if it
    seems a little convoluted, my organization uses MCMS templates and it
    means things can get more complicated in order to work with the
    templates.

    Form1.aspx -> contains a placeholder (ph1) and nothing else, this is
    the page that is viewed in the browser

    User1.ascx -> this is loaded into the placeholder on Form1 (ph1)
    (using ph1.Controls.Add(Page.LoadControl) )

    User1.ascx contains its own placeholder as well (ph2), as well as a
    button. It creates a dynamic control (a textbox) within the OnInit()
    part of its codebehind, and adds it to the placeholder (ph2) using
    ph2.Controls.Add(TextBox1). This takes place in the OnInit() area so
    that the dynamic controls are preserved even after postbacks.

    When running Form1, if after a postback, you try and capture the value
    of the textbox by creating a new object using ph2.FindControl(), it
    manages to find the textbox (the FindControl() method does not return
    null), but the text parameter is always blank.

    The strange thing though, is that when you are viewing the page itself
    in the browser, any text that you entered before the postback is still
    visible on the screen afterwards. So I don't believe it's a problem
    with the ViewState, since the information is actually being preserved
    between postbacks. Rather the information seems to be "lost" when you
    try to load the textbox using FindControl() after the postback in the
    Page_Load area. The control is found, but the Text parameter is
    always blank.

    Very strange. Any ideas?
     
    , Feb 28, 2008
    #1
    1. Advertising

  2. Phil H Guest

    On 28 Feb, 20:08, "" <> wrote:
    > Hello, I've been trying to solve this problem for a while now, I
    > wonder if anyone can help.  Here is the scenario. Forgive me if it
    > seems a little convoluted, my organization uses MCMS templates and it
    > means things can get more complicated in order to work with the
    > templates.
    >
    > Form1.aspx -> contains a placeholder (ph1) and nothing else, this is
    > the page that is viewed in the browser
    >
    > User1.ascx -> this is loaded into the placeholder on Form1 (ph1)
    > (using ph1.Controls.Add(Page.LoadControl) )
    >
    > User1.ascx contains its own placeholder as well (ph2), as well as a
    > button.  It creates a dynamic control (a textbox) within the OnInit()
    > part of its codebehind, and adds it to the placeholder (ph2) using
    > ph2.Controls.Add(TextBox1).  This takes place in the OnInit() area so
    > that the dynamic controls are preserved even after postbacks.
    >
    > When running Form1, if after a postback, you try and capture the value
    > of the textbox by creating a new object using ph2.FindControl(), it
    > manages to find the textbox (the FindControl() method does not return
    > null), but the text parameter is always blank.
    >
    > The strange thing though, is that when you are viewing the page itself
    > in the browser, any text that you entered before the postback is still
    > visible on the screen afterwards.  So I don't believe it's a problem
    > with the ViewState, since the information is actually being preserved
    > between postbacks.  Rather the information seems to be "lost" when you
    > try to load the textbox using FindControl() after the postback in the
    > Page_Load area.  The control is found, but the Text parameter is
    > always blank.
    >
    > Very strange.  Any ideas?


    Hi

    ViewState only preserves the visible content of controls (e.g. Text in
    a textbox) even when they are created programmatically so long as the
    code used to construct them is re-executed on postback (the page
    ViewState data is then re-linked to the originating control). Any
    other type of data created programatically but varies between
    postbacks will be lost. Use Session states instead.
     
    Phil H, Feb 29, 2008
    #2
    1. Advertising

  3. Guest

    I actually figured out the problem if this helps anybody else. When I
    was loading the ascx file on the aspx page using LoadControl(), I was
    doing this from the Page_Load area. If the web user control is loaded
    in the OnInit area, there is no problem on postbacks.

    On Feb 29, 4:56 pm, Phil H <> wrote:
    > On 28 Feb, 20:08, "" <> wrote:
    >
    >
    >
    > > Hello, I've been trying to solve this problem for a while now, I
    > > wonder if anyone can help. Here is the scenario. Forgive me if it
    > > seems a little convoluted, my organization uses MCMS templates and it
    > > means things can get more complicated in order to work with the
    > > templates.

    >
    > > Form1.aspx -> contains a placeholder (ph1) and nothing else, this is
    > > the page that is viewed in the browser

    >
    > > User1.ascx-> this is loaded into the placeholder on Form1 (ph1)
    > > (using ph1.Controls.Add(Page.LoadControl) )

    >
    > > User1.ascxcontains its own placeholder as well (ph2), as well as a
    > > button. It creates a dynamic control (a textbox) within the OnInit()
    > > part of its codebehind, and adds it to the placeholder (ph2) using
    > > ph2.Controls.Add(TextBox1). This takes place in the OnInit() area so
    > > that the dynamic controls are preserved even after postbacks.

    >
    > > When running Form1, if after apostback, you try and capture the value
    > > of the textbox by creating a new object using ph2.FindControl(), it
    > > manages to find the textbox (the FindControl() method does not return
    > > null), but the text parameter is always blank.

    >
    > > The strange thing though, is that when you are viewing the page itself
    > > in the browser, any text that you entered before thepostbackis still
    > > visible on the screen afterwards. So I don't believe it's a problem
    > > with theViewState, since the information is actually being preserved
    > > between postbacks. Rather the information seems to be "lost" when you
    > > try to load the textbox using FindControl() after thepostbackin the
    > > Page_Load area. The control is found, but the Text parameter is
    > > always blank.

    >
    > > Very strange. Any ideas?

    >
    > Hi
    >
    > ViewStateonly preserves the visible content of controls (e.g. Text in
    > a textbox) even when they are created programmatically so long as the
    > code used to construct them is re-executed onpostback(the pageViewStatedata is then re-linked to the originating control). Any
    > other type of data created programatically but varies between
    > postbacks will be lost. Use Session states instead.
     
    , Mar 7, 2008
    #3
    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. Quentin
    Replies:
    1
    Views:
    508
    Quentin
    Jul 29, 2003
  2. Bert
    Replies:
    3
    Views:
    1,948
    Juan T. Llibre
    Dec 12, 2006
  3. nail

    Build control in a placeholder or like a placeholder

    nail, Sep 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    360
  4. david mccormack
    Replies:
    1
    Views:
    261
    Ishigava Butahhi
    Aug 28, 2003
  5. Replies:
    2
    Views:
    553
Loading...

Share This Page