2013-07-01 51 views
2

我正在通過linq搜索姓名,我想將結果綁定到中繼器。如何將linq查詢綁定到中繼器?

我在做什麼錯?

// this the results for list 

public static List<SearchResults> lsSearchResults = new List<SearchResults>(); 

public class SearchResults 
     { 
      public string Name { get; set; } 
      public string Surname { get; set; } 

      public SearchResults(string name, string surname) 
      { 
       Name = name; 
       Surname = surname; 
      } 

     } 

// search button 
protected void btnSearch_Click(object sender, EventArgs e) 
     { 

      var found = (from User in myDB.Memberships 
         where User.Name.ToLower().Contains(txtSearch.Text.ToLower()) || 
         User.Surname.ToLower().Contains(txtSearch.Text.ToLower()) 
         select new { User.Name, User.Surname }); 

      // validates items in search query if Exist 
      if (!ChillZARdev.App_Code.Utilities.IsEmpty(found)) 
      { 
       foreach (var user in found) 
       { 
        lsSearchResults.Add(new SearchResults(user.Name, user.Surname)); 
        // ls.Add(user.Name + " " + user.Surname); 

        // Response.Write(user); 
       } 
       Repeater1.DataSource = lsSearchResults; 

       Repeater1.DataBind(); 

      } 
      } 

     private bool IsEmpty(object found) 
     { 
      throw new NotImplementedException(); 
     } 

     protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) 
     { 

      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
      { 

       WAT THE HELL I PUT IN HERE ?? LOL for this ish to work 
         CLUELESS 

      } 

這是中繼器的HTML代碼:

 <asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound"> 
     <HeaderTemplate> 
     <table> 
     <tr> 
     <td> 
     <asp:Label ID="Label6" runat="server" Text='<%#Eval("Name")%>'></asp:Label> 
     <asp:Label ID="Label5" runat="server" Text='<%#Eval("Surname")%>'></asp:Label> 
     </td> 
     <td> 
     </td> 
     </tr> 

     </table> 
     </HeaderTemplate> 


     </asp:Repeater> 
+0

有什麼問題?你有編譯器錯誤嗎?一個例外?有什麼問題,先! –

+0

什麼不起作用?!?!?如果你不告訴我們什麼不工作,我們應該如何幫助你? –

回答

3

你一定要糾正你Repeater聲明。在此之後就沒有必要處理ItemDataBound事件都:

<asp:Repeater ID="Repeater1" runat="server"> 
    <HeaderTemplate> 
     <table> 
    </HeaderTemplate> 
    <ItemTemplate> 
     <tr> 
      <td> 
       <asp:Label ID="Label6" runat="server" Text='<%#Eval("Name")%>'></asp:Label> 
       <asp:Label ID="Label5" runat="server" Text='<%#Eval("Surname")%>'></asp:Label> 
      </td> 
      <td> 
      </td> 
     </tr> 
    </ItemTemplate> 
    <FooterTemplate> 
     </table> 
    </FooterTemplate> 
</asp:Repeater> 
+0

問題是我必須用替換headerTemplate –

0

爲了讓您的類的對象,早在DataBound事件,你只需要投e.Item.DataItem到類:

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
    { 
     var result = (SearchResult)e.Item.DataItem; 
    } 
}