467,182 Members | 1,042 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,182 developers. It's quick & easy.

Need help retrieving controls dynamically added to GridView

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 '06 #1
  • viewed: 1602
Share:

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by mangomadman | last post: by
4 posts views Thread by Mark Olbert | last post: by
5 posts views Thread by Nathan Sokalski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.