Using Imar Spaanjars' model for layered web applications, I have built a small one of my own. Inside a FormView I have a DropDownList containing states. I am trying to set the SelectedValue of the DropDownList to the value that is in an object named Contact.cs. I have tried these two techniques and neither one works:
Technique #1:
The DropDownList displays the correct state the first time but the PREVIOUS state on subsequent selections in the parent GridView.
Code:
<asp:DropDownList ID="ddlAddState" runat="server"
SelectedValue='<%# Bind("state") %>'
AppendDataBoundItems="True"
DataSourceID="odsStates3" DataTextField="abbr"
DataValueField="abbr" TabIndex="8">
<asp:ListItem>Select...</asp:ListItem>
</asp:DropDownList>
<asp:ObjectDataSource ID="odsStates3"
runat="server" SelectMethod="SelectStates"
TypeName="GeneralDB">
</asp:ObjectDataSource>
Technique #2:
The Debug line displays the correct state but the DropDownList displays the PREVIOUS state.
Code:
protected void fvContact_DataBound(object sender, EventArgs e)
{
if (fvContact.CurrentMode == FormViewMode.Edit)
{
Contact myContact = null;
IEnumerable ie = odsFvContacts.Select();
if (ie != null)
{
IEnumerator num = ie.GetEnumerator();
if (num.MoveNext())
{
myContact = (Contact)num.Current; //Gets the class
}
}
Debug.WriteLine("fvContact_DataBound: " + myContact.State);
DropDownList ddl = (DropDownList)fvContact.FindControl("ddlState");
ddl.ClearSelection();
ddl.SelectedIndex
= ddl.Items.IndexOf(ddl.Items.FindByText(myContact.State));
}
}
I would be happy to post more code if that would be helpful to the community.