【问题标题】:Displaying collection in Repeater control在Repeater控件中显示集合
【发布时间】:2012-01-06 16:51:30
【问题描述】:

我是 Asp.Net 的初学者,我试图在 reapet 控件上显示“gc”。

下面是代码:

 public partial class _Default : System.Web.UI.Page
{
      List<GlassesCollection> gc= BL.Example.GetCategory() ;

    protected void Page_Load(object sender, EventArgs e)
    {
        rpt1.DataSource = gc;
        rpt1.DataBind();
    }

    protected void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {

    }

我正在使用以下 ASP 代码:

<asp:Repeater ID="rpt1" runat="server" onitemdatabound="rpt1_ItemDataBound">
    <ItemTemplate>
        <%# Eval("gc") %>
    </ItemTemplate>
</asp:Repeater>

但是在运行时我得到了这个异常: 异常详细信息:System.Web.HttpException:DataBinding:“ISeeOptic.DataType.GlassesCollection”不包含名为“gc”的属性。

为什么我得到这个 Excption 以及如何解决这个问题的想法?

提前谢谢你!

【问题讨论】:

  • GlassesCollection 暴露了哪些属性?这些是你可以Eval 反对的。

标签: asp.net exception repeater


【解决方案1】:

尝试评估特定属性而不是对象

<asp:Repeater ID="rpt1" runat="server" onitemdatabound="rpt1_ItemDataBound">
   <ItemTemplate>         
      <%# Eval("gcProperty") %>     
   </ItemTemplate> 
</asp:Repeater>

SO Question 提供了有关将转发器绑定到通用列表的更多详细信息。

【讨论】:

    【解决方案2】:

    您通常可以利用 DataBinder 类,它与此处的代码列表一样具有最大的功能和灵活性...

    <asp:Repeater ID="rpt1" runat="server" onitemdatabound="rpt1_ItemDataBound">
       <ItemTemplate>         
          <%# DataBinder.Eval("gcProperty") %>     
       </ItemTemplate> 
    </asp:Repeater>
    

    【讨论】:

    • 在绑定到转发器中的一行的上下文中 - Eval 和 DataBinder.Eval 之间是否有任何区别。这个链接说不是forums.asp.net/p/984156/1264782.aspx。除了范围之外还有什么区别 - 它会带来什么额外的东西。老实说,我很好奇
    猜你喜欢
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    相关资源
    最近更新 更多