DataBinding:'System.Web.UI.WebControls.ListItem'不包含具有名称的属性

时间:2017-03-14 19:29:40

标签: c# asp.net

我正在尝试向下拉列表添加只读属性,但收到以下错误:“DataBinding:'System.Web.UI.WebControls.ListItem'不包含具有名称的属性。”

以下是下拉代码:

<asp:DropDownList ID="ddlgvAcademicYear" runat="server" DataSourceID="odsYearsRow"
    Width="150px" ValidationGroup="Add New" DataTextField="AcademicYearDisplay" DataValueField="AcademicYear"
    SelectedValue='<%# Bind("AcademicYear") %>'>
</asp:DropDownList>

<asp:ObjectDataSource ID="odsYearsRow" runat="server" SelectMethod="GetCurrentYearsWithDefault"
    TypeName="BLLYears" OldValuesParameterFormatString="original_{0}">
    <SelectParameters>
        <asp:Parameter DefaultValue="30" Name="yearsBack" Type="Int32" />
        <asp:Parameter DefaultValue="3" Name="yearsForward" Type="Int32" />
        <asp:Parameter DefaultValue="0" Name="defaultYear" Type="Int32" />
        <asp:ControlParameter ControlID="hfAcademicYear" PropertyName="Value" Type="Int32" Name="includeYear" />
    </SelectParameters>
</asp:ObjectDataSource>

以下是下拉代码的来源:

public List<ListItem> GetCurrentYearsWithDefault(int yearsForward, int defaultYear)
{
    int currentYear = new BLLSchoolInfo().GetCurrentAcademicYear(Campus.AppCode.BusinessLayer.Scheduling.BLLScheduling.SchoolId);
    int yearsBack = 0;
    if (yearsForward <= 0 || yearsForward > 50) yearsForward = 5;

    List<ListItem> ret = new List<ListItem>();
    if (currentYear > 20002001)
        for (int i = currentYear + (10001 * yearsForward); i >= currentYear - (10001 * yearsBack); i -= 10001)
            if (i != currentYear)
                ret.Add(new ListItem(i.ToString().Insert(4, "/"), i.ToString()));

    if (defaultYear > 0 && ret.Find(x => x.Value == defaultYear.ToString()) == null)
        ret.Insert(0, new ListItem(defaultYear.ToString().Insert(4, "/"), defaultYear.ToString()));
    else
        ret.Insert(0, new ListItem(currentYear.ToString().Insert(4, "/"), currentYear.ToString()));

    return ret;
}

这是只读属性代码:

public int AcademicYear { get; set; }
public string AcademicYearDisplay
{
    get { return string.Format("{0}/{1}", AcademicYear / 10000, AcademicYear % 10000); }
}

当'DataTextField =“Text”DataValueField =“Value”'时,代码运行正常,但是当我将它们更改为“AcademicYearDisplay”和“AcademicYear”时,我收到错误。

以下是完整错误:

DataBinding: 'System.Web.UI.WebControls.ListItem' does not contain a property with the name 'AcademicYearDisplay'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: DataBinding: 'System.Web.UI.WebControls.ListItem' does not contain a property with the name 'AcademicYearDisplay'.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[HttpException (0x80004005): DataBinding: 'System.Web.UI.WebControls.ListItem' does not contain a property with the name 'AcademicYearDisplay'.]
   System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName) +2843958
   System.Web.UI.DataBinder.GetPropertyValue(Object container, String propName, String format) +11
   System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource) +347
   System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +290
   System.Web.UI.WebControls.ListControl.PerformSelect() +37
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
   System.Web.UI.Control.DataBindChildren() +185
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +160
   System.Web.UI.Control.DataBind() +17
   System.Web.UI.Control.DataBindChildren() +185
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +160
   System.Web.UI.Control.DataBind() +17
   System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +167
   System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +3721
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +67
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +14
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +128
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +34
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
   System.Web.UI.WebControls.GridView.DataBind() +9
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +114
   System.Web.UI.WebControls.GridView.OnPreRender(EventArgs e) +26
   System.Web.UI.Control.PreRenderRecursiveInternal() +88
   System.Web.UI.Control.PreRenderRecursiveInternal() +160
   System.Web.UI.Control.PreRenderRecursiveInternal() +160
   System.Web.UI.Control.PreRenderRecursiveInternal() +160
   System.Web.UI.Control.PreRenderRecursiveInternal() +160
   System.Web.UI.Control.PreRenderRecursiveInternal() +160
   System.Web.UI.Control.PreRenderRecursiveInternal() +160
   System.Web.UI.Control.PreRenderRecursiveInternal() +160
   System.Web.UI.Control.PreRenderRecursiveInternal() +160
   System.Web.UI.Control.PreRenderRecursiveInternal() +160
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +883

我在当天早些时候发布了此消息,但我将其重新发布以提高可见度。如果我需要提供更多信息,请告诉我。

提前谢谢。

1 个答案:

答案 0 :(得分:1)

您绑定了ListItemsddlgvAcademicYear的列表。 因为这些已经是DropDownList所需的项目类型,所以您无需指定DataTextFieldDataValueField。这些值已经存在于ListItem本身中。并且因为您绑定了ListItem类型的列表,所以它不包含属性AcademicYearDisplayAcademicYear

像这样使用它。

<asp:DropDownList ID="ddlgvAcademicYear" runat="server" DataSourceID="odsYearsRow"
Width="150px" ValidationGroup="Add New" SelectedValue='<%# Bind("AcademicYear") %>'>

当您使用数据库或类列表中的表时,需要绑定方式。这些转换为ListItems,但您需要指定哪个列/属性应成为ListItem的Text和Value属性。