📄 aspnetpager.cs
字号:
{
if (ShowCustomInfoSection == ShowCustomInfoSection.Left || ShowCustomInfoSection == ShowCustomInfoSection.Right)
{
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
}
base.RenderEndTag(writer);
}
writer.WriteLine();
writer.Write("<!-- ");
writer.Write("AspNetPager V" + ver + " for VS2005 End");
writer.WriteLine(" -->");
writer.WriteLine();
}
/// <include file='AspNetPagerDocs.xml' path='AspNetPagerDoc/Method[@name="RenderContents"]/*'/>
protected override void RenderContents(HtmlTextWriter writer)
{
if (PageCount <= 1 && !AlwaysShow)
return;
if (ShowCustomInfoSection == ShowCustomInfoSection.Left)
{
writer.Write(GetCustomInfoText(CustomInfoHTML));
writer.RenderEndTag();
WriteCellAttributes(writer, false);
writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
}
int midpage = ((CurrentPageIndex - 1) / NumericButtonCount);
int pageoffset = midpage * NumericButtonCount;
int endpage = ((pageoffset + NumericButtonCount) > PageCount) ? PageCount : (pageoffset + NumericButtonCount);
this.CreateNavigationButton(writer, "first");
this.CreateNavigationButton(writer, "prev");
if (ShowPageIndex)
{
if (CurrentPageIndex > NumericButtonCount)
CreateMoreButton(writer, pageoffset);
for (int i = pageoffset + 1; i <= endpage; i++)
{
CreateNumericButton(writer, i);
}
if (PageCount > NumericButtonCount && endpage < PageCount)
CreateMoreButton(writer, endpage + 1);
}
this.CreateNavigationButton(writer, "next");
this.CreateNavigationButton(writer, "last");
if ((ShowInputBox == ShowInputBox.Always) || (ShowInputBox == ShowInputBox.Auto && PageCount >= ShowBoxThreshold))
{
string inputClientID = this.UniqueID + "_input";
writer.Write(" ");
if (!string.IsNullOrEmpty(TextBeforeInputBox))
writer.Write(TextBeforeInputBox);
writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "30px");
writer.AddAttribute(HtmlTextWriterAttribute.Value, CurrentPageIndex.ToString());
if (!string.IsNullOrEmpty(InputBoxStyle))
writer.AddAttribute(HtmlTextWriterAttribute.Style, InputBoxStyle);
if (!string.IsNullOrEmpty(InputBoxClass))
writer.AddAttribute(HtmlTextWriterAttribute.Class, InputBoxClass);
if (PageCount <= 1 && AlwaysShow)
writer.AddAttribute(HtmlTextWriterAttribute.ReadOnly, "true");
writer.AddAttribute(HtmlTextWriterAttribute.Name, inputClientID);
writer.AddAttribute(HtmlTextWriterAttribute.Id, inputClientID);
string chkInputScript = "ANP_checkInput(\'" + inputClientID + "\'," + PageCount.ToString() + ")";
string keydownScript = "ANP_keydown(event,\'" + this.UniqueID + "_btn\');";
string clickScript = "if(" + chkInputScript + "){ANP_goToPage(document.getElementById(\'" + inputClientID + "\'));}";
writer.AddAttribute("onkeydown", keydownScript);
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
if (!string.IsNullOrEmpty(TextAfterInputBox))
writer.Write(TextAfterInputBox);
writer.AddAttribute(HtmlTextWriterAttribute.Type, (UrlPaging == true) ? "Button" : "Submit");
writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
writer.AddAttribute(HtmlTextWriterAttribute.Id, this.UniqueID + "_btn");
writer.AddAttribute(HtmlTextWriterAttribute.Value, SubmitButtonText);
if (!string.IsNullOrEmpty(SubmitButtonClass))
writer.AddAttribute(HtmlTextWriterAttribute.Class, SubmitButtonClass);
if (!string.IsNullOrEmpty(SubmitButtonStyle))
writer.AddAttribute(HtmlTextWriterAttribute.Style, SubmitButtonStyle);
if (PageCount <= 1 && AlwaysShow)
writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "true");
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, (UrlPaging == true) ? clickScript : "return " + chkInputScript);
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
}
if (ShowCustomInfoSection == ShowCustomInfoSection.Right)
{
writer.RenderEndTag();
WriteCellAttributes(writer, false);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.Write(GetCustomInfoText(CustomInfoHTML));
}
}
#endregion
#region Private Helper Functions
/// <summary>
/// Add attributes to the cell of CustomInfoSection or navigation buttons section.
/// </summary>
/// <param name="writer"></param>
/// <param name="leftCell"></param>
private void WriteCellAttributes(HtmlTextWriter writer, bool leftCell)
{
string customUnit = CustomInfoSectionWidth.ToString();
if (ShowCustomInfoSection == ShowCustomInfoSection.Left && leftCell || ShowCustomInfoSection == ShowCustomInfoSection.Right && !leftCell)
{
if (CustomInfoClass != null && CustomInfoClass.Trim().Length > 0)
writer.AddAttribute(HtmlTextWriterAttribute.Class, CustomInfoClass);
if (CustomInfoStyle != null && CustomInfoStyle.Trim().Length > 0)
writer.AddAttribute(HtmlTextWriterAttribute.Style, CustomInfoStyle);
writer.AddAttribute(HtmlTextWriterAttribute.Valign, "bottom");
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, customUnit);
writer.AddAttribute(HtmlTextWriterAttribute.Align, CustomInfoTextAlign.ToString().ToLower());
}
else
{
if (CustomInfoSectionWidth.Type == UnitType.Percentage)
{
customUnit = (Unit.Percentage(100 - CustomInfoSectionWidth.Value)).ToString();
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, customUnit);
}
writer.AddAttribute(HtmlTextWriterAttribute.Valign, "bottom");
writer.AddAttribute(HtmlTextWriterAttribute.Align, HorizontalAlign.ToString().ToLower());
}
writer.AddAttribute(HtmlTextWriterAttribute.Nowrap, "true");
}
/// <summary>
/// Get the navigation url for the paging button.
/// </summary>
/// <param name="pageIndex">the page index correspond to the button.</param>
/// <returns>href string for the paging navigation button.</returns>
private string GetHrefString(int pageIndex)
{
if (UrlPaging)
{
if (EnableUrlRewriting)
{
Regex reg = new Regex("(?<p>%(?<m>[^%]+)%)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
MatchCollection mts = reg.Matches(UrlRewritePattern);
string prmValue;
foreach (Match m in mts)
{
prmValue = urlParams[m.Groups["m"].Value];
if (prmValue != null)
UrlRewritePattern = UrlRewritePattern.Replace(m.Groups["p"].Value, prmValue);
}
return ResolveUrl(string.Format(UrlRewritePattern, (pageIndex == -1) ? "\"+pi+\"" : pageIndex.ToString()));
}
else
{
NameValueCollection col = new NameValueCollection();
col.Add(UrlPageIndexName, (pageIndex == -1) ? "\"+pi+\"" : pageIndex.ToString());
return BuildUrlString(col);
}
}
return Page.ClientScript.GetPostBackClientHyperlink(this, pageIndex.ToString());
}
/// <summary>
/// Replace the property placeholder in the CustomInfoText with the property value repectively
/// </summary>
/// <param name="origText">original CustomInfoText</param>
/// <returns></returns>
private string GetCustomInfoText(string origText)
{
if (!string.IsNullOrEmpty(origText) && origText.IndexOf('%') >= 0)
{
string[] props = new string[] { "recordcount", "pagecount", "currentpageindex", "startrecordindex", "endrecordindex", "pagesize", "pagesremain", "recordsremain" };
StringBuilder sb = new StringBuilder(origText);
Regex reg = new Regex("(?<ph>%(?<pname>\\w{8,})%)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
MatchCollection mts = reg.Matches(origText);
foreach (Match m in mts)
{
string p = m.Groups["pname"].Value.ToLower();
if (Array.IndexOf(props, p) >= 0)
{
string repValue = null;
switch (p)
{
case "recordcount":
repValue = RecordCount.ToString(); break;
case "pagecount":
repValue = PageCount.ToString(); break;
case "currentpageindex":
repValue = CurrentPageIndex.ToString(); break;
case "startrecordindex":
repValue = StartRecordIndex.ToString(); break;
case "endrecordindex":
repValue = EndRecordIndex.ToString(); break;
case "pagesize":
repValue = PageSize.ToString(); break;
case "pagesremain":
repValue = PagesRemain.ToString(); break;
case "recordsremain":
repValue = RecordsRemain.ToString(); break;
}
if (repValue != null)
sb.Replace(m.Groups["ph"].Value, repValue);
}
}
return sb.ToString();
}
return origText;
}
/// <summary>
/// add paging parameter and value to the current url or change parameter value if it already exists when using url paging mode
/// </summary>
/// <param name="col">url parameter collection to be added to the current url</param>
/// <returns>href string for the navigattion buttn</returns>
private string BuildUrlString(NameValueCollection col)
{
int i;
string tempstr = "";
if (urlParams == null || urlParams.Count <= 0)
{
for (i = 0; i < col.Count; i++)
{
tempstr += String.Concat("&", col.Keys[i], "=", col[i]);
}
return String.Concat(Path.GetFileName(currentUrl), "?", tempstr.Substring(1));
}
NameValueCollection newCol = new NameValueCollection(urlParams);
string[] newColKeys = newCol.AllKeys;
for (i = 0; i < newColKeys.Length; i++)
{
if (newColKeys[i] != null)
newColKeys[i] = newColKeys[i].ToLower();
}
for (i = 0; i < col.Count; i++)
{
if (Array.IndexOf(newColKeys, col.Keys[i].ToLower()) < 0)
newCol.Add(col.Keys[i], col[i]);
else
newCol[col.Keys[i]] = col[i];
}
StringBuilder sb = new StringBuilder();
for (i = 0; i < newCol.Count; i++)
{
if (newCol.Keys[i] != null)
{
sb.Append("&");
sb.Append(newCol.Keys[i]);
sb.Append("=");
if (newCol.Keys[i] == UrlPageIndexName)
sb.Append(newCol[i]);
else
sb.Append(Page.Server.UrlEncode(newCol[i]));
}
}
return String.Concat(Path.GetFileName(currentUrl), "?", sb.ToString().Substring(1));
}
/// <summary>
/// Create first, prev, next or last button.
/// </summary>
/// <param name="writer">A <see cref="System.Web.UI.HtmlTextWriter"/> that represents the output stream to render HTML content on the client.</param>
/// <param name="btnname">the button name</param>
private void CreateNavigationButton(HtmlTextWriter writer, string btnname)
{
if (!ShowFirstLast && (btnname == "first" || btnname == "last"))
return;
if (!ShowPrevNext && (btnname == "prev" || btnname == "next"))
return;
string linktext = "";
bool disabled;
int pageIndex;
bool imgButton = (PagingButtonType == PagingButtonType.Image && NavigationButtonType == PagingButtonType.Image);
if (btnname == "prev" || btnname == "first")
{
disabled = (CurrentPageIndex <= 1);
if (!ShowDisabledButtons && disabled)
return;
pageIndex = (btnname == "first") ? 1 : (CurrentPageIndex - 1);
writeSpacingStyle(writer);
if (imgButton)
{
if (!disabled)
{
writer.AddAttribute(HtmlTextWriterAttribute.Href, GetHrefString(pageIndex));
AddToolTip(writer, pageIndex);
writer.RenderBeginTag(HtmlTextWriterTag.A);
writer.AddAttribute(HtmlTextWriterAttribute.Src, String.Concat(ImagePath, btnname, ButtonImageNameExtension, ButtonImageExtension));
writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
if (ButtonImageAlign != ImageAlign.NotSet)
writer.AddAttribute(HtmlTextWriterAttribute.Align, ButtonImageAlign.ToString());
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();
writer.RenderEndTag();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -