473,395 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Data-driven Templated Controls (GridView)

I'm running into a well-described issue in the ASPNET model that I haven't found a good work around for.

I have a GridView to which I dynamically add data-bound TemplateFields at run-time. The specific fields that are added depend on the
state of the application.

The contents of these fields displays properly after they are first initialized, but not after postback (e.g., just a simple button
control that doesn't do anything except trigger the postback). This is the well-described issue -- it's apparently due to the fact
that they dynamically added controls must be recreated in the page life cycle before the point where viewstate is loaded.

My problem is that the specific controls that are added depend on the >>state<< of the application...which is itself stored in
viewstate (well, actually controlstate, but for this discussion I don't think that should matter).

So I think I have a chicken-and-egg situation: I have to recreate the dynamically-added TemplateFields >>before<< viewstate is
loaded, but I don't know which TemplateFields to add until >>after<< viewstate is loaded.

I hope that's a reasonably clear description of the problem/issue. Is there an approach that lets me resolve this conundrum? Is
there a way to load or examine viewstate/controlstate "early"?

- Mark
Apr 4 '07 #1
4 2520
Hi Mark,

Dynamically added controls don't have to be recreated before the point
where viewstate is loaded. For example, you could dynamically create some
control during Page_Load and they will still get state restored correctly.
The reason it may work is because the Controls.Add() method recursively
loads the parent's view state into its children, even though the load view
state stage has passed. (See
http://msdn2.microsoft.com/en-us/library/ms972976.aspx, section "View State
and Dynamically Added Controls")
I'm not sure about your exact requirement regarding dynamically added
TemplateField for GridView, therefore I cannot guarantee this will also
work in your case. I can depict more if you could some of your code.
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Apr 4 '07 #2
Walter,

Thanx for the link to the article. I'll play around with the problem some more and post the results here.

Something that I didn't mention earlier is that not all of the dynamically-added controls demonstrate this problem. Here's what I'm
doing in pseudo-code when I first update the GridView dynamically:

Add CommandField control to GridView
Add TemplateField control to GridView
Add TemplateField control to GridView
Add BoundField control to GridView
....
DataBind() GridView
....
Render Page

On postback, if I do nothing (i.e., don't add the dynamically-added field controls again), the CommandField and BoundField controls
display their values. It's the TemplateFields that don't show up.

Adding all the field controls on postback as early as I can gives the same result: the BoundField and CommandField show up, the
TemplateFields do not. I haven't tried databinding on postback, but I suspect that will cause the TemplateFields to display
properly.

So the issue seems to involve TemplateFields not retaining the results of having been bound to data. Is there something I need to do
to get them to retain the info? Or do TemplateFields just require being re-bound on each postback?

- Mark
Apr 4 '07 #3
Walter,

Some more information, and -- I hope! -- a solution.

In all my previous experimentation I was focusing on recreating the >>columns<< in the GridView on postback. Unfortunately, changing
the columns on postback doesn't do a thing to the child controls embedded in the GridView.

The solution I think I've found is to recreate the >>controls<< in the GridView cells instead. When you do that, they load their
viewstate and display properly. As an aside, I also specifically assigned IDs to the controls created in the TemplateFields because
I've run into problems in other areas when you just leave it up to .NET to assign IDs to dynamically-created controls.

In pseudo-code what I do is this:

On initial dynamic configuration of the GridView:

foreach column to be added
if the column is templated
Create TemplateField
Add instance of custom ITemplate object to TemplateField's ItemTemplate.

The custom ITemplate object's InstantiateIn() method creates a Label to hold information and assigns it a unique ID (I use Labels
because I just need to display data). The unique ID just needs to be unique within a row, since the GridViewRow is the
NamingContainer. I use something like "lbl<name of data field that the column is displaying>".

On postback:

foreach column in the GridView
if column is TemplateField
foreach row in GridView
create an instance of Label (with the same unique ID as used in initial configuration)
add instance of Label to controls for the column's cell in the row

This restoration needs to take place after the InitComplete event (I do it in the Load event handler) because before then the rows
have not been restored to the GridView.

- Mark
Apr 4 '07 #4
Hi Mark,

Thanks for sharing your solution with the community.

Yes it's recommended to assign an unique ID to each dynamically created
control to make sure correct state is restored correctly; otherwise the
order of the controls that are added to the container will be used to
restore the state.
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Apr 5 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Chris | last post by:
Could someone please provide me an effective means of exporting data from a data set (or data grid) to Excel?
0
by: NicK chlam via DotNetMonster.com | last post by:
this is the error i get System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. at System.Data.Common.DbDataAdapter.Update(DataRow dataRows, DataTableMapping tableMapping) at...
5
by: Gene | last post by:
What can I do if I want to get the result using the sql command? for example, the select command is "select Name from Employee where StaffID=10" How to get the "Name"??? dim Name as string and...
5
by: DC Gringo | last post by:
I am having a problem reading a simple update to the database. Basically I'm testing a small change to the pubs database -- changing the price of the Busy Executive's Database Guide from 19.99 to...
0
by: Winder | last post by:
Computer Data Recovery Help 24/7 Data recovering tools and services is our focus. We will recover your data in a cost effective and efficient manner. We recover all operating systems and media....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.