I've a ListView
with a DataPager
the paging in the buttons (1,2,3,...) do not preserve the order of the parameters
in querystring
which is a bad thing in an SEO
context (the bot thinks it is duplicate content).
I.E. if my page is:
Test.aspx?r=8&type=10
the pager buttons show:
Test.aspx?type=8&r=10&page=1
Test.aspx?type=8&r=10&page=2
etc... I think the control is ordering them in alphabetical order?
I've tried to override those URL's on the control PreRender()
event, but it still renders the URL
as described above...
Has anyone faced this issue??
Thanks.
ASPX:
<asp:ListView ID="ListViewSearchResults" runat="server" GroupItemCount="3"
OnItemDataBound="ListViewSearchResults_ItemDataBound"
OnPagePropertiesChanging="ListViewSearchResults_PagePropertiesChanging" >
......
</asp:ListView>
.....
<asp:DataPager ID="ListViewPager" runat="server" QueryStringField="page"
OnDataBinding="ListViewPager_DataBinding" OnPreRender="ListViewPager_PreRender"
PagedControlID="ListViewSearchResults" PageSize="21" >
<Fields>
<asp:NextPreviousPagerField ShowFirstPageButton="true"
ShowPreviousPageButton="false" ShowNextPageButton="false"
ShowLastPageButton="false" ButtonCssClass="pagingright1" ButtonType="Link"
FirstPageText="" />
<asp:NextPreviousPagerField ShowFirstPageButton="false"
ShowPreviousPageButton="true" ShowNextPageButton="false"
ShowLastPageButton="false" ButtonCssClass="pagingright2" ButtonType="Link"
PreviousPageText="" />
<asp:NumericPagerField ButtonType="Link" CurrentPageLabelCssClass="pagingcurrent" RenderNonBreakingSpacesBetweenControls="false" ButtonCount="10" />
<asp:NextPreviousPagerField ShowFirstPageButton="false" ShowPreviousPageButton="false" ShowNextPageButton="true" ShowLastPageButton="false" ButtonCssClass="pagingleft2" ButtonType="Link" NextPageText="" />
<asp:NextPreviousPagerField ShowFirstPageButton="false" ShowPreviousPageButton="false" ShowNextPageButton="false" ShowLastPageButton="true" ButtonCssClass="pagingleft1" ButtonType="Link" LastPageText="" />
</Fields>
</asp:DataPager>
CS: trying to fix the links programmatically
private void FixAllPagerLinks(string FixedUrl)
{
foreach (Control c in ListViewPager.Controls)
{
if(c.HasControls())
{
if(c.Controls[0] is HyperLink)
{
string PageParam = string.Empty;
string CurrentUrl = ((HyperLink)c.Controls[0]).NavigateUrl;
char[] pageparam = new char[CurrentUrl.Length - CurrentUrl.IndexOf("page")];
CurrentUrl.CopyTo(CurrentUrl.IndexOf("page"), pageparam, 0, CurrentUrl.Length - CurrentUrl.IndexOf("page"));
PageParam = new string(pageparam);
((HyperLink)c.Controls[0]).NavigateUrl = string.Format("/{0}&{1}", FixedUrl , PageParam);
}
}
}
}
// in the DataPager PreRender Event nothing happen
//but on page prerender event it show the correct links but after
//clicking on next page in the pager it's messed up again
protected override void OnPreRender(EventArgs e)
{
//getting the URL
string NextPageurl = ((System.Web.UI.WebControls.HyperLink)
(ListViewPager.Controls[0].Controls[0])).NavigateUrl;
//removing the page=1 to get the Basic url of the link
NextPageurl = NextPageurl.Replace("&page=1", string.Empty).Replace("?page=1", string.Empty);
string FullFixedUrl = FixQueryStringParameters(NextPageurl );
FixAllPagerLinks(FullFixedUrl);
}
private string FixQueryStringParameters(string QueryWithParams)
{
string returnQueryStringParams = string.Empty;
string p1 = string.Empty;
string p2 = string.Empty;
string p3 = string.Empty;
Dictionary<string, string> QSKeyValuePairs = new Dictionary<string, string>();
if (QueryWithParams.Split('?').Length > 1)
{
string parameters = QueryWithParams.Split('?')[1];
if (parameters.Split('&').Length > 1)
{
p1 = parameters.Split('&')[0];
QSKeyValuePairs.Add(p1.Split('=')[0], p1.Split('=')[1]);
p2 = parameters.Split('&')[1];
QSKeyValuePairs.Add(p2.Split('=')[0], p2.Split('=')[1]);
if (parameters.Split('&').Length > 2)
{
p3 = parameters.Split('&')[2];
QSKeyValuePairs.Add(p3.Split('=')[0], p3.Split('=')[1]);
}
}
}