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

Discussion in 'ASP .Net Web Controls' started by muzzy, Nov 5, 2003.

  1. muzzy

    muzzy Guest

    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
    muzzy, Nov 5, 2003
    #1
    1. Advertising

  2. muzzy

    Jan Limpens Guest

    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
    Jan Limpens, Nov 7, 2003
    #2
    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. =?Utf-8?B?Tm92aWNl?=
    Replies:
    1
    Views:
    476
    John Saunders
    Sep 24, 2004
  2. Tom wilson
    Replies:
    2
    Views:
    2,623
    Tom wilson
    Feb 11, 2005
  3. Tom wilson
    Replies:
    2
    Views:
    559
    Tom wilson
    Feb 14, 2005
  4. =?Utf-8?B?RGVlcHNvbiBUaG9tYXM=?=

    Dropdown is maintaining itz state after postback

    =?Utf-8?B?RGVlcHNvbiBUaG9tYXM=?=, May 30, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    464
    Brock Allen
    Jun 1, 2006
  5. =?Utf-8?B?RGVlcHNvbiBUaG9tYXM=?=

    Dropdown is not maintaining itz state after postback

    =?Utf-8?B?RGVlcHNvbiBUaG9tYXM=?=, May 30, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    600
    Mark Rae
    May 31, 2006
Loading...

Share This Page