ajax|server| Interaction | control
How does an AJAX component allow it to interact with the server control, such as outputting a list, with only one HTML output in JS? Of course not, you can now interact with the DataGrid.
NOTE: Ajax. NET components can be downloaded to this web, and this article is for. NET version 1.1. http://ajax.schwarz-interactive.de/
1. Add Reference Ajax.dll to the reference. (This is nonsense)
2. Establish HttpHandler in Web.config (this is of course in the system.web string)
3. Add a setting to the Application_Start of global
4. Create a new class Demomethods, which provides a way to update the database and output lists. The main idea is to get the HTML that the control generates after it is run, and then output it.
4//Enter a string, and then update
5 SqlConnection conn = new SqlConnection ( system.configuration.configurationsettings.appsettings["connectionString"]);
6 SqlCommand cmd = new SqlCommand ("INSERT into ajaxtable (name) VALUES (' +name+" '), conn);
7 cmd. Connection.Open ();
8 int result = cmd. ExecuteNonQuery ();
9 Conn. Dispose ();
Ten cmd. Dispose ();
one return to result;
12}
13
[Ajax.ajaxmethod]
public string getajaxtable ()
16 {
17//The method is to get the HTML generated by the DataGrid
SqlConnection conn = new SqlConnection (system.configuration.configurationsettings.appsettings["ConnectionString" ]);
SqlCommand cmd = new SqlCommand ("SELECT * from ajaxtable ORDER by ID", conn);
SqlDataAdapter ap = new SqlDataAdapter (cmd);
Datasetds = new DataSet ();
AP. SelectCommand.Connection.Open ();
AP. Fill (ds, "db");
24
Conn. Dispose ();
-CMD. Dispose ();
27
28//Instantiate a Datagird class and set the data source.
DataGrid dg = new DataGrid ();
DG. DataSource = ds. tables["DB"];
To DG. DataBind ();
32
33//Instantiate a HtmlTextWriter class
System.Text.StringBuilder strb = new System.Text.StringBuilder ();
System.IO.StringWriter SW = new System.IO.StringWriter (STRB);
System.Web.UI.HtmlTextWriter HTW = new HtmlTextWriter (SW);
37
38//Execute control render and output to HtmlTextWriter
DG. RenderControl (HTW);
40
A string s = strb. ToString ();
42
The return s;//finally returns this HTML
44} |
5. Then build a default.js file, used for storing JS method
6. Build a default.aspx, add a registered thing in the Pageload event
7. The last is Default.aspx HTML and JS.
<%@ Page language= "C #" codebehind= "Default.aspx.cs"
|
This way, you can use the DataGrid to output the table. The overall idea is actually relatively simple.