非常简单的问题......但结果很奇怪。 我只想填写c#
中的下拉列表protected void Page_Load(object sender, EventArgs e)
{
if (!(Page.IsPostBack))
{
LoadDropDowns();
}
}
protected void LoadDropDowns()
{
ddlVendor.DataSource = BL.GetAllVendors();
ddlVendor.DataTextField = "VendorName";
ddlVendor.DataValueField = "VendorName";
ddlVendor.DataBind();
}
BL.GetAllVendors只是一个静态类,它可以做到这一点:
public static List<Vendor> GetAllVendors()
{
return DL.GetAllVendors();
}
DL.GetAllVendors也是静态类(数据层),它构建了List:
public static List<Vendor> GetAllVendors()
{
using(SqlConnection con = new SqlConnection(connString))
{
//use sproc
SqlCommand cmd = new SqlCommand("selAllVendors", con);
cmd.CommandType = CommandType.StoredProcedure;
//temporary storage of list of vendors
List<Vendor> lv = new List<Vendor>();
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
//continue as long as we have vendors
while (reader.Read())
{
//instantiate a vendor
Vendor v = new Vendor((string)reader["VendorName"]);
//add them to the list
lv.Add(v);
}
//clean the reader
reader.Close();
reader = null;
//return that list
return lv;
}
}
供应商类很简单:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Vendor
/// </summary>
namespace TK.Objects
{
public class Vendor
{
private string VendorName { get; set; }
public Vendor(string vn)
{
VendorName = vn;
}
}
}
我知道我的sproc是正确的,因为我做了调试/监视,我看到它取消了两个记录“All”和“Microsoft”。
简单来说:
CREATE PROCEDURE selAllVendors
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT
Vendor AS VendorName,
1 as SortOrder
FROM
Vendor
UNION ALL
SELECT
'All' AS VendorName,
0 as SortOrder
ORDER BY
SortOrder,
Vendor
END
GO
但是当我运行此代码时出现错误:
System.Web.HttpException was unhandled by user code
Message=DataBinding: 'TK.Objects.Vendor' does not contain a property with the name 'VendorName'.
Source=System.Web
ErrorCode=-2147467259
WebEventCode=0
StackTrace:
at System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName)
at System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName, String format)
at System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource)
at System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e)
at System.Web.UI.WebControls.ListControl.PerformSelect()
at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
at _Default.LoadDropDowns() in c:\Users\Documents\Visual Studio 2010\WebSites\T\Default.aspx.cs:line 26
at _Default.Page_Load(Object sender, EventArgs e) in c:\Users\Documents\Visual Studio 2010\WebSites\T\Default.aspx.cs:line 17
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
我在这里缺少什么?
答案 0 :(得分:5)
VendorName
课程中的Vendor
属性必须是公开的,而不是私有的。
答案 1 :(得分:0)
更改供应商类属性
private string VendorName { get; set; }
到
public string VendorName { get; set; }
你应该都很好。
答案 2 :(得分:0)
您可以将此代码用于类供应商。
public class Vendor
{
private string vendorName = string.Empty;
public string VendorName
{
get { return vendorName; }
set { vendorName = value; }
}
public Vendor(string vn)
{
VendorName = vn;
}
}