ASP.Net - ListView - Add A DropdownList

Adding a dropdown control to a ListView

A dropdown list if pretty simple - it works practically the same as it wound on a normal form.

1
2
3
4
5
6
7
8
9
10
11
12
13
<InsertItemTemplate>
<tr>
<td></td>
<td><asp:DropDownList
id="DropDownList_TableCode"
runat="server"
autopostback="true">
<asp:ListItem></asp:ListItem>
<asp:ListItem>Invoice</asp:ListItem>
<asp:ListItem>AdditionalAttribute</asp:ListItem>
</asp:DropDownList>
</td>
:

One problem, is it doesn’t bind very well to a list view control. Consequently if you have one in the InsertItemTemplate you need to add a routine to move it’s contents to a database on save. This is done with the item inserting event.

1
2
3
4
5
6
7
8
9
Protected Sub lv_VendorAccount_ImportSpecs_ItemInserting( _
ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.ListViewInsertEventArgs) _
Handles lv_VendorAccount_ImportSpecs.ItemInserting

Dim TableCode As String _
= CType(lv_VendorAccount_ImportSpecs.InsertItem.FindControl("DropDownList_TableCode"), DropDownList).SelectedValue.ToString
e.Values("TableCode") = TableCode
End Sub

Another problem, is you cannot simply doubleclick the control to bring you to code. I have a template that I paste into the code whenwever I add a dropdown with an autopostback set to true

1
2
3
4
5
6
Protected Sub ddlx_SelectedIndexChanged( _
ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles ddlx.SelectedIndexChanged
'
End Sub