C# 返回控件的ASP.net方法
我有一个返回DropDownList控件的方法。在调用页面中,我在CodeBehind中创建了一个新的DropDownList,将其设置为方法的返回值,然后设置为DataBind()。我总是得到一个空的下拉列表C# 返回控件的ASP.net方法,c#,asp.net,C#,Asp.net,我有一个返回DropDownList控件的方法。在调用页面中,我在CodeBehind中创建了一个新的DropDownList,将其设置为方法的返回值,然后设置为DataBind()。我总是得到一个空的下拉列表 public static DropDownList GetDropDownList() { DropDownList ddlInstance = new DropDownList(); ddlInstance.DataSource = [METHOD
public static DropDownList GetDropDownList()
{
DropDownList ddlInstance = new DropDownList();
ddlInstance.DataSource = [METHOD THAT RETURNS DATATABLE]
ddlInstance.DataTextField = ["COLUMN FROM TABLE";]
ddlInstance.DataValueField = ["ANOTHER COLUMN FROM TABLE";]
ddlInstance.DataBind();
return ddlInstance;
}
然后在页面的代码后面
DropDownListOnPage = GetDropDownList();
DropDownListOnPage.DataBind();
有人能在这里发现任何可能导致页面上的下拉列表不显示任何数据的问题吗?您真的不应该让该方法返回完全加载的下拉列表,而是让它只返回数据。然后:
DropDownListOnPage.DataSource=GetDataSource()。。。DropDownListOnPage.DataBind()代码>为什么不?你是说这是一种不好的做法还是不起作用?我需要重复使用这个控件,最好是得到完全加载的控件,而不是不断设置它的DataTextField和DataValueField。