maintaining state for dynamic controls between PostBack() ???

M

muzzy

Hi all.

Relative newcomer to asp.net. I am constructing an asp.net/C# app
that requires some metadata-based dynamic form creation (shows only
those product details required for the user's chosen product). I
create my controls in Page_Load() and add them to an existing panel.
I then capture the web control data when the user kicks off a
Button_Click() and save it to the database.

My question: without simply recreating them with every Page_Load()
(or OnInit()), is it possible to maintain the state of the dynamically
created controls between PostBacks()?.

I have attempted storing the panel in a session object. Retrieving
this object causes me two problems: the view state is not maintained
(user's changes are not recognized), and the child controls no longer
appear on the panel.

We anticipate some users requiring dozens of web controls, many of
which will be populated via calls to our database (client lists,
etc.). I want to avoid repeating the actions necessary for
constructing them every time a PostBack() occurs. I would imagine
that this is somehow possible, I suspect that using the Session object
is how it is accomplished (and that I am not using it correctly). Any
advice for this newcomer would be most appreciated.

A simple example of my problem:

public class TestControls : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Panel Holder_pnl;
protected System.Web.UI.WebControls.Label display_lbl;

private void Page_Load(object sender, System.EventArgs e)
{
//want to create controls only once!
if (! IsPostBack)
{
TextBox test_tb = new TextBox();
miweb_tb.ID = "webtext";
miweb_tb.Text = "From Page_Load()";
Holder_pnl.Controls.Add( miweb_tb );

Session["panel"] = Holder_pnl;
}

}

private void test_btn_Click(object sender, System.EventArgs e)
{
//attempt to grab info from the session
Holder_pnl = (Panel) Session["panel"];
display_lbl.Text = ( (TextBox) Holder_pnl.FindControl( "webtext" )
).Text;
}

}

actions
*The first load of the page displays the text box inside the panel
with the text "From Page Load()"
*Change text and hit submit – fire off test_btn_click()
*debugger shows that webtext.text has not changed
*text box does not display in panel when re-loaded.

thanks all,
muzzy
 
J

Jan Limpens

hi muzzy,
if (! IsPostBack)
{
TextBox test_tb = new TextBox();
miweb_tb.ID = "webtext";
miweb_tb.Text = "From Page_Load()";
Holder_pnl.Controls.Add( miweb_tb );

Session["panel"] = Holder_pnl;
}
your textbox is only created when the page is loadad with no buttons
clicked (or similar), so the textbox shows up only then. once you click
somewhere postback==true.
private void test_btn_Click(object sender, System.EventArgs e)
{
Holder_pnl = (Panel) Session["panel"];

this replaces any changes you made in the panel by replacing the whole
panel. objects in the session space are not connected to your pages
object. so changes made to the object once posted to session do not
reflect in the session object. i guess this means its there by value,
not by reference.

think about putting your datasource to the session, not the panel. and
try to avoid session objects as much as possible, they suck.

jan
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top