In a ListView's EditTemplate, I have a DropDownList control. The DropDownList is populated in the ItemDataBound method of the ListView (only way I found to get the list to populate in the EditTemplate....the DropDownList only exists in the EditTemplate; it must be a label in the ItemTemplate).

The list populates, and it all works. The issue I have is when the Update button is clicked and the ItemUpdating method is called, the SelectedIndex of the DropDownList is always zero, no matter which value is selected at the time.

The aspx code, minus the extra templates looks like this:

<asp:ListView ID="ListView1" runat="server" OnItemDataBound="ListView1_ItemDataBound" OnItemUpdating="ListView1_ItemUpdating" OnItemEditing="ListView1_ItemEditing">
    <tr style="">
    <td><asp:DropDownList CssClass="vaultDropDownList" ID="EditStaffList" runat="server" AutoPostBack="false" CausesValidation="True"></asp:DropDownList></td>
       <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
       <asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />

And the codebehind:

protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)
    ListView1.EditIndex = e.NewEditIndex;

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
    if (e.Item.DisplayIndex == ListView1.EditIndex)
        DropDownList ddl = e.Item.FindControl("EditStaffList") as DropDownList;

protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
   DropDownList staff = ListView1.Items[e.ItemIndex].FindControl("EditStaffList") as DropDownList;

   //SQL update stuff will go here
   ListView1.EditIndex = -1;

If I try to populate the DropDownList in the Page_Load method to use IsPostBack, the dropdown control always returns null.

Any help greatly appreciated!

Related posts

Recent Viewed