


Use jQuery to implement GridView asynchronous sorting and paging code_jquery
Every time the backup management page is requested, the server will transmit all the backup and restore information to the client, and then ui.tabs will fold the two types of information and display them separately. Fortunately, ui.tabs provides me with the ajax function, and each of our A tab can be directly applied to another page
such as:
But in this way, when Restore.aspx has a server-side control, when he interacts with the server For example, GridView comes with sorting and paging is impossible, because every time you interact, it will only display the status of the first time you load the tab (gridView may always display the first page), sometimes even filling the entire page.
To solve this problem, first think of using ajax to prevent all referenced pages from reloading. I tried UpdatePanel but it didn't work, so I thought of juery.
Below I will demonstrate how to implement asynchronous sorting and paging of GridView with jquery.
First of all, we also put a gridview on the page. It will not be used as the actual displayed part of the page, but as an auxiliary html output. When an ajax request comes, we use this GridView and Render is Html output, ajax callback function completes the display. In order not to display the GridView, I set Visible = false in PreRender. You cannot set Visible=false directly, otherwise it will not be rendered into html
Code
Note that we are in the onload of Body A function is specified in the event, which will request the server and return data when the page is loaded. It is an ajax request
The prototype is as follows:
Code
var getPageData=function(i)
{
$.ajax({
url:'Restore.aspx?' new Date() '&page=' i,//Specify pageindex
type:'get',
success:function(data,textStatus)
{
$('#ShowData')[0].innerHTML=data;
},
error:function(XMLHttpRequest,textStatus)
{
//debugger;
$('#ShowData').text(XMLHttpRequest.responseText);
},
complete:function(XMLHttpRequest , textStatus)
{
}
});
The next step is sorting. Specify the sorting field and sorting direction through the get method. The function is as follows:
Code
var sortDataGridView=function( sortExpression,sortDirection)
{
event.returnVaule=false;//Prevent submission server
$.ajax({
url:'Restore.aspx?' new Date() '&sortEx=' sortExpression '&sortDir=' sortDirection,//IE is cached, so add new Date() to prevent the impact of caching
type:'get',
success:function(data, textStatus)
{
$('#ShowData')[0].innerHTML=data;
},
error:function(XMLHttpRequest,textStatus)
{
$('#ShowData').text(XMLHttpRequest. responseText);
},
complete:function(XMLHttpRequest,textStatus)
{
}
});
}
when When clicking the HeadText in the GridView, we need to trigger the sortDataGridView to implement asynchronous sorting and view the original generated content of the GridView. It is actually an A mark
We are going to add an onclick event to this tag and remove the href attribute value to prevent PostBack to the server. Therefore, I do the following processing in the RowDataBound event of GridView:
Code
protected void gvRestore_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 1; i <= 7; i )
{
LinkButton lt = (LinkButton)e.Row.Cells[i].Controls[0];
lt.Attributes["href"] = "#" ;
lt.OnClientClick = string.Format(" return sortDataGridView('{0}','{1}')", lt.CommandArgument, "ASC");
}
}
if (e.Row.RowType == DataControlRowType.Pager)
{
e.Row.Visible = false;
}
}
Go here In the first step, the idea is basically clear. The only thing left is to respond to the ajax request on the server side. It is very simple. Just look at the code directly. Note that the RendControl method of GridView is called to output html.
Now you can implement ajax sorting and paging of gridview. To summarize, the idea is actually very simple, but there are still some detours in the implementation. The main reason is that I originally wanted to use manual examples in the form of code. to a GridView, but it didn't work out in the end because I added a template column. Add an input type='Radio' in the template column. I inherit ITemplate when coding, but I don't know how to implement the binding of value='<%#Eval("operatePath") %>', leaving here a Question, if anyone knows, please tell me.
< label for="Radio1">select
Code
static string sortDirection = "ASC";
protected void Page_Load(object sender, EventArgs e)
{
if (hasKeyName("page"))
{
if (!string.IsNullOrEmpty(Request.QueryString["page"].ToString()))
{
this.gvRestore.PageIndex = int.Parse(Request.QueryString["page"].ToString());
ResponseData(this.gvRestore);
}
}
else
if (hasKeyName("sortEx"))
{
string sortEx = Request.QueryString["sortEx"].ToString();
string sortDir = Request.QueryString["sortDir"].ToString();
if (string.Compare(sortDir, sortDirection, true) == 0)
{
this.gvRestore.Sort(sortEx, SortDirection.Ascending);
sortDirection = "DSAC";
}
else
{
this.gvRestore.Sort(sortEx, SortDirection.Descending);
sortDirection = "ASC";
}
ResponseData(this.gvRestore);
}
}
private bool hasKeyName(string key)
{
string[] keys = Request.QueryString.AllKeys;
foreach (string str in keys)
{
if (String.Compare(key, str, true) == 0)
return true;
}
return false;
}
private void ResponseData(GridView gv)
{
gv.DataSourceID = this.SqlDataSource1.ID;
System.Globalization.CultureInfo info = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter sWriter = new System.IO.StringWriter(info);
System.Web.UI.HtmlTextWriter html = new HtmlTextWriter(sWriter);
gv.DataBind();
if (gv != null)
{
gv.RenderControl(html);
}
Response.Write(html.InnerWriter);
Response.Write(GetNav(gv.PageCount));
Response.Flush();
Response.End();
}
public string GetNav(int pagesize)
{
string NavStr = @"
" (i 1).ToString() @" | " @" | ";
return NavStr;
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
protected void gvRestore_PreRender(object sender, EventArgs e)
{
this.gvRestore.Visible = false;
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

There are two most common ways to paginate PHP arrays: using the array_slice() function: calculate the number of elements to skip, and then extract the specified range of elements. Use built-in iterators: implement the Iterator interface, and the rewind(), key(), current(), next(), and valid() methods are used to traverse elements within the specified range.

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

jQuery is a popular JavaScript library widely used in web development. During web development, it is often necessary to dynamically add new rows to tables through JavaScript. This article will introduce how to use jQuery to add new rows to a table, and provide specific code examples. First, we need to introduce the jQuery library into the HTML page. The jQuery library can be introduced in the tag through the following code:
