Finding edititem control names in datagrid at runtime

R

rohan_p

I'm trying to make a generic class that will manage an editable datagrid.
Part of the functionality of the class is that it shouldn't need to know the
names or types of the input fields - they will get passed in inside a panel,
and it will loop through the panel's control collection to extract the data
and create SP parameters on the fly.

This is OK for inputing (since the input fields are not part of the
datagrid) and deleting, but I've run into a problem when trying to edit a row
- I can't find a way of finding out the names of the controls. I can't even
find out if the controls are in some sort of container.

Of course, usually you find the edit mode controls by using Findcontrol(),
but you need to know the control's name. Looping through the controls or
items collection of the DataGrid doesn't seem to work.

The only alternative I can think of is to extrace the data in the page and
store it in some sort of data structure, and pass this in, but it isn't what
I had in mind.

Is there any way to get references to the EditItem controls at runtime?
 
R

rohan_p

I figured out one way to doing it. Perhaps there are simpler ones:

For i As Integer = 0 To grid.Items.Count - 1
Select Case grid.Items(i).ItemType
Case ListItemType.EditItem
'Get the cells of the editable row
Dim tccCells As TableCellCollection = grid.Items(i).Cells

For j As Integer = 0 To tccCells.Count - 1
Dim tcCell As TableCell = tccCells.Item(j)
'Get the child controls of each cell
Dim colControls As ControlCollection = tcCell.Controls
'Examine each control type
For k As Integer = 0 To colControls.Count - 1
Select Case GetControlType(colControls.Item(k))
'library function
Case "TextBox"
Dim t As TextBox = colControls.Item(k)
recordset.AddParameter("@" &
GetFieldName(t.ID), Trim(t.Text))
Exit Select

<-- Other cases -->

Case Else
Exit Select
End Select
Next
Next
Exit Select
End Select
Next
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top