Gabriel Teixeira
Gabriel Teixeira

Reputation: 41

Error using Databind() in an object for a dropdownlist

I'm learning webforms for my internship and currently things are not going well.

I'm in a project that need to use a web api instead of a database to get the data needed in a system built in webforms.

To simplify my problem lets say I want to bind an object to a dropdownlist.

Here's my class model

namespace BindingDataToADropdownlist
{
    public class State
    {
        public int id;
        public string sigla;
        public string name;
    }
}

Here's the .aspx with a simple dropdownist

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="BindingDataToADropdownlist._Default" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:DropDownList ID="CboState" runat="server" Height="56px"  Width="129px">
</asp:DropDownList>
</asp:Content>

And here's the code behind

namespace BindingDataToADropdownlist
{
    public partial class _Default : Page
    {      
        protected void Page_Load(object sender, EventArgs e)
        {
            List<State> listStates = new List<State>();
            listStates.Add(new State() { id = 33, sigla = "RJ", name = "Rio de Janeiro" });
            listStates.Add(new State() { id = 34, sigla = "SP", name = "Sao Paulo" });
            
            CboState.DataTextField = "name";
            CboState.DataValueField = "id";
            CboState.DataSource = listStates;
            CboState.DataBind();
        }
    }
}

I`m getting the error

"DataBinding: 'BindingDataToADropdownlist.State' does not contain a property with the name 'name' "

If i don't use

CboState.DataTextField = "name";
CboState.DataValueField = "id";

The dropdownlist works but shows BindingDataToADropdownlist.State in both itens as expected.

How can I bind the ID and name correctly?

Upvotes: 0

Views: 104

Answers (2)

Ryan Zeck
Ryan Zeck

Reputation: 51

Using your example, we can populate the drop down list by building and adding the list items manually.

private void AddStatesToDDL()
    {
        List<State> states = new List<State>();

        states.Add(new State { id = 33, sigla = "RJ", name = "Rio de Janeiro" });
        states.Add(new State { id = 34, sigla = "SP", name = "Sao Paulo" });


        foreach (State state in states)
        {
            CboState.Items.Add(new ListItem(state.name, state.id.ToString()));
        }

    }

Only run this code on initial page load to avoid duplicating the items during a post back.

Upvotes: 1

tontonsevilla
tontonsevilla

Reputation: 2799

Use System.Web.UI.WebControls.ListItem when giving list for dropdown and you don't want to set which field is for text and value.

    private List<ListItem> getItemList()
    {
        List<ListItem> itemList = new List<ListItem>();

        itemList.Add(new ListItem { 
            Text = "Item1",
            Value = "1"
        });

        itemList.Add(new ListItem
        {
            Text = "Item2",
            Value = "2"
        });

        itemList.Add(new ListItem
        {
            Text = "Item3",
            Value = "3"
        });

        return itemList;
    }

DropDownList1.DataSource = getItemList();
DropDownList1.DataBind();

Upvotes: 0

Related Questions