C# 在下拉菜单中填充数据
我对c#还不熟悉,所以有点拘泥于我认为非常简单的模块。我只需要在下拉菜单中显示数据,但绑定时出现一些错误。。。或者我会在装订之前说。这就是我要做的。如果我犯了一个非常简单的错误,我真的很抱歉,但我已经尽了最大的努力&现在我想我需要一些指导 CustomService.csC# 在下拉菜单中填充数据,c#,asp.net,data-binding,drop-down-menu,C#,Asp.net,Data Binding,Drop Down Menu,我对c#还不熟悉,所以有点拘泥于我认为非常简单的模块。我只需要在下拉菜单中显示数据,但绑定时出现一些错误。。。或者我会在装订之前说。这就是我要做的。如果我犯了一个非常简单的错误,我真的很抱歉,但我已经尽了最大的努力&现在我想我需要一些指导 CustomService.cs public partial class CustomService { public List<Code> GetDepartment(bool activeOnly) { List<
public partial class CustomService
{
public List<Code> GetDepartment(bool activeOnly)
{
List<Code> retVal = new List<Code>();
---some code----
return retVal;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<Code> dept = new List<Code>CustomService.GetDepartment(true);
ddlDepartment.DataSource = dept;
ddlDepartment.DataBind();
}
}
//error an object reference is required for an nonstatic field, method or Property CustomService.GetDepartment(true);
ProgramList.ascx.cs
public partial class CustomService
{
public List<Code> GetDepartment(bool activeOnly)
{
List<Code> retVal = new List<Code>();
---some code----
return retVal;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<Code> dept = new List<Code>CustomService.GetDepartment(true);
ddlDepartment.DataSource = dept;
ddlDepartment.DataBind();
}
}
//error an object reference is required for an nonstatic field, method or Property CustomService.GetDepartment(true);
要能够调用GetDepartment方法,需要创建CustomService的新实例:
CustomService service = new CustomService();
service.GetDepartment(true);
或使该方法成为静态的:
public static List<Code> GetDepartment(bool activeOnly) { }
但是,如果将其置为静态,则驻留在类中的该方法使用的每个变量也需要是静态的。您忘记了先创建对象,然后才可以调用该方法 另一件事是你只需要直接赋值,就像我在下面做的那样,没有必要创建任何新的列表 请检查下面适用于您的代码
CustomService custsrv = new CustomService();
List<Code> dept = custsrv.GetDepartment(true);
我想这会有帮助的
CustomService custS = new CustomService();
ddlDepartment.DataSource = custS.GetDepartment(true);
ddlDepartment.DataBind();
GetDepartment不是静态的方法啊!对不起,我没注意到它在另一个班级。让我感到不快的是,Page_LoadCustomService缺少类定义。CustomService在不同的项目中是一个单独的助手类。我已经使用Prod.Integration.DataModel为该项目添加了dll;在页面顶部…我还需要为它创建一个实例吗?好的,现在我没有错误。它编译得很好,但我没有在下拉列表中获得数据列表??对不起,还有一个问题