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