无法将标头模板与L2S IQueryable列表绑定

时间:2012-02-10 09:56:12

标签: c# asp.net .net linq linq-to-sql

我的方法是返回IQuerable,如下所示:

internal IQueryable<TradeLeads> GetLeadsByCategory(int categoryId)
        {
            return
                _context.BuySells.Where(bs => bs.CategoryId == categoryId).OrderBy(bs => bs.CreationDate).Select(
                    bs => new TradeLeads
                              {
                                  Id = bs.Id,
                                  BuySellTypeId = Convert.ToInt32(bs.BuySellTypeId.ToString()) ,
                                  Description = bs.Description,
                                  Flag = bs.Company.Country1.Flag,
                                  MembershipType = bs.Company.MembershipType,
                                  IsUsingSmsNotifications = bs.Company.IsUsingSMSNotifications,
                                  IsVerified = bs.Company.IsVerified,
                                  ProductImagePath = bs.ProductImagePath,
                                  ProductName = bs.ProductName,
                                  CompanyName = bs.Company.CompanyName,
                                  CompanyId = Convert.ToInt32(bs.CompanyId.ToString()),
                                  PostedDate = bs.CreationDate
                              });
        }

所有字段都有值。我在转发器控件的标头模板中绑定BuySellTypeId。下面给出了ASPX,它位于Usercontrol中。

<HeaderTemplate>
        <div class="grdheader">
            <div class="hdrText">
                <h3 id="h3TypeName">
                    </h3> <asp:HiddenField runat="server" ID="HidTypeId" Value='<%#Eval("BuySellTypeId") %>'/>
            </div>
            <div class="hdrMore">
                <a href='<%#string.Format("ViewAll.aspx?t={0}",Eval("BuySellTypeId"))%>'>
                    <img src="cdn/images/more.png" />
                    View More </a>
            </div>
        </div>
    </HeaderTemplate>

我从它的父页面绑定中继器就像这样。首先,我将转发器的保护级别从protected更改为public,以便我可以从任何位置访问它,而无需从父页面进行转换或查找。

  private void BindAllBuyAndSellLeads(int categoryId)
        {
            var repo = new LeadsRepository();
            var list = repo.GetLeadsByCategory(categoryId);
            BindGrid(1, Leads1.LeadsGrid, list);
            BindGrid(2, Leads2.LeadsGrid, list);
        }
        private static void BindGrid(int leadTypeId, Repeater gv, IQueryable<Core.Helper.TradeLeads> list)
        {
            var query = (from p in list
                         where p.BuySellTypeId == leadTypeId
                         select p).ToList();
            Common.BindGrid(query, gv);
        }

这里Leads1和Leads2是用户控件Leads.ascx。这是相同的usercontrol放在页面上的两个位置。但是我在绑定时变空了。请帮助,我在哪里以及我做错了什么。

1 个答案:

答案 0 :(得分:0)

标题中的绑定永远不会起作用。绑定仅适用于ItemTemplate;你可以编程设置标题中的值,但实现转发器用于绑定多行数据;有多个项目,但只有一个标题。应该使用哪个 BuySellTypeId ?第一个?最后一个?转发器无法告知,因此您必须以可编程方式设置值。