Asp.net + Jquery implement functions similar to the Gridview (1), jquerygridview

Source: Internet
Author: User

Asp.net + Jquery implement functions similar to the Gridview (1), jquerygridview

Unconsciously, in 2015, the company's staff left the company one after another (this ...), the project is busy, and it has been busy since the Chinese New Year. Recently, the project has finally come to an end and is on the right track (you don't have to catch up with the project every day ). So I only have time to write this item recently. Maybe I will start from the first demo and implement the functions I want step by step during the writing process, if you are bored, ignore it.

Requirements:

I want to use Jquery to implement a function similar to the Gridview.

Main functions

1. Display Data

2. Paging

3. Sorting

4. Good style

These are implemented first, and then optimized for expansion.

Okay. Let's get started!

Create a blank project with only one simple page and one jquery library. You need to put the implemented method in the JScript. js file.

 

Foreground Html:

1 <% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %> 2 3 <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN "" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 4 5 The front-end only uses a div container to display the GridView. There is a hidden field on the page that stores the Json data retrieved from the background. ajax can also be used to asynchronously retrieve the data, which is directly transmitted in the background.

Background code:

 

1 private void BindGridView () 2 {3 DataTable dt = new DataTable (); 4 dt. columns. add ("ID"); 5 dt. columns. add ("Name"); 6 dt. columns. add ("Age"); 7 dt. columns. add ("Sex"); 8 dt. columns. add ("Score"); 9 DataRow dr = null; 10 Random r = new Random (); 11 for (int I = 0; I <20; I ++) 12 {13 dr = dt. newRow (); 14 dr ["ID"] = I; 15 dr ["Name"] = "Name _" + I; 16 dr ["Sex"] = (I % 2 = 0? "Female": "male"); 17 dr ["Age"] = r. next (16, 27); 18 dr ["Score"] = r. next (60,100); 19 dt. rows. add (dr); 20} 21 22 // datatable to json23 string strJson = DataTable2Json (dt); 24 hfJsonValue. value = strJson; 25}View Code

One method is used to convert a able to a data in Json format. Here is only a simple method, which is complicated and does not apply.

The Code is as follows:

1 public static string DataTable2Json (DataTable dt) {2 StringBuilder jsonBuilder = new StringBuilder (); 3 jsonBuilder. append ("["); 4 for (int I = 0; I <dt. rows. count; I ++) 5 {6 jsonBuilder. append ("{"); 7 for (int j = 0; j <dt. columns. count; j ++) 8 {9 jsonBuilder. append ("\" "); 10 jsonBuilder. append (dt. columns [j]. columnName); 11 jsonBuilder. append ("\": \ ""); 12 jsonBuilder. append (dt. rows [I] [j]. toString (); 13 jsonBuilder. append ("\", "); 14} 15 jsonBuilder. remove (jsonBuilder. length-1, 1); 16 jsonBuilder. append ("},"); 17} 18 jsonBuilder. remove (jsonBuilder. length-1, 1); 19 jsonBuilder. append ("]"); 20 // jsonBuilder. append ("}"); 21 return jsonBuilder. toString (); 22}View Code

Isn't it very simple, and the data is also available, so how to display them on the page, the following begins to implement a js method in the above Html code

Directly paste the Code:

1 function ShowGridView (strJson) {2 3 var jsonVal = eval (strJson); 4 5 var headHtml = "<table width = '000000'> <tr> "; 6 var rowHtml = "<table width = '000000'>"; 7 8 for (var I = 0; I <jsonVal. length; I ++) {9 rowHtml + = "<tr>"; 10 11 $. each (jsonVal [I], function (key, value) {12 if (I = 0) {13 headHtml + = "<td>" + key + "</td> "; 14} 15 rowHtml + = "<td>" + value + "</td>"; 16}); 17 rowHtml + = "</tr> "; 18} 19 20 21 headHtml + = "</tr> <table>"; 22 rowHtml + = "</table> "; 23 24 $ ("# divHeadTemplate" ).html (headHtml); 25 $ ("# divRowTemplate" ).html (rowHtml); 26}View Code

Json data is parsed here. The header is written into the divHeadTemplate container by means of html splicing, And the row is written into the divRowTemplate container.

All our work is done here.

Let's take a look at the effect:

The style is ugly, but it is the effect I want. The data is displayed, but what is the purpose?

Today, I want to write it here. I am sleepy, I am asleep, and I am not going to continue ..

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.