I am trying to rework some code from an article I read where they are populating a drop down list located inside a form view by getting the values from an enum class. I want to be able to populate the drop down list from a database table instead so I can add values later if I want to.
I've written a set of classes to get the name value pairs from the database table but I am not sure how to bind the drop down list to the database table so that it gets the values there.
The function used in the article to get the values from the enums looks like this:
public string[] GetContactTypes()
{
// Code based onh ttp://www.codeproject.com/cs/miscctrl/enumedit.asp
List<string> myList = new List<string>();
FieldInfo[] myEnumFields = typeof(ContactType).GetFields();
foreach (FieldInfo myField in myEnumFields)
{
if (!myField.IsSpecialName && myField.Name.ToLower() != "notset")
{
myList.Add(myField.Name);
}
}
return myList.ToArray();
}
and is located in the codebehind file for the .aspx page. THey reference this function this way from within the formview:
<asp:DropDownList ID="lstType" runat="server" DataSource="<%#GetContactTypes() %>" SelectedValue='<%#Bind("Type") %>' />
So how can I bind the drop down list here to the database table so I don't have to use the enums??
What's the syntax for databinding a dropdown list contained in a formview to a dtabase table ?