从下拉列表中获取所选OBJECT的问题 [英] Problem with getting selected OBJECT from dropdownlist

查看:136
本文介绍了从下拉列表中获取所选OBJECT的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我通过以下方式将用户收集到下拉列表:

  ddlContractors.DataSource = service.GetAllUsers(); 
ddlContractors.DataTextField =Name;
ddlContractors.DataValueField =Id;
ddlContractors.DataBind();

它的工作。
但是当我尝试通过以下方式获取所选对象时:

  var user =(User)ddlContractors.SelectedItem; 

我得到:

 (用户)ddlContractors.SelectedItem无法将System.Web.UI.WebControls.ListItem类型转换为GWDSite.GWDService.User

如何从下拉列表中获取对象用户?如果需要,我可以更改我的列表控件的类型

解决方案

下拉列表中的值字段是Id不是用户对象。所以'SelectedItem'返回的是Id值,而不是对象。您可以使用该Id查找User对象(从会话或缓存或任何可以保留的位置)


I have a problem with getting selected objectfrom my list.

I bind collection of users to dropdownlist by:

 ddlContractors.DataSource = service.GetAllUsers();
                ddlContractors.DataTextField = "Name";
                ddlContractors.DataValueField = "Id";
                ddlContractors.DataBind();

It's working. But when I try getting selected object by:

var user = (User)ddlContractors.SelectedItem;

I get:

    (User)ddlContractors.SelectedItem   Cannot convert type 'System.Web.UI.WebControls.ListItem' to 'GWDSite.GWDService.User'

How can I get object user from dropdownlist ? I can change type of my list control if it is necessary

解决方案

The value field in the dropdown list is the field "Id" not the User object. so 'SelectedItem' is returning the "Id" value -- not the object. You can use that Id to lookup the User object (from session or cache or wherever you can keep it)

这篇关于从下拉列表中获取所选OBJECT的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆