2016-11-09 119 views
1

我試圖創建一個嵌套的Repeaters。Asp.net/C#:將客戶ID傳遞給函數中的Linq查詢

我有我的觀點的文章

http://www.aspsnippets.com/Articles/Implement-Nested-Repeater-Repeater-inside-Repeater-with-example-in-ASPNet-using-C-and-VBNet.aspx

我做到了在本指南正確地創建代碼,但有客戶ID傳遞給函數的一個問題。

我將通過代碼解釋它,我的.aspx代碼:

<asp:Repeater ID="rptCustomers" runat="server" OnItemDataBound="rptCustomers_ItemDataBound"> 
<HeaderTemplate> 
    <table class="table table-hover table-striped table-condensed table-bordered table-responsive" > 
     <tr> 
      <th> 

      </th> 
      <th> 
       Deposit Number 
      </th> 
      <th> 
       Custom Declaration 
      </th> 

       <th>Category</th> 
        <th>Location</th> 
        <th>Goods Description</th> 
        <th>Units Balance</th> 
        <th>WT Balance</th> 
        <th>Goods Balance Amount(LC)</th> 
     </tr> 
</HeaderTemplate> 
<ItemTemplate> 
    <tr> 
     <td> 
      <img alt="" style="cursor: pointer" src="images/plus.png" /> 
      <asp:Panel ID="pnlOrders" runat="server" Style="display: none"> 
       <asp:Repeater ID="rptOrders" runat="server"> 
        <HeaderTemplate> 
         <table class="table table-hover table-striped table-condensed table-bordered table-responsive" > 
          <tr> 
           <th> 
            Item No 
           </th> 
           <th> 
            Item descr 
           </th> 
           <th> 
            UOM 
           </th> 
            <th> 
            Balance Units 
           </th> 
            <th> 
            Balance LC 
          </tr> 
        </HeaderTemplate> 
        <ItemTemplate> 
         <tr> 
          <td> 
          <%# Eval("itemNo") %> 
          </td> 
          <td> 
          <%# Eval("itemDesc") %> 
          </td> 
          <td> 
          <%# Eval("Uom") %> 
          </td> 
          <td> 
          <%# Eval("balUnits") %> 
          </td> 
          <td> 
          <%# Eval("balLc") %> 
          </td> 

         </tr> 
        </ItemTemplate> 
        <FooterTemplate> 
         </table> 
        </FooterTemplate> 
       </asp:Repeater> 
      </asp:Panel> 
      <asp:HiddenField ID="hfCustomerId" runat="server" Value='<%# Eval("depID") %>' /> 
     </td> 
     <td> 
      <%# Eval("depNo") %> 
     </td> 
     <td> 
      <%# Eval("customDec") %> 
     </td> 

      <td><%#Eval("category") %></td> 
        <td><%#Eval("location") %></td> 
        <td><%#Eval("goodDesc") %></td> 
        <td><%#Eval("unitsBal") %></td> 
        <td><%#Eval("wtBal") %></td> 
        <td><%#Eval("lcBal") %></td> 


    </tr> 
</ItemTemplate> 
<FooterTemplate> 
    </table> 
</FooterTemplate> 

現在這是隱藏字段,將開展客戶ID:

<asp:HiddenField ID="hfCustomerId" runat="server" Value='<%# Eval("depID") %>' /> 

這是page_load中父代轉發器的代碼:

protected void Page_Load(object sender, EventArgs e) 
{ 

    var query = (from cd in db.CDIndexes 
       join com in db.Companies on cd.cdin_CompanyId equals com.Comp_CompanyId 
       join ter in db.Territories on cd.cdin_Secterr equals ter.Terr_TerritoryID 

       where cd.cdin_Deleted == null && 
       com.Comp_Deleted == null && 
       cd.cdin_Status == "InProgress" && 
       com.Comp_CompanyId == 408 
       select new 
       { 
        depID=cd.cdin_CDIndexID, 
        location = ter.Terr_Caption, 
        depNo = cd.cdin_Serial, 
        customDec = cd.cdin_Customdeclar, 
        category = cd.cdin_category, 
        goodDesc = cd.cdin_goodsDesc, 
        unitsBal = cd.cdin_RemainPackages, 
        wtBal = cd.cdin_RemainWT, 
        lcBal = cd.cdin_ActMortgageAmnt, 
       } 

       ).ToList(); 


    rptCustomers.DataSource = query; 
    rptCustomers.DataBind(); 
} 

這是的ItemDataBound父中繼轉發

protected void rptCustomers_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    var queryItems = (from cd in db.CDIndexes 
         join com in db.Companies on cd.cdin_CompanyId equals com.Comp_CompanyId 
         join terr in db.Territories on cd.cdin_Secterr equals terr.Terr_TerritoryID 
         join gds in db.Goods on cd.cdin_CDIndexID equals gds.good_CDIndexId 
         join itms in db.Items on gds.good_ItemsID equals itms.item_ItemsID 
         join capt in db.Custom_Captions on gds.good_UOM equals capt.Capt_Code 

         where 

         capt.Capt_Family== "good_UOM" && 
         cd.cdin_Deleted == null && 
         cd.cdin_Status== "InProgress" && 
         cd.cdin_CompanyId==408 && 
         cd.cdin_CDIndexID== 2506542 && 
         itms.item_Deleted == null && 
         cd.cdin_GoodsProperty=="01" && 
         com.Comp_Deleted== null 

         select new 
         { 
          itemNo=itms.item_itemNo, 
          itemDesc=itms.item_Name, 
          Uom=capt.Capt_US, 
          balUnits=gds.good_RemainWT, 
          balLc=gds.good_BalanceAmtLC, 
         } 
        ).ToList(); 

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
    { 
     string customerId = (e.Item.FindControl("hfCustomerId") as HiddenField).Value; 
     Repeater rptOrders = e.Item.FindControl("rptOrders") as Repeater; 
     rptOrders.DataSource = queryItems; 
     rptOrders.DataBind(); 
    } 
} 

我要的是如何將客戶的ID值傳遞到LINQ查詢,特別是通的,而不是在數(2506542)的ID下面的語句:

cd.cdin_CDIndexID== 2506542 

,所以我需要更換中繼器的數據源下面的代碼:

rptOrders.DataSource = queryItems; 
      rptOrders.DataBind(); 

與參數字符串客戶ID值 函數調用作爲一個例子:

rptOrders.DataSource = function(customerId); 
       rptOrders.DataBind(); 

那是什麼將返回一個LINQ查詢函數的語法。

回答

0

創建其返回基於給定id項目的方法,創建類代表一個數據項你想回到

public class DataItem 
{ 
    public string ItemNo { get; set; } 
    public string ItemDesc { get; set; } 
    public string Uom { get; set; } 
    public string BalUnits { get; set; } 
    public string BalLc { get; set; } 
} 

private IEnumerable<DataItem> GetItemsById(int customerId) 
{ 
    var queryItems = 
     from cd in db.CDIndexes 
      join com in db.Companies on cd.cdin_CompanyId equals com.Comp_CompanyId 
      join terr in db.Territories on cd.cdin_Secterr equals terr.Terr_TerritoryID 
      join gds in db.Goods on cd.cdin_CDIndexID equals gds.good_CDIndexId 
      join itms in db.Items on gds.good_ItemsID equals itms.item_ItemsID 
      join capt in db.Custom_Captions on gds.good_UOM equals capt.Capt_Code 
     where 
      capt.Capt_Family== "good_UOM" && 
      cd.cdin_Deleted == null && 
      cd.cdin_Status== "InProgress" && 
      cd.cdin_CompanyId==408 && 
      cd.cdin_CDIndexID== customerId && // Just use parameter instead of number 
      itms.item_Deleted == null && 
      cd.cdin_GoodsProperty=="01" && 
      com.Comp_Deleted== null 
     select new DataItem 
       { 
        ItemNo=itms.item_itemNo, 
        ItemDesc=itms.item_Name, 
        Uom=capt.Capt_US, 
        BalUnits=gds.good_RemainWT, 
        BalLc=gds.good_BalanceAmtLC, 
       };   
} 

然後在您的RowDataBound方法,使用這種方法

protected void rptCustomers_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListItemType.Item || 
     e.Item.ItemType == ListItemType.AlternatingItem) 
    { 
     string fieldValue= (e.Item.FindControl("hfCustomerId") as HiddenField).Value; 
     int customerId = int.Parse(fieldValue); 

     Repeater rptOrders = e.Item.FindControl("rptOrders") as Repeater; 
     rptOrders.DataSource = GetItemsById(customerId).ToList(); 
     rptOrders.DataBind(); 
    } 
} 
+0

非常感謝你這解決了這個問題 –