473,327 Members | 1,979 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,327 software developers and data experts.

Data disappearing on postback in GridView with Dynamic columns

I have a web app that I have been working on for the last couple of weeks
trying to solve this problem. One page contains a GridView with four base
columns, and an unknown number of columns to be added depending on the data
source chosen. I create some TemplateColumns based on the data returned for
the selection, and add them into the GridView using the following code:

DataSet dsTraits =
ssql_common.selectRubricTraits(sConnectionString,
Convert.ToInt32(measInstID));

DataTable dt = dsTraits.Tables[0];

for (int i = 0; i < dt.Rows.Count; i++)
{
TemplateField field = new TemplateField();

field.HeaderStyle.HorizontalAlign =
HorizontalAlign.Center;
field.HeaderTemplate = new
DataGridTemplate(ListItemType.Header, (string)dt.Rows[i]["trait_name"],
(int)dt.Rows[i]["trait_id"], scoreCategoryList);

field.ItemStyle.HorizontalAlign =
HorizontalAlign.Center;
field.ItemTemplate = new
DataGridTemplate(ListItemType.Item, (string)dt.Rows[i]["trait_name"],
(int)dt.Rows[i]["trait_id"], scoreCategoryList);

gvMultiEval.Columns.Add(field);
}

gvMultiEval.DataBind();

This will load the page correctly, but on a postback, the contents of the
dynamically generated columns is destroyed, and so is inaccessible. The
columns are still there, but there is no content within the cells. Is there
any way I can get this data to persist somewhere long enough to record the
changes within the GridView so I can save it somewhere? I could then recreate
the GridView and repopulate it, but by the time any event is fired, I have no
access to the data.

Thanks in advance, and please ask questions if you don't understand me. I'd
be glad to give more code if you need it, as well.
Jan 25 '07 #1
2 16934
This is the definition of the Template for the columns. Ignore the fact that
it is called DataGridTemplate. This used to be a DataGrid, but now it's a
GridView, and I just now noticed the name hasn't changed. :)

public class DataGridTemplate : ITemplate
{
private ListItemType pTemplateType;
private string pColumnName;
private int pTraitID;

public int TraitID
{
get { return pTraitID; }
set { pTraitID = value; }
}
private List<stringpCategoryList = new List<string>();

public DataGridTemplate(ListItemType type, string colname, int
traitID, List<stringscoreCats)
{
pTemplateType = type;
pColumnName = colname;
pTraitID = traitID;
pCategoryList = scoreCats;
}

public void InstantiateIn(System.Web.UI.Control container)
{
Literal lc = new Literal();
switch (pTemplateType)
{
case ListItemType.Header:
lc.Text = "<strong>" + pColumnName + "</strong>";
container.Controls.Add(lc);
break;

case ListItemType.Item:
Label lbl = new Label();
lbl.Visible = false;
lbl.Text = TraitID.ToString();
container.Controls.Add(lbl);

RadioButtonList rbl = new RadioButtonList();
rbl.ID = "rbl" + pColumnName;
for (int i = 0; i < pCategoryList.Count; i++)
{
rbl.Items.Insert(i, (string)pCategoryList[i]);
}

TextBox txtComments = new TextBox();
txtComments.ID = "txt" + pTraitID;
txtComments.TextMode = TextBoxMode.MultiLine;
txtComments.Rows = 4;
txtComments.Columns = 30;

container.Controls.Add(rbl);
container.Controls.Add(txtComments);
break;
}
}
}

Jan 25 '07 #2
You should re-create dynamically-created controls on every postback,
preferably in PreInit event.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"vandil" <va****@discussions.microsoft.comwrote in message
news:7F**********************************@microsof t.com...
>I have a web app that I have been working on for the last couple of weeks
trying to solve this problem. One page contains a GridView with four base
columns, and an unknown number of columns to be added depending on the
data
source chosen. I create some TemplateColumns based on the data returned
for
the selection, and add them into the GridView using the following code:

DataSet dsTraits =
ssql_common.selectRubricTraits(sConnectionString,
Convert.ToInt32(measInstID));

DataTable dt = dsTraits.Tables[0];

for (int i = 0; i < dt.Rows.Count; i++)
{
TemplateField field = new TemplateField();

field.HeaderStyle.HorizontalAlign =
HorizontalAlign.Center;
field.HeaderTemplate = new
DataGridTemplate(ListItemType.Header, (string)dt.Rows[i]["trait_name"],
(int)dt.Rows[i]["trait_id"], scoreCategoryList);

field.ItemStyle.HorizontalAlign =
HorizontalAlign.Center;
field.ItemTemplate = new
DataGridTemplate(ListItemType.Item, (string)dt.Rows[i]["trait_name"],
(int)dt.Rows[i]["trait_id"], scoreCategoryList);

gvMultiEval.Columns.Add(field);
}

gvMultiEval.DataBind();

This will load the page correctly, but on a postback, the contents of the
dynamically generated columns is destroyed, and so is inaccessible. The
columns are still there, but there is no content within the cells. Is
there
any way I can get this data to persist somewhere long enough to record the
changes within the GridView so I can save it somewhere? I could then
recreate
the GridView and repopulate it, but by the time any event is fired, I have
no
access to the data.

Thanks in advance, and please ask questions if you don't understand me.
I'd
be glad to give more code if you need it, as well.

Jan 25 '07 #3

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

Similar topics

3
by: theKirk | last post by:
using Visual Studio 2005 C# ASP.NET I know there has to be a simple way to do this....I want to use C# in a code behind for aspx. Populate a GridView from an xml file Add Fields to the...
3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
2
by: JaM | last post by:
Hi all, I have created a gridview vith dynamic textbox columns (they are in variable number, it depends on what things I select from database) aspx code:...
0
by: Tony Hedge | last post by:
Okay I'm back with another issue ;-) Platform is .NET, VB 2005... I created a templace class that I was successfully able to add as a template column to a GridView control - dynamically!!! So...
4
by: Mark Olbert | last post by:
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...
4
by: Don Miller | last post by:
This is a repost of a reproducible problem/bug with GridView with dynamic SQL and binding. Is there a better ASP.NET newsgroup I should post to where MS techs or MVPs take an interest in such...
1
by: =?Utf-8?B?V2VzbGV5IERhdmlzLCBHZW5lcmFsIER5bmFtaWNz | last post by:
I'm moving from years with the datagrid to a new project, .net 2.0, using GridView controls. Per past practice, it is often a lot easier to inject controls (or special formatting) in RowDataBound...
0
by: dcollier9 | last post by:
Hi ! I need to embed a text box in a footer of a GridView's boundfield column to get data prior to a row insert. The textbox is usable and showing up in the web page source using View
0
by: John | last post by:
I'm dynamically creating all the Columns in a GridView at run time and binding it to a DataTable. I have to build it at runtime (using an ITemplate subclass) because the number of columns is based...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.