Maintaining state with a dynamically created TemplateColumn

Discussion in 'ASP .Net' started by =?Utf-8?B?UGV0ZSBNb3Nz?=, Apr 5, 2006.

  1. 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
     
    =?Utf-8?B?UGV0ZSBNb3Nz?=, Apr 5, 2006
    #1
    1. Advertising

  2. 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/DataGridEventDelegates.aspx

    and another in asp.net 2.0
    http://www.webswapp.com/codesamples/aspnet20/itemplate/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 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
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Apr 5, 2006
    #2
    1. Advertising

  3. 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/DataGridEventDelegates.aspx
    >
    > and another in asp.net 2.0
    > http://www.webswapp.com/codesamples/aspnet20/itemplate/default.aspx
    > --
    > HTH,
    > Phillip Williams
    > http://www.societopia.net
    > http://www.webswapp.com
    >
    >
     
    =?Utf-8?B?UGV0ZSBNb3Nz?=, Apr 8, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RWFybCBUZWlncm9i?=

    Maintaining View State For Dynamically Created Controls

    =?Utf-8?B?RWFybCBUZWlncm9i?=, Oct 7, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    2,942
    =?Utf-8?B?RWFybCBUZWlncm9i?=
    Oct 7, 2004
  2. Replies:
    0
    Views:
    428
  3. Earl Teigrob

    Maintaining View State For Dynamically Created Controls

    Earl Teigrob, Oct 7, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    154
    Earl Teigrob
    Oct 7, 2004
  4. Replies:
    3
    Views:
    194
  5. Jules Dekker

    Dynamically created TemplateColumn does not raise SortCommand

    Jules Dekker, Feb 13, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    143
    Jules Dekker
    Feb 13, 2004
Loading...

Share This Page