ViewState and re-creating ASP.Net page controls

G

Glenn Owens

I have an ASP.Net page on which there are serveral static controls
(listboxes, radiobuttonlist and textboxes). These controls are used to
create criteria from which the code-behind will dynamically create 1-n
datagrids.

When the Submit button is clicked I need to save (in viewstate) the
contents of the criteria controls so that I can recreate the dynamic
DataGrid(s) in the LoadViewState (overloaded method). I need to do
this so that the DataGrid controls are available for the page's
control heirarchy when the Changed events are serviced (i.e.
Datagrid:SelectedIndexchanged event).

All that being said... I know that I can save the criteria control
values to the ViewState via "brute force" (saving the values of each
control into an array, etc) and then reference the values when the
DataGrids are to be re-created in the LoadViewState. My question is -
is there a more elegant/efficient way of accessing the control values
prior to the Load_Page event?

I know that the listboxes, radiobuttonlist and textboxes are all
properly maintaining state (in their respective postback data
collections - via IPostbackDataHandler) but I don't know how to access
the data from the page prior to Page Load.

I look forward to your input on this, yet-another-headbanger, problem.

Glenn
 
G

Glenn Owens

Kevin, while I appreciate your providing a link to this familiar MSDN
article I am fairly aware of the basics of the page lifecycle.

Perhaps you could provide some insight as to the problem (namely the
re-creation of Dynamic controls based on static non-viewstate - i.e.
post data collection control values). I haven't found any articles on
that particular topic.
 
K

Kevin Spencer

It's a sequence issue. It's not a simple answer, and I didn't understand all
of what you posted, so I posted a reference. However, I will address each
point of your post:
I have an ASP.Net page on which there are serveral static controls
(listboxes, radiobuttonlist and textboxes). These controls are used to
create criteria from which the code-behind will dynamically create 1-n
datagrids.

Define "static controls." A Web Control is not static, and static HTML is
not a Control. The rest of the answer depends upon your answer to this
question, as Controls maintain their own State.
When the Submit button is clicked I need to save (in viewstate) the
contents of the criteria controls so that I can recreate the dynamic
DataGrid(s) in the LoadViewState (overloaded method). I need to do
this so that the DataGrid controls are available for the page's
control heirarchy when the Changed events are serviced (i.e.
Datagrid:SelectedIndexchanged event).

Still confused. Again, the "criteria controls" should maintain their own
State.
All that being said... I know that I can save the criteria control
values to the ViewState via "brute force" (saving the values of each
control into an array, etc) and then reference the values when the
DataGrids are to be re-created in the LoadViewState. My question is -
is there a more elegant/efficient way of accessing the control values
prior to the Load_Page event?

Same problem.
I know that the listboxes, radiobuttonlist and textboxes are all
properly maintaining state (in their respective postback data
collections - via IPostbackDataHandler) but I don't know how to access
the data from the page prior to Page Load.

I don't understand why Page_Load is so important here. Again, it's a
sequencing issue. Page_Load occurs at a certain point in the Cycle. But it
is certainly not necessary to use it at all. What is necessary is to have
code execute in the right sequence. Page_Load occurs after all Controls have
been created, but prior to event-handling.

Finally, again, it sounds like you're using static HTML elements for some of
your Page. If you want them to maintain state, that's what Server Controls
are for. If you are using static HTML form elements, and feel that you must,
their values are constantly avaiable on the server via the Request.Form
Collection. You could certainly grab their values from the Request.Form
Collection, and put them back in the HTML when rendering your static HTML,
but again, that's what Server Controls are for.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
G

Glenn Owens

OK, please forgive my frustration level as I've been working on this
problem for some days.

I believe that I've found the last remaining problem. Apparently, for
some unknow reason, if I add a control (i.e. DataGrid) dynamically to
the .aspx page followed by a DataBind everything seems to work. If, on
the other hand, I perform the same DataBind anywhere "inside" the
ASP.Net container (i.e. TableRow, Panel, etc) the DataGrid is rendered
but the SelectedIndexChanged never fires.

So, please illuminate me as to this little idiosyncracy with ASP.Net.
This puts a real crimp in the flexibility of the page I'm designing.

I have n-DataGrids being dynamically generated based on Usre input from
a variety of APS.NET controls on the page. Thus far I have insured that
the DataGrid(s) are successfully re-created in the LoadListView (so the
the events are "wired-up" by the time of the Load_Page). Currently, in
the page, the datagrids are arranged in n-up fashion within a panel
(overflow:Auto)/table/tablerow(s)/tablecell(s). This nice visual
arragement will need to be changed if I can't find some work-around in
fairly short order.

Kevin, you've been more than patient - and I really do appreciate the
information that you've provided. Please, help with this last issue.

Thanks,
Glenn
 
K

Kevin Spencer

Hi Glenn,

If it's any consolation, sequence is one of the most difficult aspects of
ASP.Net to master, so when this is over, you should be a much stronger
developer!

The expression "followed by a DataBind" is not precise enough to tell me
what's going on. the DataBind method is common to ALL Server Controls, and
it binds the data in a Server Control as well as all Server Controls
contained within that Server Control. So, if you call Page.DataBind() you
are DataBinding all Controls in the Page. But if you call DataGrid.DaaBind()
it binds only that DataGrid, and all child Controls of that DataGrid.

I can tell you this much: This is definitely a sequencing issue.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
G

Glenn Owens

OK, here's the lastest status. I've delayed the DataBind on all
DataGrids until the panel, table, rows, cells have been
created/populated. Once these "container" controls are in place I then
cycle through the control list and locate the DataGrids and perform the
appropriate DataBind. That part works find (the DataGrids are displayed
and the User can click on a row).

Unfortunately, when the a row is clicked I now get the following error:

=============================
=============================


Server Error in '/Disbursement/Dashboard' Application.
------------------------------------------------------------------------
--------

An error has occurred because a control with auto-generated id
'AggregateDataGridInvalid' could not be located to raise a postback
event. To avoid this error, explicitly set the ID property of controls
that raise postback events.

Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Web.HttpException: An error has occurred
because a control with auto-generated id 'AggregateDataGridInvalid'
could not be located to raise a postback event. To avoid this error,
explicitly set the ID property of controls that raise postback events.

Source Error:

An unhandled exception was generated during the execution of the current
web request. Information regarding the origin and location of the
exception can be identified using the exception stack trace below.

Stack Trace:


[HttpException (0x80004005): An error has occurred because a control
with auto-generated id 'AggregateDataGridInvalid' could not be located
to raise a postback event. To avoid this error, explicitly set the ID
property of controls that raise postback events.]
System.Web.UI.Page.ProcessPostData(NameValueCollection postData,
Boolean fBeforeLoad) +854
System.Web.UI.Page.ProcessRequestMain() +621

=============================
=============================

I have maually "Id'd" each dynamic control in the control list so, I
suspect that there's something else going on here. I'm going to add some
exception catching logic to investigate further. You know.... this
shouldn't be this difficult!!!

Glenn
 

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,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top