Problem with webcontrol state using PlaceHolder within ASCX file


B

benoitc

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?
 
Ad

Advertisements

P

Phil H

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.
 
Ad

Advertisements

B

benoitc

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.
 

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

Top