By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,492 Members | 1,289 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,492 IT Pros & Developers. It's quick & easy.

Data disappearing on postback in GridView with Dynamic columns

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.