开发者

ASP.NET Server Controls - Data Source

开发者 https://www.devze.com 2022-12-28 12:58 出处:网络
I put a dropdownlist on a webform and give a list as datasource. But what can I do, if I want the first item in dropdownlist to be \"Select a user\" or something like that?

I put a dropdownlist on a webform and give a list as datasource. But what can I do, if I want the first item in dropdownlist to be "Select a user" or something like that?

For example: // ddlAllUsers is a DropDownList

        List<REGUSER> users = UserOperat开发者_JAVA技巧ions.GetAllUsers();
        ddlAllUsers.DataSource = users;
        ddlAllUsers.DataTextField = "NameLastName";
        ddlAllUsers.DataValueField = "ID";
        ddlAllUsers.DataBind();

Adding a new item in list whose associated property is "Select a user" is a solution, but I don't prefer.


You can actually add an item to your List with required properties.

For example,

List<REGUSER> users = UserOperations.GetAllUsers();
users.Insert( 0, new REGUSER() { NameLastName = "Select a user...", ID = -1 } );
0

精彩评论

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