Need help retrieving controls dynamically added to GridView

Y

yeltsin27

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top