Saturday, March 24, 2012

Maintaining state with a dynamically created TemplateColumn

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 collectio
n
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,
PeteYou have to create dynamically controls upon each postback during the
page.init event handling.
I have a sample code in asp.net 1.1
http://www.societopia.net/Samples/D...tDelegates.aspx
and another in asp.net 2.0
http://www.webswapp.com/codesamples...te/default.aspx
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Pete Moss" wrote:

> 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 DataS
et
> in my Session object and I bind the CheckBox to the DataSet. So far so goo
d.
> 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, tha
t
> during postback (eg, when a Button is clicked), the DataGrid.Items collect
ion
> 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
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
"Phillip Williams" wrote:

> You have to create dynamically controls upon each postback during the
> page.init event handling.
> I have a sample code in asp.net 1.1
> http://www.societopia.net/Samples/D...tDelegates.aspx
> and another in asp.net 2.0
> http://www.webswapp.com/codesamples...te/default.aspx
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>

0 comments:

Post a Comment