Asp.net does not refresh the page.

Source: Internet
Author: User

I personally like to use Repeater. Because of its conciseness, this AjaxPager is targeted to Repeater!
Step by step:
Code
Copy codeThe Code is as follows:
[DefaultProperty ("TotalRecord "),
ToolboxData ("<{0}: AjaxPager runat = server> </{0}: AjaxPager>")]
Public class AjaxPager: WebControl, ICallbackEventHandler
{
Public AjaxPager ()
: Base (HtmlTextWriterTag. Div)
{
This. Load + = new EventHandler (AjaxPager_Load );
}
Void AjaxPager_Load (object sender, EventArgs e)
{
String script = "function AjaxPagerCallBack (returnData) {var parts = returnData. split ('[_]'); document. getElementById ('"+ this. uniqueID. replace ('$', '_') + "'). innerHTML = parts [0]; document. getElementById ('"+ Info. containID + "'). innerHTML = parts [1]} ";
This. Page. ClientScript. RegisterClientScriptBlock (this. Page. GetType (), "AjaxPagerCallBack", script, true );
}
}

Here, a piece of JS Code is registered to the page in the Load event. The AjaxPagerCallBack method performs two operations and puts the data presented by itself into the DIV container generated by the client, that is, the id is this. uniqueID. replace ('$', '_') div, ClientID seems OK! Haha, confused for a moment! The second step is to put the paging data in the div with the id Info. ContainID, which will be discussed below the Info Object.
Variable attributes
Copy codeThe Code is as follows:
# Region variable
Private string _ BarBackGroundColor = "# FFFFFF ";
Private string _ BarLinkColor = "Navy ";
Private string _ BarCurrentColor = "# EEEEEE ";
Private int _ TotalRecord = 50;
Private int _ TotalPage = 0;
Private int _ CurrentIndex = 1;
Private int _ ItemSize = 10;
Private PageInfo _ Info = new PageInfo ();
# Endregion
# Region attributes
# Region appearance
[Description ("Page background color "),
Bindable (true ),
Category ("appearance "),
DefaultValue ("# FFFFFF")]
Public string BarBackGroundColor
{
Get {return _ BarBackGroundColor ;}
Set {_ BarBackGroundColor = value ;}
}
[Description ("pagination bar link digit color "),
Bindable (true ),
Category ("appearance "),
DefaultValue ("Navy")]
Public string BarLinkColor
{
Get {return _ BarLinkColor ;}
Set {_ BarLinkColor = value ;}
}
[Description ("number color of the current page of the paging bar "),
Bindable (true ),
Category ("appearance "),
DefaultValue ("# EEEEEE")]
Public string BarCurrentColor
{
Get {return _ BarCurrentColor ;}
Set {_ BarCurrentColor = value ;}
}
# Endregion
# Region Behavior
[Description ("Total number of records "),
Category ("behavior "),
DefaultValue (50)]
Public int TotalRecord
{
Get {return _ TotalRecord ;}
Set
{
_ TotalRecord = value;
}
}
[Description ("Total pages "),
Category ("behavior "),
DefaultValue (0)]
Public int TotalPage
{
Get {return _ TotalPage ;}
}
[Description ("Bar size "),
Category ("behavior "),
DefaultValue (10)]
Public int BarSize
{
Get {return _ ItemSize ;}
Set
{
Foreach (char c in System. Convert. ToString (value ))
{
If (! Char. IsNumber (c ))
{
_ ItemSize = 10;
Break;
}
}
_ ItemSize = value;
}
}
[Description ("Current page value "),
Category ("behavior "),
DefaultValue (1)]
Public int PageIndex
{
Get {return _ CurrentIndex ;}
Set {_ CurrentIndex = value ;}
}
# Endregion
Public PageInfo Info
{
Get {return _ Info ;}
Set {_ Info = value ;}
}
# Endregion

It is not necessary to elaborate here. The PageInfo is as follows:
PageInfo
Copy codeThe Code is as follows:
[Serializable]
Public class PageInfo
{
Private string _ RepeaterUniqueID;
Private string _ ContainID = "AjaxData ";
Private string _ TableName = string. Empty;
Private string _ IdentityField = "ID ";
Private int _ PageSize = 10;
Private string _ Fields = "*";
Private bool _ IsDesc = true;
Private string _ Content = string. Empty;
Private string _ ConnectStringName = string. Empty;
Public string RepeaterUniqueID
{
Get {return _ RepeaterUniqueID ;}
Set {_ RepeaterUniqueID = value ;}
}
Public string ContainID
{
Get {return _ ContainID ;}
Set {_ ContainID = value ;}
}
Public int PageSize
{
Get {return _ PageSize ;}
Set
{
_ PageSize = int. Parse (value. ToString ());
}
}
Public string TableName
{
Get {return _ TableName ;}
Set {_ TableName = value ;}
}
Public string IdentityField
{
Get {return _ IdentityField ;}
Set {_ IdentityField = value ;}
}
Public string Fields
{
Get {return _ Fields ;}
Set {_ Fields = value ;}
}
Public bool IsDesc
{
Get {return _ IsDesc ;}
Set {_ IsDesc = value ;}
}
Public string Content
{
Get {return _ Content ;}
Set {_ Content = value ;}
}
Public string ConnectStringName
{
Get {return _ ConnectStringName ;}
Set {_ ConnectStringName = value ;}
}
}

This is marked as Serializable because it is saved to ViewState below.
Auxiliary Methods
Copy codeThe Code is as follows:
Private string GetContents ()
{
This. _ TotalPage = (this. TotalRecord/this. Info. PageSize) * this. Info. PageSize = this. TotalRecord )? (This. TotalRecord/this. Info. PageSize): (this. TotalRecord/this. Info. PageSize) + 1 );
Int BeginRecord = (this. PageIndex-1) * this. Info. PageSize + 1;
Int EndRecord = Math. Min (this. PageIndex * this. Info. PageSize, this. TotalRecord );
String PageInfo = string. format ("[<span style = 'color: # cc000000 '> {0} ({1}-{2 }) </span> total <span style = 'color: # cc000000'> {3} </span> <span style = 'color: # CC0000 '> {4} </span> page] ", Info. pageSize, BeginRecord, EndRecord, this. totalRecord, this. totalPage );
StringBuilder PageListStr = new StringBuilder ();
String PageIndexColor = "#000000 ";
Int SingleNumber = this. TotalPage-(TotalPage/BarSize) * BarSize;
Int IntPageForMax = (this. PageIndex-1)/BarSize;
Int MinInt = (1 + BarSize * IntPageForMax );
Int MaxInt = (IntPageForMax + 1) * BarSize)> TotalPage? TotalPage: (IntPageForMax + 1) * BarSize );
If (this. TotalRecord = 0 | this. TotalPage = 0)
{
PageListStr. AppendFormat ("<span style = 'color: '{0}; margin: auto 3px;'> 0 </span>", PageIndexColor );
PageListStr. appendFormat ("[total <span style = 'color: # cc000000'> 0 </span> page/current <span style = 'color: # CC0000 '> 0 </span> page: <span style = 'color: # cc000000'> 0 </span> records, current number of records <span style = 'color: # CC0000 '> 0 </span> to <span style = 'color: # cc000000'> 0 </span>] ");
Return PageListStr. ToString ();
}
Else
{
If (this. TotalPage <= this. BarSize)
{
For (int I = 1; I <= TotalPage; I ++)
{
PageIndexColor = PageIndex = I? "# CC0000": "#000000 ";
If (PageIndex = I)
PageListStr. appendFormat ("<a id = '{0}' style = 'color: {1}; margin: auto 3px; '>{2} </a>", this. uniqueID, PageIndexColor, I );
Else
PageListStr. appendFormat ("<a id = '{0}' style = 'color: {1}; margin: auto 3px; 'href = \" javascript: {2} \ "> {3} </a>", this. uniqueID, PageIndexColor, Page. clientScript. getCallbackEventReference (this, I. toString (), "AjaxPagerCallBack", null), I );
}
PageListStr. AppendFormat ("{0}", PageInfo );
Return PageListStr. ToString ();
}
Else
{
For (int I = MinInt; I <= MaxInt; I ++)
{
PageIndexColor = PageIndex = I? "# CC0000": "#000000 ";
If (PageIndex = I)
PageListStr. appendFormat ("<a id = {0} 'style = 'color: {1}; margin: auto 3px; '>{2} </a>", this. uniqueID, PageIndexColor, I );
Else
PageListStr. appendFormat ("<a id = '{0}' style = 'color: {1}; margin: auto 3px; 'href = \" javascript: {2} \ "> {3} </a>", this. uniqueID, PageIndexColor, Page. clientScript. getCallbackEventReference (this, I. toString (), "AjaxPagerCallBack", null), I );
}
If (PageIndex <= BarSize & TotalPage> BarSize)
{
PageListStr. appendFormat ("<a id = '{0}' href = \" javascript: {1} \ "> next page </a>", this. uniqueID, Page. clientScript. getCallbackEventReference (this, System. convert. toString (BarSize + 1), "AjaxPagerCallBack", null ));
}
If (this. PageIndex> BarSize & (TotalPage-this. PageIndex)> = SingleNumber)
{
Int MultiMinPageIndex = (IntPageForMax * BarSize );
Int MultiMaxPageIndex = (IntPageForMax + 1) * BarSize) + 1;
PageListStr. insert (0, string. format ("<a id = '{0}' href = \" javascript: {1} \ "> previous page </a>", this. uniqueID, Page. clientScript. getCallbackEventReference (this, MultiMinPageIndex. toString (), "AjaxPagerCallBack", null )));
PageListStr. appendFormat ("<a id = '{0}' href = \" javascript: {1} \ "> next page </a>", this. uniqueID, Page. clientScript. getCallbackEventReference (this, MultiMaxPageIndex. toString (), "AjaxPagerCallBack", null ));
}
If (PageIndex> 10 & (TotalPage-PageIndex) <SingleNumber)
{
Int MultiMinPageIndex = (IntPageForMax * BarSize );
PageListStr. insert (0, string. format ("<a id = '{0}' href = \" javascript: {1} \ "> previous page </a>", this. uniqueID, Page. clientScript. getCallbackEventReference (this, MultiMinPageIndex. toString (), "AjaxPagerCallBack", null )));
}
PageListStr. AppendFormat ("{0}", PageInfo );
Return PageListStr. ToString ();
}
}
}
Public void BindData ()
{
Repeater rpt = getRpt ();
Rpt. Visible = true;
SqlHelper helper;
Helper = this. Info. ConnectStringName. IsNullOrEmpty ()? New SqlHelper (): new SqlHelper (Info. ConnectStringName );
If (this. Info. RepeaterUniqueID. IsNullOrEmpty ())
{
Throw new Exception ("The RepeaterUniqueID attribute of Info must be assigned ");
}
Int count = 0;
DataTable dt = helper. GetPageData (Info. TableName, Info. Fields, Info. IdentityField, Info. PageSize, PageIndex, Info. IsDesc, Info. Content, out count );
This. TotalRecord = count;
Rpt. DataShow (dt );
}
Private Repeater getRpt ()
{
Return this. Page. FindControl (this. Info. RepeaterUniqueID) as Repeater;
}

First, thanks to the person who wrote the Pager. I only made a few changes in GetContents (HTML after getting my own page), or I have to calculate it carefully !!
BindData (My SqlHelper is used) is to use the DataBind () method of the server to put the data in the repeater, but not to show it, hey!
GetRpt only finds the Repeater reference
Maintain view status
Copy codeThe Code is as follows:
# Maintain the view status of region
Protected override void LoadViewState (object savedState)
{
Triplet tp = savedState as Triplet;
This. TotalRecord = Convert. ToInt32 (tp. Third );
This. Info = tp. Second as PageInfo;
Base. LoadViewState (tp. First );
}
Protected override object SaveViewState ()
{
Triplet tp = new Triplet ();
Tp. First = base. SaveViewState ();
Tp. Second = Info;
Tp. Third = this. TotalRecord;
Return tp;
}
# Endregion

It does not need to be said here, but PageInfo must be Serializable.
Rewrite Method
Copy codeThe Code is as follows:
# Region rewrite Method
Protected override void RenderContents (HtmlTextWriter writer)
{
Writer. Write (GetContents ());
Base. RenderContents (writer );
}
Protected override void AddAttributesToRender (HtmlTextWriter writer)
{
Writer. AddStyleAttribute ("White-space", "nowrap ");
Writer. AddStyleAttribute ("padding-top", "2px ");
Writer. AddStyleAttribute ("padding-bottom", "2px ");
Writer. AddStyleAttribute ("color", "#949494 ");
Writer. AddStyleAttribute ("font-weight", "bold ");
Writer. AddStyleAttribute ("background-color", this. BarBackGroundColor );
Base. AddAttributesToRender (writer );
}
# Endregion

Needless to say, everyone can understand it at a glance.
Implement ICallbackEventHandler
Copy codeThe Code is as follows:
# Region ICallbackEventHandler Member
Public string GetCallbackResult ()
{
StringBuilder sb = new StringBuilder ();
StringWriter sw = new StringWriter (sb );
GetRpt (). RenderControl (new HtmlTextWriter (sw ));
Return this. GetContents () + "[_]" + sb. ToString ();
}
Public void RaiseCallbackEvent (string eventArgument)
{
Int pageindex = int. Parse (eventArgument );
This. _ CurrentIndex = pageindex;
BindData ();
}
# Endregion

Run RaiseCallbackEvent first during callback, so CurrentIndex is changed and BindData () is executed !!!!
GetCallbackResult is executed in the return time. The string is separated by "[_]" And corresponds to var parts = returnData. split ('[_]') in the preceding registered AjaxPagerCallBack js method.
OK! Simple Ajax paging is simply completed !!!
The Northwind Orders table is called as follows:
The page contains Repeater in <div id = "AjaxData"> </div>
Code
Copy codeThe Code is as follows:
Private void BindPage (string content)
{
SinoHelper. PageInfo info = new SinoHelper. PageInfo ();
Info. PageSize = 5;
Info. RepeaterUniqueID = rpt. UniqueID;
Info. TableName = "Orders ";
Info. Fields = "OrderID, CustomerID, ShipCity ";
Info. IdentityField = "OrderID ";
Info. Content = content;
AjaxPager1.Info = info;
AjaxPager1.BindData ();
}

Attachment download:
Asp.net refreshing pagination

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.