Dynamic Controls on a Multi-Step Form

L

Leo J. Hart IV

OK, here's another question for the experts:

I am building a multi-step (3 steps actually) form using a panel for
each step and hiding/displaying the appropriate panel/panels depending
on which step you're on. This all works fine, but I ran into some
trouble when I started creating controls dynamically in my code-behind
file.

Each panel contains a table which is filled with various radio buttons,
text fields and the such which are built in the code-behind file using
table.rows.add, row.cells.add, cell.controls.add, etc.

Originally I was building all of these either in the Page_Load->Not
IsPostBack section of my code-behind (for the initial panel view) and in
the "Next" button Click event handlers for the panels that followed.

This apparently does not work because the ViewState is not maintained.
Dynamic controls must be created in the Page_Load or Page_Init methods
in order for them to be properly filled with the appropriate ViewState.

So now I have all of these panels being built in the Page_Load method
EVERY time the page is loaded (not just the first time). This
definitely works - I'm seeing the results I expected - but it just feels
wasteful to be building all three panels every time I load the page even
though I am only displaying one at a time.

I'd like to be able to only build the one panel I intend to display, but
since the Page_Load occurs prior to the event handling section of a
page's life cycle, I have no way of knowing which button was pushed and
thus which panel is being requested.

So basically I'm wondering the following:

1. Is there a way for me to determine which button was pushed in the
Page_Load method?
2. Regardless of the answer to #1, am I required to load all dynamic
controls each page load to maintain their viewstate even though I may
not be displaying them this particular page load?
3. If I am required to load all dynamic controls, can you explain how
.NET handles this in an efficient manner as at first look it seems like
a lot of additional processing.
4. What best practices are people using when developing multi-stepped
forms with dynamic controls. We have a ton of projects like these and I
want to create a template to be used for the future.

Thanks for all of your time and patience.

Leo Hart
 
H

Hermit Dave

if you have a fixed set of controls then avoid using dynamically added
controls.

1. i am not aware of this one. though it should be there... i just never
needed to know so never bothered :)

2. you answered your own question.
This apparently does not work because the ViewState is not maintained.
Dynamic controls must be created in the Page_Load or Page_Init methods
in order for them to be properly filled with the appropriate ViewState.

3. the framework goes through lot of steps from IIS passing on a request to
ISAPI filters to actual creation of an instance of the requested page class
for a page's life cycle. it goes through creating the child controls. then
loads the viewstate. then calls page_load then calls the specific event
handler. then renders the output before channelling it across
this blog details a lot of articles by a lot of people
http://weblogs.asp.net/eporter/archive/2003/07/15/10109.aspx

msdn article
http://msdn.microsoft.com/library/d.../en-us/dnaspp/html/aspnet-pageobjectmodel.asp

4. i will leave this one for someone else to ponder. i dont use the above
mechanism it would be wrong of me to answer this.

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
 
G

Guest

I use the same mechanism as you wrote: multiple Panels and user switch panel
visibility to view next/previous step. The biggest form I have is 11 steps
long.

I. My current event model usage.
===========================
I use these event mechanism points to work:

1. in Page_Load you maintain basic activities such as athentication checks
and else


2. the next point is catching events from controls such as
subOnStep1_Go
subOnStep2_Back
onStepCommands("Step1", "Go")
onStepCommands("Step2", "Back")

there you can decide which step to show
process input, store or update data into db and elsewhere


3. Page_Prerender

here you have all events processed
so you can just load updated data from db into selected step


II. Catching user input
===========================

How to catch dinamically added RadioButtons Checkboxes selections

1. You can add checkboxes with clearly identifiable names
and catch their selections in the incoming Form by names
such as:

checkbox[DataRowId]_[WhatToDo]
example:
<input type=checkbox name=checkbox2315_Delete value=1>
<input type=checkbox name=checkbox2316_Delete value=1>
<input type=checkbox name=checkbox2317_Delete value=1>

or

checkbox[StepNumber_WhatToDo] with a value containing [DataRowId]
example:
<input type=checkbox name=checkboxStep1_Delete value=2315>
<input type=checkbox name=checkboxStep1_Delete value=2316>
<input type=checkbox name=checkboxStep1_Delete value=2317>
so the
Request.Form("checkboxStep1_Delete") contains "2315,2316,2317"

when you do like this you can just render controls into Literal or Placeholder
txtStep1Data.text &= "<input type=checkbox name=checkboxStep1_Delete
value=2315>"
txtStep1Data.text &= "<input type=checkbox name=checkboxStep1_Delete
value=2316>"
txtStep1Data.text &= "<input type=checkbox name=checkboxStep1_Delete
value=2317>"


2. You can use DataGrid with row templates
in these templates you define Checkbox control
with an event OnServerChange pointing to your procedure

and, when adding rows to datagrid,
you must specify correct ID of your objects
so you can get it later in your event handling procedure
through Sender object parameters


so, this all works as it is described
I often use the first method cause of it is simplier to make
you just render html string yourself and it is just works

do not forget to switch off Viewstate for an output control
if the rendered text grows up too large
 
L

Leo J. Hart IV

Thank you both for your responses!

From the sound of the first answer, it seems I may be required to build
all of these dynamic controls each load, regardless of whether they will
be displayed. I would like to figure out if you can determine which
button was pressed in the Page_Load method.

On the second answer, although I can see how using plain html controls
and Request.Form would work, I'd really like to make use of the new
features in ASP.NET. For instance, I need to know if a particular
control is enabled or disabled and it's much easier (and cleaner) to
modify/reference a controls properties via an ASP.NET control. The
datagrid approach might be worth looking into, but won't I still have
the same problem when I process form postings? Won't I still need to
create the controls each load to reference them?
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top