views:

70

answers:

4

For example, I want a user to create a new fanclub for a given Team.

Obviously the teams have to be loaded into a DropDownList so they are chosen from a pre-set choice group. The list of teams are returned from a database.

As of now I'm doing things like this:

//FindAll() returns IQueryable<Team>
var Teams = teamsRepo.FindAll().AsEnumarable();
myDropDownList.DataTextField = "Name";
myDropDownList.DataValueField = "ID";
myDropDownList.DataSource = Teams;
myDropDownList.DataBind();

Unfortunately when doing things this way I have no strongly typed attributes, so there is a risk of misspelling the ValueField or TextField.

There is also a problem when trying to get the selected value of the DropDownList. Using the following code, everything is saved with ID 1 regardless of what team was chosen.

fansite.IDTeam = myDropDownList.SelectedIndex;

Any suggestions on how to improve?

+1  A: 

(This was written before the ASP.Net clarification, I have answered from a winforms persepective).

I'm not sure what your Teams IDs are like, but if they are not sequential, zero based and with no gaps, using myDropDownList.SelectedIndex; will give you the wrong piece of data.

I think you last line should read:

fansite.IDTeam = myDropDownList.SelectedValue;

On the data type to use, I usually use a Dictionary<int, string> for dropdown lists, and have a few helper extension methods to make population a little nicer.

Pondidum
+2  A: 

This is exactly how .NET databinding works, you have to type in the property names.

You could always add a converter to a combobox item and then call add (ms-doc)

As far as getting the value of the selected item try this:

fansite.IDTeam = myDropDownList.SelectedValue;
Hogan
A: 

You should consider using DataSources like ObjectDataSource or LinqDataSource to DataBind your controls.

It will save you time and is less error prone because you will configure your settings through a wizard instead of defining them in your code-behind.

Jeroen
+2  A: 

From your question it seems you're concerned about the hard coded strings for the databinding in your code. The same thing used to drive me nuts too.

There is a good way of using lambda methods and expression trees that will allow you to get rid of the hard coded strings and get the property names in a strongly typed way:

You can use this class and extension method...

public class Nameof<T>
{
    public static string Property<TProp>(Expression<Func<T, TProp>> expression)
    {
        var body = expression.Body as MemberExpression;
        if(body == null)
            throw new ArgumentException("'expression' should be a member expression");
        return body.Member.Name;
    }
}

...and this will allow you to do the following:

//instead of myDropDownList.DataTextField = "Name";
myDropDownList.DataTextField = Nameof<Team>.Property(t => t.Name);
//instead of myDropDownList.DataValueField = "ID";
myDropDownList.DataValueField = Nameof<Team>.Property(t => t.ID);

That's what I like to see; not a hard coded string literal in sight! :-)

DoctaJonez
Reflection is slow, IMO this is a solution to a non-problem which just slows down the code and makes it more obtuse.
Hogan
Yes reflection is slow, but expression trees do not use reflection.
DoctaJonez
@Docta : Ah, point taken -- I see now it happens in the compiler; mea culpa.
Hogan