如何使用 Repeater 从对象列表中输出字符串
我不确定这是否是解决此问题的最佳方法,但我正在尝试使用中继器控件显示一些搜索结果。查询写在页面后面的代码中,我不知道如何将结果绑定到控件。到目前为止,我已经创建了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以按如下方式重构您的代码
You can refactor your code as follow