I cannot figure out how to set my selected value after the databind. I store the value in a temp variable and then set it again after the binding but it is not working.
Code Behind
protected void InsertButton_Click(object sender, EventArgs e)
{
var ctrl = (Control)sender;
var lvl = (ListViewItem)ctrl.NamingContainer;
var formSectionListBox = (ListBox)lvl.FindControl("formsection");
var temp = formSectionListBox.SelectedValue;
// Update ListView
ListView1.DataSource = SqlDataSource1;
ListView1.DataBind();
formSectionListBox.Items.FindByValue(temp).Selected = true;
}
ASP.net
<asp:ListView ID="ListView1" runat="server" InsertItemPosition="FirstItem" OnPagePropertiesChanged="ListView1_PagePropertiesChanged" OnItemEditing="ListView1_OnItemEditing" DataKeyNames="FormTitle" OnSelectedIndexChanged="ListView1_SelectedIndexChanged" OnItemCanceling="ListView1_OnItemCanceling" OnItemUpdating="ListView1_ItemUpdating" OnItemInserting="ListView1_ItemInserting" OnItemDeleting="ListView1_ItemDeleting">
<InsertItemTemplate>
<tr>
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" OnClick="InsertButton_Click" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" CausesValidation="False" />
</td>
<td>
<div style="height: auto; width: 250px; overflow: auto; border: solid; border-color: ActiveBorder">
<asp:ListBox ID="formsection" runat="server" DataSourceID="FormSectionDataSource" DataTextField="FormSection" DataValueField="FormSectionID" AppendDataBoundItems="True" SelectedValue='<%# Bind("FormSectionID") %>' Height="150px">
<asp:ListItem Value=""><- please select -></asp:ListItem>
</asp:ListBox>
</div>
</td>
</tr>
</InsertItemTemplate>
</asp:ListView>