我正在尝试编写一个WebPart代码,它会收集您所属的每个网站上的所有公告,然后显示它们。我必须在两个列表视图中显示它们:即将到来和过去。
我已经全部工作了......它获得了所有正确的声明并将它们放入正确的列表视图中。即将到来的列表视图显示所有即将发布的公告,过去的livst视图显示所有过去的公告。
我遇到的问题是:我们希望每个列表视图只显示10个带有数据页的项目以进行更多页面。
当我添加两个datapagers,每个都指向其各自的列表时,它确实显示1-10,但显示更多的arros不起作用。事实上,它最终显示出好玩的东西:
点击后:< > < 1 - 10>
再次点击: < < > > < 1 - 10>
ascx文件:
<div class="announcement-upcming">
<asp:ListView ID="UpcmingListView" ItemPlaceholderID="UpcmingItem" OnItemDataBound="UpcmingItemDataBound"
runat="server">
<LayoutTemplate>
<ul class="aggregation announcement upcming">
<asp:PlaceHolder runat="server" ID="UpcmingItem"></asp:PlaceHolder>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li class="item announcement upcming" id="liItem" runat="server"><a id="aLink" class="item-title" runat="server"></a>
<div style="font-size: 80%">
<a id="aSiteLink" target="_blank" runat="server"></a>
</div>
<div id="expireDate" runat="server" style="font-size: 80%"></div>
</li>
</ItemTemplate>
</asp:ListView>
<asp:Panel ID="pnlEmptyUpcming" CssClass="no-items" Visible="false" runat="server">
<ul class="aggregation announcement upcming">
<li>You have no upcoming announcements. </li>
</ul>
</asp:Panel>
<center>
<asp:datapager id="UpcmingListingDataPager" pagedcontrolid="UpcmingListView" runat="server">
<fields></fields>
</asp:datapager>
</center>
</div>
代码隐藏(ascx.cs文件):
private void BuildUpcmingPagingControlFields()
{
PagingControlTemplate pagingControl = new PagingControlTemplate();
TemplatePagerField field = new TemplatePagerField();
field.PagerTemplate = pagingControl;
UpcmingListingDataPager.PageSize = NumberOfUpcmingItems;
if (UpcmingListingDataPager.PageSize < (UpcmingListingDataPager.TotalRowCount + 1))
{
UpcmingListingDataPager.Fields.Add(pagingControl.GetBegginingPagerField());
UpcmingListingDataPager.Fields.Add(field);
UpcmingListingDataPager.Fields.Add(pagingControl.GetEndingPagerField());
}
else
{
UpcmingListingDataPager.Fields.Add(field);
}
UpcmingListView.DataBind();
UpcmingListView.PagePropertiesChanged += UpcmingListing_PagePropertiesChanged;
}
protected void UpcmingListing_PagePropertiesChanged(object sender, EventArgs e)
{
this.UpcmingListView.DataBind();
}
protected void UpcmingItemDataBound(object obj, ListViewItemEventArgs ea)
{
try
{
DatedItem item = (DatedItem)(((ListViewDataItem)ea.Item).DataItem);
HtmlControl li = (HtmlControl)ea.Item.FindControl("liItem");
HtmlAnchor announcementAnchor = (HtmlAnchor)ea.Item.FindControl("aLink");
HtmlAnchor siteAnchor = (HtmlAnchor)ea.Item.FindControl("aSiteLink");
HtmlGenericControl expireAnchor = (HtmlGenericControl)ea.Item.FindControl("expireDate");
DateTime ItemDate = item.Date;
DateTime Tomorrow = DateTime.Now.AddDays(1);
announcementAnchor.InnerText = item.Title;
Uri SiteUri = new Uri(item.Url);
if (item.Url.Contains("/Lists"))
{
siteAnchor.HRef = SiteUri.AbsoluteUri.Remove(SiteUri.AbsoluteUri.IndexOf("/Lists"));
using (SPSite announceSite = new SPSite(SiteUri.AbsoluteUri.Remove(SiteUri.AbsoluteUri.IndexOf("/Lists"))))
{
siteAnchor.InnerText = announceSite.RootWeb.Title;
}
}
else
{
siteAnchor.HRef = SiteUri.AbsoluteUri.Remove(SiteUri.AbsoluteUri.IndexOf("/Announcements"));
using (SPSite announceSite = new SPSite(SiteUri.AbsoluteUri.Remove(SiteUri.AbsoluteUri.IndexOf("/Announcements"))))
{
siteAnchor.InnerText = announceSite.RootWeb.Title;
}
}
if ((ItemDate == null) || (ItemDate == DateTime.MaxValue))
{
expireAnchor.InnerText = "Never expires";
}
else if ((ItemDate.Year == Tomorrow.Year) && (ItemDate.Month == Tomorrow.Month) && (ItemDate.Day == Tomorrow.Day))
{
expireAnchor.InnerText = "Expires tomorrow";
}
else
{
expireAnchor.InnerText = "Expires " + item.Date.ToString("MMMM d, YYYY");
}
announcementAnchor.InnerText = item.Title;
announcementAnchor.HRef = item.Url;
announcementAnchor.Attributes.Add("onclick", string.Format("return OpenDialog('{0}');", item.Url));
}
catch (Exception ex)
{
Logger.DefaultLogger.LogToOperations(ex);
}
}
protected void PopulateUpcmingAnnouncements(List<DatedItem> Announcements)
{
if (Announcements != null)
{
List<DatedItem> upcmingAnnouncements = Announcements.Where(i => i.Date >= DateTime.Today).ToList();
upcmingAnnouncements = upcmingAnnouncements.OrderBy(t => t.Date).ToList();
if (upcmingAnnouncements.Count == 0)
{
pnlEmptyUpcming.Visible = true;
UpcmingListView.Visible = false;
UpcmingListingDataPager.Visible = false;
}
UpcmingListView.DataSource = upcmingAnnouncements;
UpcmingListView.ItemDataBound += UpcmingItemDataBound;
UpcmingListView.DataBind();
NumberOfUpcmingItems = upcmingAnnouncements.Count;
}
else
{
pnlEmptyUpcming.Visible = true;
UpcmingListView.Visible = false;
UpcmingListingDataPager.Visible = false;
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnInit(e);
try
{
{{{Code that gets announcements and places the in TheseAnnouncements}}}
PopulateUpcmingAnnouncements(TheseAnnouncements);
PopulatepstAnnouncements(TheseAnnouncements);
}
catch (Exception ex)
{
Logger.DefaultLogger.LogToOperations(ex);
}
}