Need help retrieving controls dynamically added to GridView

Discussion in 'ASP .Net' started by yeltsin27@yahoo.co.uk, Jul 18, 2006.

  1. Guest

    I need some advice on handling dynamically added controls in a
    GridView.

    My app takes an uploaded CSV file containing addresses, converts it to
    a DataTable, databinds the DataTable to a GridView, accepts information
    via dropdowns embedded in the first row of the GridView about which
    incoming fields should go to which database fields, and writes the data
    to the database.

    The incoming data has an arbitrary number of columns so I create the
    GridView dynamically. When I create the DataTable I add an extra row at
    the beginning so that the first line of the GridView will contain no
    data. I then add the dropdown controls dynamically to each of the cells
    in this row, giving each dropdown a unique ID, ie

    dt.Rows.InsertAt(r, 0)
    GridView1.DataSource = dt
    GridView1.DataBind()

    Dim gvr As GridViewRow
    gvr = GridView1.Rows(0)
    Dim j As Integer
    For j = 0 To gvr.Cells.Count - 1
    Dim dd As New DropDownList
    dd.ID = "DropDownList" & j.ToString.Trim
    dd.Items.Add("- nothing selected -")
    dd.Items.Add("Addr Line 1")
    dd.Items.Add("Town")
    dd.Items.Add("Country")
    dd.Items.Add("etc")
    gvr.Cells(j).Controls.Add(dd)
    Next

    That all seems to work fine. The GridView appears and the first row
    contains dropdowns.

    But when I do the postback and loop through GridViewRow(0) to retrieve
    each DropDownX.SelectedValue, either in Page_Load or GridView1_Load,
    the dropdowns have disappeared. The first row is blank. The following
    rows show the actual data as normal.

    I understand I have to recreate the dropdowns on each postback as
    they're dynamically created, but I can't even get that far.

    Maybe I don't know quite enough about the control lifecycle to
    understand what's going on. What do I need to do to be able to
    retrieve the dropdowns?

    Thanks,

    Chris N
     
    , Jul 18, 2006
    #1
    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. Jeffrey Todd
    Replies:
    1
    Views:
    4,174
    Peter Blum
    Jun 2, 2005
  2. Dennis Fazekas
    Replies:
    5
    Views:
    4,933
    =?Utf-8?B?RGVubmlzIEZhemVrYXM=?=
    Jan 1, 2006
  3. Nathan Sokalski

    Retrieving values from dynamically added controls

    Nathan Sokalski, Sep 9, 2007, in forum: ASP .Net
    Replies:
    7
    Views:
    1,269
    Scott Roberts
    Nov 20, 2007
  4. Nathan Sokalski

    Retrieving values from dynamically added controls

    Nathan Sokalski, Sep 9, 2007, in forum: ASP .Net Building Controls
    Replies:
    6
    Views:
    979
    Beatrix
    Nov 20, 2007
  5. Nathan Sokalski

    Retrieving values from dynamically added controls

    Nathan Sokalski, Sep 9, 2007, in forum: ASP .Net Web Controls
    Replies:
    7
    Views:
    258
    Scott Roberts
    Nov 20, 2007
Loading...

Share This Page