views:

68

answers:

6

I want to load a DropDownList with possible Paises from my database.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Backend;
using Backend.Models;

namespace Frontend_UI_Web.Administrativos
{
    public partial class Ciudad : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PaisRepository paisRepo = new PaisRepository();
            LoadPaises(paisRepo);

            CiudadRepository ciudadRepo = new CiudadRepository();            
        }

        private void LoadPaises(PaisRepository paisRepo)
        {
            //FindAll() returns a collection IQueryable<Pais>!
            var Paises = paisRepo.FindAll().AsEnumerable();
            uiddlPais.DataSource = Paises;
            uiddlPais.DataBind();
        }

        protected void uibtnSubmit_Click(object sender, EventArgs e)
        {

        }
    }
}

Any guidance? If I run this code the dropdownlist loads 5 items, which is correct since I have 5 paises. But the names aren't displayed correctly.

A: 

Set DataTextField and DataValueField?

Jay
A: 

Sounds like you are forgetting to set the DataValueField and DataTextField properties of your drop-down list. Then you can have your sweet little paisies!

Dan Diplo
+1  A: 

You'll want to set the DataTextField and DataValueField properties of the DropDownList so that it knows which properties of each Pais object it should bind to.

Supposing that Pais looks like

class Pais
{
    public int Id { get; set; }
    public string Name { get; set; }
    ...
}

then you'd want DataTextField="Name" DataValueField="Id".

stevemegson
A: 

You have to specify which members of your Paise class you want to display as Text and Value fields.

Something like this would work:

uiddlPais.DataSource = Paises;
uiddlPais.DataTextField = "Name";
uiddlPais.DataValueField = "Id";
uiddlPais.DataBind();
Bertrand Marron
A: 

u will need to set DataTextField and DataValueField property of dropdownlist

DataTextField is shown to user and DataValueField does not shown. It works in code.

developer8
A: 

Make sure you defined which properties you want your DropDownList to bind to using the DataTextField and DataValueField properties.

<asp:DropDownList ID="uiddlPais" runat="server"
    DataTextField="TheFieldWithTheTextYouWantToDisplay"
    DataValueField="TheFieldWithTheValueYouWantAttached" />

Also I see a problem in your Page_Load. You should be wrapping the initalizing of you DropDownList in a if (!IsPostBack) check or else you will be rebinding it with every postback and then your uibtnSubmit will not be able to read the selected value since you would have reloaded the DropDownList before the click even was reached. So it should look like:

protected void Page_Load(object sender, EventArgs e)   
{
    if (!IsPostBack)
    {   
        PaisRepository paisRepo = new PaisRepository();   
        LoadPaises(paisRepo);
    }
}
Kelsey