asp:repeater可以用空列表作为数据源吗?

本文关键字:数据源 列表 repeater asp | 更新日期: 2023-09-27 18:10:24

我有这样的标记:

    <asp:Repeater ID="appsRepeater" runat="server">
        <ItemTemplate>
            <li>
                <div class="clearfix">
                    <a href="---some code I emitted--">
                        <img width="20" height="20" src="<%#DataItem.ThumbnailUrl%>" alt="" />
                        <span title="<%#Server.HtmlEncode(DataItem.Name)%>"><%#MarketplaceUIAdapter.EncodeTrimText(DataItem.Name,15)%></span>
                    </a>
                    <div class="app_links">
                        <a href="---some code I emitted--" class="text_link">
                            Edit</a>
                        <asp:LinkButton runat="server" CssClass="text_link" OnClick="DeleteApp" OnClientClick='<%# string.Format("return confirmDelete('"{0}'",'"{1}'")",DataItem.Name.Replace("'"","&qout;"), DataItem.AppType)%>' CommandArgument="<%#DataItem.Id.ToString()%>" analyticstrackingpath="/virtual/apps/myappslist/Delete">Delete</asp:LinkButton>
                    </div>
                </div>
            </li>
        </ItemTemplate>
    </asp:Repeater>

和后面的代码:

    private void BindData()
    {
        apps = Person.Apps.ToArray();
        Array.Sort(apps, (a2, a1) => a1.CreatedDate.CompareTo(a2.CreatedDate));
        if (apps.Count() > 0)
        {
            appsRepeater.DataSource = apps;
            appsRepeater.DataBind();
        }          
    }

我看到有时候Person是空的,所以我想做一个安全检查:

            if (Person!= null)
            {
                // same as above   
            }
        else
        {
            appsRepeater.DataSource = new List<IApp>();
            appsRepeater.DataBind();
        }

但是我的标记抛出渲染错误。数据源可以不分配,但不能为空列表?还是别的什么?

错误:"/"应用程序中的服务器错误。

对象引用未设置为对象的实例。

描述:当前web请求执行过程中出现未处理的异常。请查看堆栈跟踪以获得有关错误及其在代码中的起源位置的更多信息。

Exception Details: System。NullReferenceException:对象引用没有设置为对象的实例。

源错误:

Line 55:                        %>
Line 56:                     <Toolbar:Selector ID="Selector1" runat="server"></Toolbar:Selector>
Line 57:                        <%}
Line 58: %>
Line 59:                     
Source File: d:'MyName'RnD'Server'Services'ControlPanel'Dev-SSO-1.0-RewardsWithWibiya'ControlPanel'Masters'ControlPanelMaster.Master    Line: 57 

堆栈跟踪:

[NullReferenceException: Object reference not set to an instance of an object.]
   **ASP.browserapps_controls_myapps_ascx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) +52**
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +114
   ASP.browserapps_controls_createapp_ascx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in D:'MyName'RnD'Serve:172
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +114
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +239
   ASP.masters_controlpanelmaster_master.__RenderfrmMain(HtmlTextWriter __w, Control parameterContainer) in d:'MyName'RnD'Server'Services'ControlPanel'Dev-SSO-1.0-RewardsWithWibiya'ControlPanel'Masters'ControlPanelMaster.Master:57
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +114
   System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) +252
   System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) +86
   System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) +52
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +239
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +239
   System.Web.UI.Page.Render(HtmlTextWriter writer) +37
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4239

asp:repeater可以用空列表作为数据源吗?

问题是我发送了一个new List<IApp>()而不是新的IApp[0]