本文介绍了从asp.net c中声明为IEnumerable的方法填充下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 您好如何从声明为IEnumerable的方法填充下拉列表? 收到错误:无法将类型'void'隐式转换为'string' 在这一行上ddlUserType.DataValueField = ddlUserType.DataValueField = _UserTypeInfoList.Insert(0,new UserTypeInfo {UserType =});来自代码隐藏文件 这是我在一个名为UserTypeInfo的类中使用的代码: public IEnumerable< UserTypeInfo> ; GetUserType() { using(SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.GetConnection(),[GetUserType])) { if(reader.HasRows) { while(reader.Read()) { yield return new UserTypeInfo { UserTypeId = Convert.ToInt32(reader [UserTypeId] ), UserType = Convert.ToString(reader [UserType]) }; } } } 这就是我的代码隐藏使用asp.x文件的PageLoad: if(!Page.IsPostBack) { UserTypeInfo _UserTypeInfo = new UserTypeInfo(); 列表< UserTypeInfo > _UserTypeInfoList = _UserTypeInfo.GetUserType()。ToList < UserTypeInfo > (); ddlUserType.DataValueField = _UserTypeInfoList.Insert(0,new UserTypeInfo {UserType =}); } 和Html代码: < asp:Label ID = lblUserType runat = server class = label > 用户类型:< / asp:标签 > < asp:DropDownList 我D = ddlUserType runat = server > < / asp:DropDownList > 需要帮助!! 解决方案 错误的原因是'插入'方法返回' void '为这里: http://msdn.microsoft.com/en-us/ library / sey5k5z4%28v = vs.110%29.aspx [ ^ ] set ddlUserType.DataValueField as ddlUserType.D ataValueField = 用户类型; Hi how to populate an dropdownlist from an method declared as an IEnumerable?Am getting an error: "Cannot implicitly convert type 'void' to 'string'"on this line ddlUserType.DataValueField = ddlUserType.DataValueField = _UserTypeInfoList.Insert(0, new UserTypeInfo { UserType ="" }); from code behind file This is the code am using from a class called UserTypeInfo:public IEnumerable<UserTypeInfo> GetUserType() { using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.GetConnection(), "[GetUserType]")) { if (reader.HasRows) { while (reader.Read()) { yield return new UserTypeInfo { UserTypeId = Convert.ToInt32(reader["UserTypeId"]), UserType = Convert.ToString(reader["UserType"]) }; } } }And this the codebehind that am using on PageLoad of a asp.x file:if (!Page.IsPostBack) { UserTypeInfo _UserTypeInfo = new UserTypeInfo(); List<UserTypeInfo> _UserTypeInfoList = _UserTypeInfo.GetUserType().ToList<UserTypeInfo>(); ddlUserType.DataValueField = _UserTypeInfoList.Insert(0, new UserTypeInfo { UserType ="" }); }And Html code:<asp:Label ID="lblUserType" runat="server" class="label">User Type :</asp:Label><asp:DropDownList ID="ddlUserType" runat="server"></asp:DropDownList>Need help!! 解决方案 The reason for the error is that 'Insert' method returns 'void' as here: http://msdn.microsoft.com/en-us/library/sey5k5z4%28v=vs.110%29.aspx[^]set ddlUserType.DataValueField as ddlUserType.DataValueField="UserType"; 这篇关于从asp.net c中声明为IEnumerable的方法填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-16 11:38