How about declaring it in the <EditItemTemplate> section, and then
populating it during the OnItemDatabound event? You need to check the the
ListItemType to make sure you only do this for rows that are in edit mode.
Sub MyItemDataboundhandler(objSender As Object, objArgs As
DataListItemEventArgs)
'sets value of DropDownList in each row to current Shipper name
'see what type of row caused the event
Dim objItemType As ListItemType = CType(objArgs.Item.ItemType,
ListItemType)
'only set the row variations for an EditItem row, which occurs only
'once for each page load, and contains the EditItemTemplate content
If objItemType = ListItemType.EditItem Then
'get a reference to the asp:DropDownList control in this row
Dim objList As DropDownList =
CType(objArgs.Item.FindControl("lstShipper"), DropDownList)
'get Shippers DataSet using function elsewhere in this page
Dim objShipDataSet As DataSet = GetShippersFromSessionOrServer()
objList.DataSource = objShipDataSet
objList.DataTextField = "ShipperName"
objList.DataValueField = "ShipperID"
objList.DataBind()
'objArgs.Item.DataItem returns the data for this row of items
Dim objRowVals As DataRowView = CType(objArgs.Item.DataItem,
DataRowView)
'get the Shipping Company ID of the item in the DataRowView
Dim intShipperID As Integer = objRowVals("ShipVia")
Dim objItem As ListItem
For Each objItem In objList.Items
If objItem.Value = intShipperID Then
objItem.Selected = True
End If
Next
End If
"Dave" <Da**@discussions.microsoft.com> wrote in message
news:4F**********************************@microsof t.com...
Hi,
I'm building a maintenance form for a table and some of the fields are
textboxes (i.e name) and some should be dropdowns (i.e country of origin)
When a user clicks 'Edit' in the <asp:EditCommandColumn> I want either a
textbox or dropdown to appear when I check what is being edited.
I thought I could create the dropdown on the fly and add it to the
datagrid as below but It's not appearing. I imagined I could populate the dropdown
with codes at this point too.
private void dgCompany_Edit(Object sender, DataGridCommandEventArgs e)
{
dgCompany.EditItemIndex = e.Item.ItemIndex;
DropDownList ddwn = new DropDownList();
ddwn.ID = "dynamicDdwn";
e.Item.Cells[0].Controls.Add(ddwn);
}
Is this approach valid? I thought of maybe creating BOTH the textbox and
the dropdown at design time and hide or show the control in the Edit event
using
((TextBox) e.Item.FindControl("txtValue")).Visible = false;
or
((DropDownList) e.Item.FindControl("listValue")).Visible = false;
but I got 'object not set to instance of object'