【问题标题】:ListView Item binding does not workListView 项绑定不起作用
【发布时间】:2013-07-17 09:46:30
【问题描述】:

我是 asp.net(webforms) 的新手。我按照本教程进行操作 --> http://www.asp.net/web-forms/tutorials/aspnet-45/getting-started-with-aspnet-45-web-forms/ui_and_navigation

在 Site.Master 中,我按照教程中的说明添加了以下代码:

    <section style="text-align: center; background-color: #fff">
        <asp:ListView ID="categoryList"
            ItemType="VanchoWorks.Models.Category"
            runat="server"
            SelectionMethod="GetCategories" >
            <ItemTemplate>
                <b style="font-size: large; font-style: normal">
                    <a href="/ProductList.aspx?id=<%#: Item.CategoryID %>">
                        <%#: Item.CategoryName %>
                    </a>
                </b>
            </ItemTemplate>
            <ItemSeparatorTemplate> - </ItemSeparatorTemplate>
        </asp:ListView>
    </section>

在代码隐藏中(Site.Master.cs)

    public IQueryable<Category> GetCategories()
    {
        var db = new ProductContext();
        IQueryable<Category> query = db.Categories;
        return query;
    }

但是当我运行应用程序时没有显示 ListView 的迹象。我在 GetCategories() 第 1 行添加了断点,但它并没有停在那里,这让我觉得我没有很好地设置 SelectionMethod。这是为什么呢?

【问题讨论】:

    标签: asp.net listview binding webforms


    【解决方案1】:

    将 SelectionMethod="GetCategories" 更改为 SelectMethod="GetCategories"

    【讨论】:

      【解决方案2】:

      像这样绑定listview

      public  GetCategories()
          {
              var db = new ProductContext();
              IQueryable<Category> query = db.Categories;
      
              ListView1.DataSource=query;
              ListView1.DataBind();
          }
      

      在页面加载中调用此方法 Remove SelectionMethod="GetCategories" 从 html 其他虎钳它会显示错误。

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 2016-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-01
        • 2018-02-20
        • 2011-08-19
        • 2016-10-31
        • 1970-01-01
        相关资源
        最近更新 更多