473,836 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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...w hich 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 2545
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.
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

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

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

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

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?
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 System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable) at System.Data.Common.DbDataAdapter.Update(DataSet dataSet) at TryThis.Form1.save() in C:\Documents and Settings\Nick\My Documents\...
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 then..... what should I do?
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 1997 and back. Unfortunately, it seems does reflect the change on my web form. Perhaps it's caching the data retrieved by the SQLDataAdapter? (All of my code is generated by VS.NET '03. FYI, it works fine when I test on my developer...
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. Call for a free consultation. http://a.uuload.com/Computer-Data-Recovery.htm LiveVault's Online Recovery Service Protect vital data with LiveVault's offsite backup and data storage. http://a.uuload.com/Computer-Data-Recovery.htm
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.