C# 为DropDownList绑定生成全局方法

C# 为DropDownList绑定生成全局方法,c#,asp.net,methods,C#,Asp.net,Methods,我从这样的数据库返回一个列表 ClsCampus campus = new ClsCampus(); List<ClsCampus> camp = campus.GetCampusAll(); Utility.BindComboBox(ComboBoxCampus, camp, "CampusName", "CampusId"); 但是,它给了我一个编译错误。 那么,如何编写一个通用方法,将通用记录列表绑定到组合框 The best overloaded method match

我从这样的数据库返回一个列表

ClsCampus campus = new ClsCampus();
List<ClsCampus> camp = campus.GetCampusAll();
Utility.BindComboBox(ComboBoxCampus, camp, "CampusName", "CampusId");
但是,它给了我一个编译错误。 那么,如何编写一个通用方法,将通用记录列表绑定到组合框

The best overloaded method match for 'KenMISSchool.Repository.Utility.BindComboBox(System.Web.UI.WebControls.DropDownList, System.Collections.Generic.List<object>, string, string)' has some invalid arguments D:\Projects\KenMISSchool\Web\forms\student\registration.aspx.cs 20  9   Web
错误1

The best overloaded method match for 'KenMISSchool.Repository.Utility.BindComboBox(System.Web.UI.WebControls.DropDownList, System.Collections.Generic.List<object>, string, string)' has some invalid arguments D:\Projects\KenMISSchool\Web\forms\student\registration.aspx.cs 20  9   Web
与“KenMISSchool.Repository.Utility.BindComboBox(System.Web.UI.WebControls.DropDownList,System.Collections.Generic.List,string,string)”匹配的最佳重载方法具有一些无效参数D:\Projects\KenMISSchool\Web\forms\student\registration.aspx.cs 20 9 Web

您正在传递
列表
,但方法需要
列表
,因此我将其更改为通用方式:

public static void BindComboBox<T>(DropDownList listName, List<T> list, string textField, string valueField)
{
    listName.DataTextField = textField;
    listName.DataValueField = valueField;
    listName.DataSource = list;
    listName.DataBind();
}
publicstaticvoidbindcomboBox(dropdownlistlistname、List List、stringtextfield、stringvaluefield)
{
listName.DataTextField=textField;
listName.DataValueField=valueField;
listName.DataSource=list;
listName.DataBind();
}
这样称呼它:

Utility.BindComboBox<ClsCampus>(ComboBoxCampus, camp, "CampusName", "CampusId");
Utility.BindComboBox(ComboBoxCampus、camp、“CampusName”、“CampusId”);

我们有一个类似的想法,但用另一种方法解决了它。我不确定它是否适合你,但这里有一个建议:

我们实现了一个名为IDropdownable(我知道这不是最好的名称)的接口,它强制执行两个属性:

public string dropdown_value;
public string dropdown_text;
然后我们在所有需要轻松绑定到DropDownList的类上实现了这个接口:

public class Foo : IDropdownable
{
    //Let's say these fields are already in the class:
    private int _id;
    private string _name;
    private DateTime _date;

    //The interface was then implemented:
    public string dropdown_value
    {
        get
        {
            return this._id.ToString();
        }
    }

    public string dropdown_text
    {
        get
        {
            return String.Format("{0} ({1})", this._name, this._date.Year);
            //Or simpler: return this._name;
        }
    }

}
剩下的唯一一件事就是创建一个通用函数,该函数接受
IDropdownable
参数,并以您喜欢的方式输出下拉列表(HTML
string
SelectList
List
,…其中有许多选项)

生成的公共列表DL(
清单项目,
所选字符串(U值)
{ ... }
如果你愿意,我可以给你一个函数的近似值

评论


我们在MVC2 ASP.NET4中实现了这一点。老实说,我不确定在经典ASP.NET中应该返回什么类型,但我假设某种类型的列表/数组可以工作。

我想您的ComboBoxCampus是DropDownlist类型的?尝试将其作为(ComboBoxCampus作为DropDownList)传递以获取此错误,错误1参数2:无法从“System.Collections.Generic.List”转换为“System.Collections.Generic.List”D:\Projects\KenMISSchool\Web\forms\student\registration.aspx.cs 20 46 WebError 2找不到类型或命名空间名称“T”(是否缺少using指令或程序集引用?)D:\Projects\KenMISSchool\Repository\Utility.cs 30 69 Repository是否确实按以下方式声明了方法:
public static void BindComboBox(DropDownList listName、List List List、string textField、string valueField)
?在方法名称后面加上
很重要,而且您是否至少使用了C#2.0?
public List<SelectListItem> GenerateDDL(
                    List<IDropdownable> items, 
                    string selected_value) 
{ ... }