DataBinding to a DropDownlist within a FormView

Last post 05-11-2007 5:37 PM by Sojan80. 4 replies.

Sort Posts:

  • DataBinding to a DropDownlist within a FormView

    05-11-2007, 2:33 PM
    • Member
      181 point Member
    • Sojan80
    • Member since 10-28-2004, 3:59 AM
    • WI
    • Posts 190

    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 ?

  • Re: DataBinding to a DropDownlist within a FormView

    05-11-2007, 2:41 PM
    • All-Star
      86,764 point All-Star
    • ecbruck
    • Member since 12-30-2005, 7:39 PM
    • Des Moines, IA
    • Posts 9,209
    • Moderator
      TrustedFriends-MVPs
    Just bind your DropDownList to an ObjectDataSource instead, and let it handle the retrieval of your data.
    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

  • Re: DataBinding to a DropDownlist within a FormView

    05-11-2007, 3:41 PM
    • Member
      181 point Member
    • Sojan80
    • Member since 10-28-2004, 3:59 AM
    • WI
    • Posts 190

    So are you saying all I need to do is change this:

     <asp:DropDownList ID="lstType" runat="server" DataSource="<%#GetContactTypes() %>" SelectedValue='<%#Bind("Type") %>' />

    to this:

     <asp:DropDownList ID="lstType" runat="server" DataSourceId="odsMyDataSource" />

  • Re: DataBinding to a DropDownlist within a FormView

    05-11-2007, 3:51 PM
    • All-Star
      86,764 point All-Star
    • ecbruck
    • Member since 12-30-2005, 7:39 PM
    • Des Moines, IA
    • Posts 9,209
    • Moderator
      TrustedFriends-MVPs
    Pretty much, yes. Although, you do need to properly configure your ObjectDataSource by setting the type and select method.
    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

  • Re: DataBinding to a DropDownlist within a FormView

    05-11-2007, 5:37 PM
    Answer
    • Member
      181 point Member
    • Sojan80
    • Member since 10-28-2004, 3:59 AM
    • WI
    • Posts 190

    Ah! I figured it out.

    It should actually be:

    <asp:DropDownList ID="lstType" runat="server" DataSourceId="odsMyDataSource" DataTextField="myTextFieldName" DataValueField="myValueField" />

Page 1 of 1 (5 items)