Very good extjsdemo ~~~
Ext2.0 and later versions must be referenced in the extjsgrid page.
1. Create an Asp.net Web Application
2. Create a database db_test in the app_data folder.
Create two tables and use the foreign key of use_id to find the score of the student.
'
3. insert some data, which can be customized.
4. Right-click the solution and create a class library model (this class library abstracts a class model for the tables in the database
)
If: the front-end displays use_id, use_name, use_sex, use_address, sco_subject, and sco_score of the student.
Create a show_score.cs
Code
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Namespace Model
{
Public class show_score
{
Private int _ use_id;
Public int use_id // field user_id
{
Get {return _ use_id ;}
Set {_ use_id = value ;}
}
Private string _ use_name;
Public String use_name // field use_name
{
Get {return _ use_name ;}
Set {_ use_name = value ;}
}
Private string _ use_sex;
Public String use_sex // field use_sex
{
Get {return _ use_sex ;}
Set {_ use_sex = value ;}
}
Private string _ use_address;
Public String use_address // field use_address
{
Get {return _ use_address ;}
Set {_ use_address = value ;}
}
Private string _ sco_subject;
Public String sco_subject // field sco_subject
{
Get {return _ sco_subject ;}
Set {_ sco_subject = value ;}
}
Private int _ sco_score;
Public int sco_score
{
Get {return _ sco_score ;}
Set {_ sco_score = value ;}
}
}
}
5. Create a public service library service
Create dbhelper. CS and show_score_service.cs in Service
5. Create a public service library service
Create dbhelper. CS and show_score_service.cs in Service
Dbhelper. CS
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Data. sqlclient; // database connection namespace
Using system. Data; // dataset namespace
Namespace Service
{
Public class dbhelper
{
Private Static sqlconnection connection; // create a connection object
Public static sqlconnection conneciton
{
The get // get keyword defines the accessor method in the attribute or indexer to retrieve the value of this attribute or the indexer element.
{
String temp_connectionstring = @ "Data Source =./sqlexpress; attachdbfilename = E:/your security = true; user instance = true ";
If (connection = NULL)
{
Connection = new sqlconnection (temp_connectionstring );
Connection. open ();
}
// System. Data. connectionstate describes the current status of the connection to the data source.
// The connection between the broken and the data source is interrupted. This can only happen after the connection is opened. You can close the connection in this status and then open it again. (This value is reserved for future versions of this product .)
// The closed connection is closed.
// The connecting connection object is connecting to the data source. (This value is reserved for future versions of this product .)
// Executing the connection object is executing the command. (This value is reserved for future versions of this product .)
// The fetching connection object is retrieving data. (This value is reserved for future versions of this product .)
// The open connection is open.
Else if (connection. State = system. Data. connectionstate. Closed)
{
Connection. open ();
}
Else if (connection. State = system. Data. connectionstate. Broken)
{
Connection. Close ();
Connection. open ();
}
Return connection;
}
}
// Executenonquery: execute a Transact-SQL statement on the connection and return the number of affected rows.
Public static int executecommand (string temp_ SQL)
{
Sqlcommand temp_cmd = new sqlcommand (temp_ SQL, conneciton );
Int temp_result = temp_assist.executenonquery ();
Return temp_result;
}
// Execute the query and return the first column in the first row of the returned result set. Ignore other columns or rows.
Public static int getscalar (string temp_ SQL)
{
Sqlcommand temp_cmd = new sqlcommand (temp_ SQL, conneciton );
Int temp_result = convert. toint32 (temp_0000.executescalar ());
Return temp_result;
}
// Provides a stream-only method for reading rows from the SQL Server database.
Public static sqldatareader getreader (string temp_ SQL)
{
Sqlcommand temp_cmd = new sqlcommand (temp_ SQL, conneciton );
Sqldatareader temp_reader = temp_cmd.executereader ();
Return temp_reader;
}
// Obtain the record set of the table
Public static datatable getdataset (string temp_ SQL)
{
Dataset temp_ds = new dataset ();
Sqlcommand temp_cmd = new sqlcommand (temp_ SQL, conneciton );
Sqldataadapter temp_da = new sqldataadapter (temp_cmd );
Temp_da.fill (temp_ds );
Return temp_ds.tables [0];
}
}
}
Show_score_service.cs
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using model; // after adding the program reference set, add the namespace
Using system. Data. sqlclient;
Namespace Service
{
Public class show_score_service
{
Public list <show_score> getalluser ()
{
String temp_str = "select a. use_id, use_name," +
"Use_sex, use_address, sco_subject, sco_score from tb_user A, tb_score B a. use_id = B. use_id ";
List <show_score> temp_list = new list <show_score> ();
Temp_list = getbysql (temp_str, null );
If (temp_list.count> 0) {return temp_list ;}
Else {return NULL ;}
}
Private Static list <show_score> getbysql (string temp_ SQL, sqlparameter [] temp_values)
{
Using (sqldatareader temp_reader = dbhelper. getreader (temp_ SQL ))
{
List <show_score> temp_list = new list <show_score> ();
Show_score temp_user = NULL;
While (temp_reader.read ())
{
Temp_user = new show_score ();
Temp_user.use_id = int. parse (temp_reader ["use_id"]. tostring ());
Temp_user.use_name = temp_reader ["use_name"]. tostring ();
Temp_user.use_sex = temp_reader ["use_sex"]. tostring ();
Temp_user.use_address = temp_reader ["use_address"]. tostring ();
Temp_user.sco_subject = temp_reader ["sco_subject"]. tostring ();
Temp_user.sco_score = int. parse (temp_reader ["sco_score"]. tostring ());
Temp_list.add (temp_user );
}
Temp_reader.close ();
Return temp_list;
}
}
}
}
6. Create show_grid.aspx and add the reference and namespace of the service library service and class model.
Show_grid.aspx
<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "show_grid.aspx.cs" inherits = "extjs_vs2008_asp.net data interaction. show_grid" %>
Show_grid.aspx.cs
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using model; // Add a namespace
Using service;
Using system. Data. sqlclient;
Using system. Web. Script. serialization; // JSON serialization
Using system. text;
Namespace extjs_vs2008_asp.net Data Interaction
{
Public partial class show_grid: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
String temp_json = "";
String temp_type = request. querystring ["parm"]. tostring ();
If (temp_type = "list ")
{
Temp_json = dolist ();
Response. Write (temp_json );
}
}
Private string dolist ()
{
Show_score_service temp_ser = new show_score_service ();
List <show_score> temp_list = temp_ser.getalluser ();
Javascriptserializer Java = new javascriptserializer ();
Stringbuilder temp_sb = new stringbuilder ();
Java. serialize (temp_list, temp_sb );
String temp_json = temp_sb.tostring ();
Return temp_json;
}
}
}
7. Directory files to be introduced
A. Paging is required. I used to use pagingmemoryproxy. js (which can be searched from the extjs folder) Paging to introduce the solution
B. Introduce the extjs file into the solution
C. edit a my_datagrid.js file.
Ext. onready (function (){
Store = new Ext. Data. jsonstore ({
Data: [],
Fields :[
{Name: 'Use _ id '},
{Name: 'Use _ name '},
{Name: 'Use _ sex '},
{Name: 'Use _ address '},
{Name: 'sco _ subobject '},
{Name: 'sco _ score '}
]
});
Ext. Ajax. Request ({// read data transmitted from the background to the foreground
URL: 'Show _ grid. aspx? Parm = list ',
Method: 'get ',
Success: function (response, opts ){
VaR OBJ = ext. Decode (response. responsetext); // OBJ stores the response data
Store. Proxy = new Ext. Data. pagingmemoryproxy (OBJ), // pagingmemoryproxy () One-time read data
Store. Load ({Params: {start: 0, limit: 4}); // distribute by 4 records
},
Failure: function () {Ext. msg. Alert ("failure ");}
});
VaR grid = new Ext. Grid. gridpanel ({
Frame: True,
Title: 'student subject table ',
Striperows: True, // zebra crossing
Store: store,
ID: 'grid ',
Applyto: 'grid ',
Trackmouseover: True,
Height: 300,
Width: 500,
Loadmask: {MSG: 'loading data. Please wait ...... '},
Viewconfig :{
Forcefit: True
},
Columns :[
New Ext. Grid. rownumberer (), // row number
{Header: '<font size = 2> User Account </font>', dataindex: 'Use _ id', sortable: true },
{Header: '<font size = 2> User Name </font>', dataindex: 'Use _ name', sortable: true },
{Header: '<font size = 2> User gender </font>', dataindex: 'Use _ sex ', sortable: true },
{Header: '<font size = 2> User address </font>', dataindex: 'Use _ address', sortable: true },
{Header: '<font size = 2> exam subjects </font>', dataindex: 'sco _ subject ', sortable: true },
{Header: '<font size = 2> test score </font>', dataindex: 'sco _ score ', sortable: true}
],
Bbar: New Ext. pagingtoolbar ({// pagination
Pagesize: 4,
Store: store,
Displayinfo: True, // it must be true. Otherwise, the following paging button is not displayed.
Displaymsg: '<font size = 2> entries {0} to {1}, total {2} records </font> ',
Emptymsg: "No record"
})
})
})
8 writing default. aspx at the front end
<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "default. aspx. cs" inherits = "extjs_asp.net frontend and backend data interaction. _ default" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<LINK rel = "stylesheet" type = "text/CSS" href = "extjs/resources/CSS/ext-all.css"/>
<SCRIPT type = "text/JavaScript" src = "extjs/adapter/EXT/ext-base.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "extjs/ext-all.js"> </SCRIPT>
<SCRIPT src = "pagingmemoryproxy. js" type = "text/JavaScript"> </SCRIPT>
<SCRIPT src = "my_datagrid.js" type = "text/JavaScript"> </SCRIPT>
<Title> interaction between extjs and Asp.net frontend and backend </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div id = "Grid"> </div>
</Form>
</Body>
</Html>
9 Running Effect
========================================================== ==========================================
========================================================== ==========================================