如何使用 Repeater 从对象列表中输出字符串

发布于 2024-11-27 13:14:52 字数 1131 浏览 2 评论 0原文

我不确定这是否是解决此问题的最佳方法,但我正在尝试使用中继器控件显示一些搜索结果。查询写在页面后面的代码中,我不知道如何将结果绑定到控件。到目前为止,我已经创建了 saleItem 对象的列表,saleItem 对象包含我想要在转发器中显示的字符串。

Search.aspx.cs

List<SaleItem> resultsList = new List<SaleItem>();

  SqlDataReader reader = doMainQuery.ExecuteReader();

  while (reader.Read())
  {
    SaleItem newItem = new SaleItem((string)reader["saleTitle"]);
    resultsList.Add(newItem);
  }
  showResults.DataSource = resultsList;
  showResults.DataBind();

SaleItem.cs

public class SaleItem
{
    private String connectionString;
    public string saleTitle;

    public SaleItem(string s)
    {
        saleTitle = s;
    }

    public string  getTitle()
    {
        return saleTitle;
    }
}

Search.aspx

id希望能够以与此类似的方式显示标题,任何想法?

<asp:repeater
        id="showResults"  

        Runat="server"   >

        <ItemTemplate>
        <%# Eval("saleTitle")%></ItemTemplate> // resultsList.SaleItem.getTitle()?
        </asp:repeater>

I'm not sure if this is the best way to go about this but I'm trying to show some search results using a repeater control. The query is written in the code behind page and i cant figure out how to bind the results to the control. So far I've created a list of saleItem objects, the saleItem object contains the string i want to show in the repeater.

Search.aspx.cs

List<SaleItem> resultsList = new List<SaleItem>();

  SqlDataReader reader = doMainQuery.ExecuteReader();

  while (reader.Read())
  {
    SaleItem newItem = new SaleItem((string)reader["saleTitle"]);
    resultsList.Add(newItem);
  }
  showResults.DataSource = resultsList;
  showResults.DataBind();

SaleItem.cs

public class SaleItem
{
    private String connectionString;
    public string saleTitle;

    public SaleItem(string s)
    {
        saleTitle = s;
    }

    public string  getTitle()
    {
        return saleTitle;
    }
}

Search.aspx

id like to be able to show the title in a similar way to this, any ideas?

<asp:repeater
        id="showResults"  

        Runat="server"   >

        <ItemTemplate>
        <%# Eval("saleTitle")%></ItemTemplate> // resultsList.SaleItem.getTitle()?
        </asp:repeater>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骄兵必败 2024-12-04 13:14:52

您可以按如下方式重构您的代码

public class SaleItem
{    
    public string saleTitle {get;set;}   
}

public static class Extension
{
    public static IEnumerable<T> Select<T>(this SqlDataReader reader, Func<SqlDataReader, T> projection)
    {
        while (reader.Read())
        {
            yield return projection(reader);
        }
    }
}

List<SaleItem> resultsList = new List<SaleItem>();

SqlDataReader reader = doMainQuery.ExecuteReader();
var resultsList = reader.Select(x => new SaleItem { saleTitle = x["saleTitle"].ToString() }).Distinct().ToList();

showResults.DataSource = resultsList;
showResults.DataBind();


<asp:repeater id="showResults" Runat="server"   >
    <ItemTemplate>
      <%# Eval("saleTitle")%>
    </ItemTemplate>
</asp:repeater>

You can refactor your code as follow

public class SaleItem
{    
    public string saleTitle {get;set;}   
}

public static class Extension
{
    public static IEnumerable<T> Select<T>(this SqlDataReader reader, Func<SqlDataReader, T> projection)
    {
        while (reader.Read())
        {
            yield return projection(reader);
        }
    }
}

List<SaleItem> resultsList = new List<SaleItem>();

SqlDataReader reader = doMainQuery.ExecuteReader();
var resultsList = reader.Select(x => new SaleItem { saleTitle = x["saleTitle"].ToString() }).Distinct().ToList();

showResults.DataSource = resultsList;
showResults.DataBind();


<asp:repeater id="showResults" Runat="server"   >
    <ItemTemplate>
      <%# Eval("saleTitle")%>
    </ItemTemplate>
</asp:repeater>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文