asp.net without refreshing the page is so simple _ practical skills

Source: Internet
Author: User
Tags prev
Individuals like to use repeater, because of its simplicity, this ajaxpager directed to repeater!
In a step-by-step view:
Code
Copy Code code 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];d Ocument.getelementbyid (' + Info.containid + '). Innerhtml=parts [1]} ";
This. Page.ClientScript.RegisterClientScriptBlock (this. Page.gettype (), "Ajaxpagercallback", script, True);
}
}

Here in the Load event to the page to register a Js,ajaxpagercallback method to do two operations, the data of their own performance into the client-generated div container, that ID is this. The Div,clientid of Uniqueid.replace (' $ ', ' _ ') seems to be OK too! Ah, a momentary confusion! The second step is to put the paging data into the div ID Info.containid, the Info object will say below.
Variable Properties
Copy Code code as follows:

#region variables
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 Properties
#region appearance
[Description ("Pagination background color"),
Bindable (True),
Category ("appearance"),
DefaultValue ("#FFFFFF")]
public string Barbackgroundcolor
{
get {return _barbackgroundcolor;}
set {_barbackgroundcolor = value;}
}
[Description ("Pagination link digital color"),
Bindable (True),
Category ("appearance"),
DefaultValue ("Navy")]
public string Barlinkcolor
{
get {return _barlinkcolor;}
set {_barlinkcolor = value;}
}
[Description (page current page number color),
Bindable (True),
Category ("appearance"),
DefaultValue ("#EEEEEE")]
public string Barcurrentcolor
{
get {return _barcurrentcolor;}
set {_barcurrentcolor = value;}
}
#endregion
#region Behavior
[Description ("Total record number"),
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

There is no need to dwell here. The PageInfo are as follows:
PageInfo
Copy Code code 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 the following is to be stored in viewstate.
Auxiliary methods
Copy Code code 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 ([[per page <span style= ' color: #CC0000 ' >{0} ({1}-{2}) </span> strip total <span ' color: #CC0000 ' style=} </span> bar <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: #CC0000 ' >0</span> page/current <span style= ' color: #CC0000" >0</span> page Total <span style= ' color: #CC0000 ' >0</span> records, current record number <span style= ' color: #CC0000 ' >0 </span> to <span style= ' color: #CC0000 ' >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}\ "> Prev </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 > && (totalpage-pageindex) < Singlenumber)
{
int multiminpageindex = (Intpageformax * barsize);
Pageliststr.insert (0, String. Format ("<a id= ' {0} ' href=\" javascript:{1}\ "> Prev </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 ("must assign value to the Repeateruniqueid property of info");
}
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 of all, thank you for writing that pager person, getcontents (get their own pagination after the HTML) I only made a little change, or else have to carefully calculate!!
Binddata (used my sqlhelper) is to use the DataBind () method for the server to put the data into the repeater, just don't let it show, hey!
GETRPT just find Repeater references
Maintain view state
Copy Code code as follows:

#region Maintain view state
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. a);
}
protected override Object SaveViewState ()
{
Triplet TP = new Triplet ();
Tp. i = base. SaveViewState ();
Tp. Second = Info;
Tp. Third = this. Totalrecord;
return TP;
}
#endregion

Here also needless to say, just pageinfo must be able to serializable.
overriding methods
Copy Code code as follows:

#region Override 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

No need to say, we all see it.
Implement ICallbackEventHandler
Copy Code code as follows:

#region ICallbackEventHandler Members
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

The callback executes the raisecallbackevent first, so the currentindex changes, and Binddata () executes the!!!!
Return time to perform getcallbackresult,string with "[_]" separate, corresponding to the above registered Ajaxpagercallback JS method var parts =returndata.split (' [_] ');
Ok! Simple Ajax paging so simple to complete the!!!
The Northwind Orders table is called as follows:
Page repeater included in <div id= "Ajaxdata" ></div>
Code
Copy Code code 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 ();
}

Attached Download:
asp.net no refresh paging
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.