2

我正在通过 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>
4

2 回答 2

5

你只需要更正你的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>
于 2013-07-01T12:53:33.873 回答
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;
    }
}
于 2013-07-01T12:54:25.620 回答