maintaining state for dynamic controls between PostBack() ???
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" )
*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.
Re: maintaining state for dynamic controls between PostBack() ???
> 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
> 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.
|All times are GMT. The time now is 03:49 AM.|
Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.