Data disappearing on postback in GridView with Dynamic columns

G

Guest

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["trait_name"],
(int)dt.Rows["trait_id"], scoreCategoryList);

field.ItemStyle.HorizontalAlign =
HorizontalAlign.Center;
field.ItemTemplate = new
DataGridTemplate(ListItemType.Item, (string)dt.Rows["trait_name"],
(int)dt.Rows["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.
 
G

Guest

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<string> pCategoryList = new List<string>();

public DataGridTemplate(ListItemType type, string colname, int
traitID, List<string> scoreCats)
{
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);
}

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;
}
}
}
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top