开发者

What is the most comfortable datatype I can use to populate a DropDownList with?

开发者 https://www.devze.com 2023-01-12 07:49 出处:网络
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 re

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?


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! :-)


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;


(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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消