FindControl in a gridview's dynamically-added template

Discussion in 'ASP .Net' started by Craig Buchanan, Jul 7, 2008.

  1. I dynamically add data-bound templates to a gridview in my ascx control.
    while this works correctly when the gridview is databound to the
    datatable, i'm having issues on postback.

    i would like to iterate thru the gridview's rows, examine the databound
    controls, then perform a database action. for some reason, i can't find
    the controls.

    i have a two templates: one that uses a label the other uses a textbox
    to display data. when the InstantiateIn fires, i set the Id property
    equal to 'Label_' + the name of the databound column, add the control
    to the container and add the onDataBinding event handler.

    what am i missing?

    thanks,

    Craig buchanan
     
    Craig Buchanan, Jul 7, 2008
    #1
    1. Advertising

  2. Hi Craig

    > i would like to iterate thru the gridview's rows, examine the databound
    > controls, then perform a database action. for some reason, i can't find
    > the controls.

    Code :)

    > i have a two templates: one that uses a label the other uses a textbox
    > to display data. when the InstantiateIn fires, i set the Id property
    > equal to 'Label_' + the name of the databound column, add the control
    > to the container and add the onDataBinding event handler.

    Dito, show relevant Code to this issue.

    How are you binding? On every Page_Load or with the IsPostBack-Constrain?
    Where are you trying to access via .FindControl and how?
    How are you put your dynamically names together "Label_"....?

    --
    Gruss, Peter Bucher
    Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
    http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
    http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
     
    Peter Bucher [MVP], Jul 8, 2008
    #2
    1. Advertising

  3. Peter-

    Thanks for the reply.

    I bind the gridview in the Page_Load when it is not a postback.

    I use a button's click event as a testing harness to examing the contents of
    the gridview during a postback. In this event I loop thru the gridview's
    rows, and attempt to find my textbox or label using findcontrol.

    i dynamically create the templates prior to binding the data and in a
    separate method. in the contructor of the template's InstantiateIn method,
    i create the control (label or textbox) and assign its ID property to either
    Label_[dataMember] or TextBox_[dataMember]. the [dataMember] value is
    passed to the template in the constructor. moreover, the [dataMember]
    values is used in the OnDataBinding event handler. pseudocode :

    Class TextBoxTemplate
    Implements ITemplate

    private _dataMember as string
    Public Sub New()
    End Sub
    Public Sub New(dataMember as string)
    _dataMember=dataMember
    End Sub

    Public Sub InstantiateIn(container as )
    dim tb as new TextBox
    tb.Id = string.concat("Textbox_" ,_dataMember)

    'add event handler (can't remember coding...)

    container.Controls.Add(tb)

    End Sub

    public sub OnDataBinding(...)
    'case sender as textbox
    ...
    'cast sender's container as gridViewRow
    ...
    'bind data (something similar)
    tb.text = DataBinder.Eval(_dataMember)

    end sub

    End Class

    i added logic to recreate the dynamic column in the Page_Init event, which
    seemed to recreate the gridview correctly. i still can't seem to find my
    dynamically-created controls in the gridview, however.

    thoughts?

    thanks a lot.

    Craig

    "Peter Bucher [MVP]" <> wrote in message
    news:...
    > Hi Craig
    >
    >> i would like to iterate thru the gridview's rows, examine the databound
    >> controls, then perform a database action. for some reason, i can't find
    >> the controls.

    > Code :)
    >
    >> i have a two templates: one that uses a label the other uses a textbox to
    >> display data. when the InstantiateIn fires, i set the Id property equal
    >> to 'Label_' + the name of the databound column, add the control to the
    >> container and add the onDataBinding event handler.

    > Dito, show relevant Code to this issue.
    >
    > How are you binding? On every Page_Load or with the IsPostBack-Constrain?
    > Where are you trying to access via .FindControl and how?
    > How are you put your dynamically names together "Label_"....?
    >
    > --
    > Gruss, Peter Bucher
    > Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
    > http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
    > http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
     
    Craig Buchanan, Jul 8, 2008
    #3
  4. Craig Buchanan

    bruce barker Guest

    as your databind creates the dynamic controls, you need to rebind in the
    postback before the onload event. the best place to create dynaic
    control is in CreateChildControls method (which was designed for this).

    -- bruce (sqlwork.com)

    Craig Buchanan wrote:
    > Peter-
    >
    > Thanks for the reply.
    >
    > I bind the gridview in the Page_Load when it is not a postback.
    >
    > I use a button's click event as a testing harness to examing the contents of
    > the gridview during a postback. In this event I loop thru the gridview's
    > rows, and attempt to find my textbox or label using findcontrol.
    >
    > i dynamically create the templates prior to binding the data and in a
    > separate method. in the contructor of the template's InstantiateIn method,
    > i create the control (label or textbox) and assign its ID property to either
    > Label_[dataMember] or TextBox_[dataMember]. the [dataMember] value is
    > passed to the template in the constructor. moreover, the [dataMember]
    > values is used in the OnDataBinding event handler. pseudocode :
    >
    > Class TextBoxTemplate
    > Implements ITemplate
    >
    > private _dataMember as string
    > Public Sub New()
    > End Sub
    > Public Sub New(dataMember as string)
    > _dataMember=dataMember
    > End Sub
    >
    > Public Sub InstantiateIn(container as )
    > dim tb as new TextBox
    > tb.Id = string.concat("Textbox_" ,_dataMember)
    >
    > 'add event handler (can't remember coding...)
    >
    > container.Controls.Add(tb)
    >
    > End Sub
    >
    > public sub OnDataBinding(...)
    > 'case sender as textbox
    > ...
    > 'cast sender's container as gridViewRow
    > ...
    > 'bind data (something similar)
    > tb.text = DataBinder.Eval(_dataMember)
    >
    > end sub
    >
    > End Class
    >
    > i added logic to recreate the dynamic column in the Page_Init event, which
    > seemed to recreate the gridview correctly. i still can't seem to find my
    > dynamically-created controls in the gridview, however.
    >
    > thoughts?
    >
    > thanks a lot.
    >
    > Craig
    >
    > "Peter Bucher [MVP]" <> wrote in message
    > news:...
    >> Hi Craig
    >>
    >>> i would like to iterate thru the gridview's rows, examine the databound
    >>> controls, then perform a database action. for some reason, i can't find
    >>> the controls.

    >> Code :)
    >>
    >>> i have a two templates: one that uses a label the other uses a textbox to
    >>> display data. when the InstantiateIn fires, i set the Id property equal
    >>> to 'Label_' + the name of the databound column, add the control to the
    >>> container and add the onDataBinding event handler.

    >> Dito, show relevant Code to this issue.
    >>
    >> How are you binding? On every Page_Load or with the IsPostBack-Constrain?
    >> Where are you trying to access via .FindControl and how?
    >> How are you put your dynamically names together "Label_"....?
    >>
    >> --
    >> Gruss, Peter Bucher
    >> Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
    >> http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
    >> http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET

    >
    >
     
    bruce barker, Jul 8, 2008
    #4
  5. bruce-

    i'm trying to pivot data in code, while two columns are available at
    design time, the other column (based on a date column) are dynamically
    generated.

    my approach has been such:

    when Not Ispostback

    1). pivot the detailed datatable into a dynamically-created pivoted
    datatable. for example, i am given:
    ET FS DT $
    -- -- -- -
    A A 2008 50
    A B 2008 200
    A B 2009 100

    this is pivoted to:

    ET FS 2008 2009
    -- -- ---- ----
    A A 50
    A B 200 100

    2). add dynamically-created template columns (4 in this example) to the
    datagrid based on the columns in the pivoted datatable. these template
    columns are stored in the viewstate.

    3). bind gridview to pivoted datatable

    during a postback

    1). retrieve the template columns from the viewstate
    2). add them to the gridview
    3). allow gridview to restore data

    from what i've read, i need to add the template columns to the gridview
    in the Page_Init event, but the template columns, contained in the
    viewstate, aren't available until the Page_Load event.

    is there another way to handle this?

    thanks.

    bruce barker wrote:
    > as your databind creates the dynamic controls, you need to rebind in the
    > postback before the onload event. the best place to create dynaic
    > control is in CreateChildControls method (which was designed for this).
    >
    > -- bruce (sqlwork.com)
    >
    > Craig Buchanan wrote:
    >> Peter-
    >>
    >> Thanks for the reply.
    >>
    >> I bind the gridview in the Page_Load when it is not a postback.
    >>
    >> I use a button's click event as a testing harness to examing the
    >> contents of the gridview during a postback. In this event I loop thru
    >> the gridview's rows, and attempt to find my textbox or label using
    >> findcontrol.
    >>
    >> i dynamically create the templates prior to binding the data and in a
    >> separate method. in the contructor of the template's InstantiateIn
    >> method, i create the control (label or textbox) and assign its ID
    >> property to either Label_[dataMember] or TextBox_[dataMember]. the
    >> [dataMember] value is passed to the template in the constructor.
    >> moreover, the [dataMember] values is used in the OnDataBinding event
    >> handler. pseudocode :
    >>
    >> Class TextBoxTemplate
    >> Implements ITemplate
    >>
    >> private _dataMember as string
    >> Public Sub New()
    >> End Sub
    >> Public Sub New(dataMember as string)
    >> _dataMember=dataMember
    >> End Sub
    >>
    >> Public Sub InstantiateIn(container as )
    >> dim tb as new TextBox
    >> tb.Id = string.concat("Textbox_" ,_dataMember)
    >>
    >> 'add event handler (can't remember coding...)
    >>
    >> container.Controls.Add(tb)
    >>
    >> End Sub
    >>
    >> public sub OnDataBinding(...)
    >> 'case sender as textbox
    >> ...
    >> 'cast sender's container as gridViewRow
    >> ...
    >> 'bind data (something similar)
    >> tb.text = DataBinder.Eval(_dataMember)
    >>
    >> end sub
    >>
    >> End Class
    >>
    >> i added logic to recreate the dynamic column in the Page_Init event,
    >> which seemed to recreate the gridview correctly. i still can't seem
    >> to find my dynamically-created controls in the gridview, however.
    >>
    >> thoughts?
    >>
    >> thanks a lot.
    >>
    >> Craig
    >>
    >> "Peter Bucher [MVP]" <> wrote in message
    >> news:...
    >>> Hi Craig
    >>>
    >>>> i would like to iterate thru the gridview's rows, examine the
    >>>> databound controls, then perform a database action. for some
    >>>> reason, i can't find the controls.
    >>> Code :)
    >>>
    >>>> i have a two templates: one that uses a label the other uses a
    >>>> textbox to display data. when the InstantiateIn fires, i set the Id
    >>>> property equal to 'Label_' + the name of the databound column, add
    >>>> the control to the container and add the onDataBinding event handler.
    >>> Dito, show relevant Code to this issue.
    >>>
    >>> How are you binding? On every Page_Load or with the
    >>> IsPostBack-Constrain?
    >>> Where are you trying to access via .FindControl and how?
    >>> How are you put your dynamically names together "Label_"....?
    >>>
    >>> --
    >>> Gruss, Peter Bucher
    >>> Microsoft MVP - Visual Developer ASP / ASP.NET, Switzerland
    >>> http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community
    >>> http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET

    >>
    >>
     
    Craig Buchanan, Jul 10, 2008
    #5
    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. Edward
    Replies:
    4
    Views:
    3,161
    S. Justin Gengo
    Jun 14, 2004
  2. =?Utf-8?B?V2ViTWF0cml4?=
    Replies:
    1
    Views:
    1,717
    Teemu Keiski
    Feb 14, 2006
  3. Replies:
    4
    Views:
    7,763
  4. Craig Buchanan

    FindControl in a gridview's dynamically-added template

    Craig Buchanan, Jul 7, 2008, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    1,031
    Craig Buchanan
    Jul 10, 2008
  5. Craig Buchanan

    FindControl in a gridview's dynamically-added template

    Craig Buchanan, Jul 7, 2008, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    245
    Craig Buchanan
    Jul 10, 2008
Loading...

Share This Page