CommunityServer2.0分页控件的优化
记得去年NetFocus在群上说CommunityServer2.0的分页控件存在性能问题,当时没有留意,想不到最近就被我就碰上了,拿Pager类来说,出现问题的代码如下:
从代码中可以看出,程序直接实例化总页数多个的HyperLink,这样如果总页数过多时,必然会导致内存溢出。
可惜当时没有好好听netfocus说,没有记下他的解决方案,只好自己想就去来实现。
我采用的是对原代码影响最小的方式,只要改变原来的两个方法即可,解决的方法如下:
修改AddPageLinks方法:
修改RenderButtonRange方法:
void AddPageLinks()
{
// First add the lower page buttons
//
//CalculateTotalPages方法是在计算总页数
pagingHyperLinks = new HyperLink[CalculateTotalPages()];
// Create the buttons and add them to
// the Controls collection
//
for (int i = 0; i < pagingHyperLinks.Length; i++)
{
pagingHyperLinks[i] = new HyperLink();
pagingHyperLinks[i].EnableViewState = false;
pagingHyperLinks[i].Text = (i + 1).ToString();
pagingHyperLinks[i].NavigateUrl = CreatePagerURL((i + 1).ToString());
Controls.Add(pagingHyperLinks[i]);
}
}
{
// First add the lower page buttons
//
//CalculateTotalPages方法是在计算总页数
pagingHyperLinks = new HyperLink[CalculateTotalPages()];
// Create the buttons and add them to
// the Controls collection
//
for (int i = 0; i < pagingHyperLinks.Length; i++)
{
pagingHyperLinks[i] = new HyperLink();
pagingHyperLinks[i].EnableViewState = false;
pagingHyperLinks[i].Text = (i + 1).ToString();
pagingHyperLinks[i].NavigateUrl = CreatePagerURL((i + 1).ToString());
Controls.Add(pagingHyperLinks[i]);
}
}
从代码中可以看出,程序直接实例化总页数多个的HyperLink,这样如果总页数过多时,必然会导致内存溢出。
可惜当时没有好好听netfocus说,没有记下他的解决方案,只好自己想就去来实现。
我采用的是对原代码影响最小的方式,只要改变原来的两个方法即可,解决的方法如下:
修改AddPageLinks方法:
void AddPageLinks()
{
int totalPages = CalculateTotalPages();
int lowerBound = 0;
int upperBound = totalPages;
if (totalPages > 5)
{
lowerBound = (PageIndex - 2);
upperBound = (PageIndex + 3);
if (lowerBound <= 0)
lowerBound = 0;
if (PageIndex == 0 || PageIndex == 1)
{
upperBound = 5;
}
else if (PageIndex < (totalPages - 2))
{
upperBound = PageIndex + 3;
}
else if (PageIndex == (totalPages - 2))
{
lowerBound = totalPages - 5;
upperBound = totalPages;
}
else if (PageIndex == (totalPages - 1))
{
lowerBound = totalPages - 5;
upperBound = totalPages;
}
}
pagingHyperLinks = new HyperLink[upperBound - lowerBound];
for (int i = 0; i < upperBound - lowerBound; i++)
{
pagingHyperLinks[i] = new HyperLink();
pagingHyperLinks[i].EnableViewState = false;
pagingHyperLinks[i].Text = (i + lowerBound + 1).ToString();
pagingHyperLinks[i].NavigateUrl = CreatePagerURL((i + 1 + lowerBound).ToString());
pagingHyperLinks[i].CssClass = "OtherPage";
Controls.Add(pagingHyperLinks[i]);
}
}
{
int totalPages = CalculateTotalPages();
int lowerBound = 0;
int upperBound = totalPages;
if (totalPages > 5)
{
lowerBound = (PageIndex - 2);
upperBound = (PageIndex + 3);
if (lowerBound <= 0)
lowerBound = 0;
if (PageIndex == 0 || PageIndex == 1)
{
upperBound = 5;
}
else if (PageIndex < (totalPages - 2))
{
upperBound = PageIndex + 3;
}
else if (PageIndex == (totalPages - 2))
{
lowerBound = totalPages - 5;
upperBound = totalPages;
}
else if (PageIndex == (totalPages - 1))
{
lowerBound = totalPages - 5;
upperBound = totalPages;
}
}
pagingHyperLinks = new HyperLink[upperBound - lowerBound];
for (int i = 0; i < upperBound - lowerBound; i++)
{
pagingHyperLinks[i] = new HyperLink();
pagingHyperLinks[i].EnableViewState = false;
pagingHyperLinks[i].Text = (i + lowerBound + 1).ToString();
pagingHyperLinks[i].NavigateUrl = CreatePagerURL((i + 1 + lowerBound).ToString());
pagingHyperLinks[i].CssClass = "OtherPage";
Controls.Add(pagingHyperLinks[i]);
}
}
修改RenderButtonRange方法:
void RenderButtonRange(int start, int end, HtmlTextWriter writer)
{
for (int i = 0; i < end - start; i++)
{
// Are we working with the selected index?
//
int realpage = i + start;
if (PageIndex == realpage)
{
// Render different output
Literal l = new Literal();
l.Text = "<span class=""currentPage"">[" + (realpage + 1).ToString() + "]</span>";
l.RenderControl(writer);
}
else
{
pagingHyperLinks[i].RenderControl(writer);
}
if (realpage < (end - 1))
writer.Write(" ");
}
}
{
for (int i = 0; i < end - start; i++)
{
// Are we working with the selected index?
//
int realpage = i + start;
if (PageIndex == realpage)
{
// Render different output
Literal l = new Literal();
l.Text = "<span class=""currentPage"">[" + (realpage + 1).ToString() + "]</span>";
l.RenderControl(writer);
}
else
{
pagingHyperLinks[i].RenderControl(writer);
}
if (realpage < (end - 1))
writer.Write(" ");
}
}