Maintaining state with a dynamically created TemplateColumn

G

Guest

During a postback event, I am having trouble retrieving the state of a
CheckBox Control that I am dynamically adding to a DataGrid Control using
ASP.NET 1.1.

I have no trouble adding the TemplateColumn dynamically. I persist a DataSet
in my Session object and I bind the CheckBox to the DataSet. So far so good.
The CheckBoxes show up and the user interacts with them (checks a few,
unchecks a few).

The problem is that during a postback event, I want to update the DataSet
with the results of what the user did to those CheckBoxes. Problem is, that
during postback (eg, when a Button is clicked), the DataGrid.Items collection
and DataGrid.Columns collection no longer has any knowledge of the
dynamically added TemplateColumn.

I tried setting the CheckBox.EnableViewState = true when I create the
TemplateColumn, but I can't figure out how to get at the dynamically added
CheckBox Controls during postback. Anyone know how to do this?

I have no trouble if I declaratively add the CheckBox TemplateColumns. If I
do that, I can get exactly what I want during postback. But no go when I
create the TemplateColumns dynamically.

Thanks,
Pete
 
G

Guest

Phillip,

Thanks for the reply and the sample,but appears that your sample is doing
something different and is doing a postback on each CheckBox click. That is
what I am trying to avoid.

I believe I found a solution, however. The trick seemed to be in re-creating
the TemplateColumns during the LoadViewState() override on the page. It
simply looks like this:

protected override void LoadViewState(object savedState)
{
base.LoadViewState (savedState);

CreateTemplateColumns();
}

Once that is done, in a postback event such as a button click, the state of
all DataGrid columns is available to me.

The only annoying thing from my POV is that I have to make sure I update the
DataSet with the current CheckBox states in the TemplateColumns. Otherwise,
I lose the state of the CheckBox when the DataGrid gets updated.

Pete
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top