C# Repeater SelectMethod-如何获取返回的项目数?
我正在使用asp.net repeater的SelectMethod返回我的对象。是否可以获取被退回的物品数量 我最初使用的是C# Repeater SelectMethod-如何获取返回的项目数?,c#,asp.net,C#,Asp.net,我正在使用asp.net repeater的SelectMethod返回我的对象。是否可以获取被退回的物品数量 我最初使用的是,但很快意识到返回的对象数量不正确 <asp:Repeater ID="docResults" runat="server" ItemType="ArchiveViewer.Models.Document" SelectMethod="GetSearchResults" > <HeaderTemplate>
,但很快意识到返回的对象数量不正确
<asp:Repeater ID="docResults" runat="server"
ItemType="ArchiveViewer.Models.Document"
SelectMethod="GetSearchResults" >
<HeaderTemplate>
<p class="result-info">
Found <strong> <%#: Items.Count %> </strong> results.
</p>
</HeaderTemplate>
<ItemTemplate>
<div>
Title: <%#:Item.Metadata.Title %>
</div>
<div>
Author: <%#:Item.Metadata.Author %>
</div>
</ItemTemplate>
</asp:Repeater>
找到结果。
标题:
作者:
编辑:按要求:我的GetSearchResults方法
[WebMethod]
public IEnumerable<Document> GetSearchResults(
[QueryString("query")] string query,
[QueryString("type")] string queryType)
{
IEnumerable<Document> results = null;
try
{
ArchiveSearcher searcher = new ArchiveSearcher();
results = searcher.SearchMetadata(query, queryType, 1, 20);
if (results.Count() > 0)
{
// Display the first search result in the viewer
Document firstResult = results.FirstOrDefault();
hfCurrentDocId.Value = firstResult.DocumentId.ToString();
hfImageDir.Value = firstResult.FolderPath;
hfObjectData.Value = firstResult.JSONPath;
}
}
catch (Exception ex)
{
// Log the exception.
ArchiveViewer.Logic.ExceptionUtility.LogException(ex,
"GetSearchResults in Search.aspx.cs");
}
return results;
}
[WebMethod]
公共IEnumerable GetSearchResults(
[QueryString(“查询”)]字符串查询,
[查询字符串(“类型”)]字符串查询类型)
{
IEnumerable results=null;
尝试
{
ArchiveSearcher=新的ArchiveSearcher();
结果=searcher.SearchMetadata(查询,查询类型,1,20);
如果(results.Count()>0)
{
//在查看器中显示第一个搜索结果
Document firstResult=results.FirstOrDefault();
hfCurrentDocId.Value=firstResult.DocumentId.ToString();
hfImageDir.Value=firstResult.FolderPath;
hfObjectData.Value=firstResult.JSONPath;
}
}
捕获(例外情况除外)
{
//记录异常。
ArchiveViewer.Logic.ExceptionUtility.LogException(例如,
“GetSearchResults in Search.aspx.cs”);
}
返回结果;
}
谢谢 如果用标签和ID替换计数控件:
<HeaderTemplate>
<asp:Label ID="TotalCount" runat="server" />
提取它,并在数据绑定后设置它。请记住,Repeater Items集合中有标题,因此您只想计算Repeater Items。要获取项目计数,请执行以下操作:
Repeater1.Items.Where(i => i.ItemType == ListItemType.Item)
您必须包括要计算的项目类型,如上文所述。您从
中获得的是脚本管理器列表。我不知道为什么…向我们显示GetSearchResults
我已经更新了我的问题。那么,我在执行“Items.Count”时得到了标题?如何获取repeater项?您必须省略标题,因此使用LINQ我认为您可以执行repeater.items.Where(I=>I.ItemType==ListItemType.DataItem)
,它们只是项模板中的项。如果使用备用模板,则该模板有一个单独的项目类型。见:
Repeater1.Items.Where(i => i.ItemType == ListItemType.Item)