ajax| Server | interaction | control
It was nice to have an AJAX component on the web for the last two days. But then I started to think how can I let it interact with the server control, for example, I output a list, only with JS one HTML output? No!! Now let me talk about how to interact with the DataGrid.
Note: The Ajax.NET components can be downloaded to this web, and I use the For. NET 1.1 version. 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)
<add verb= "Post,get" path= "Ajax/*.ashx" type= "Ajax.pagehandlerfactory, Ajax"/>
3. Add a setting to the Application_Start of global
protected void Application_Start (Object sender, EventArgs e)
{
Ajax.Utility.HandlerPath = "Ajax";
}
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.
1 [Ajax.ajaxmethod]
2 public int addajaxtable (string name)
3 {
4//Enter a string, and then update
5 SqlConnection conn = new SqlConnection (system.configuration.configurationsettings.appsettings["ConnectionStr ing "]);
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["ConnectionStr ing "]);
SqlCommand cmd = new SqlCommand ("SELECT * from ajaxtable ORDER by ID", conn);
SqlDataAdapter ap = new SqlDataAdapter (cmd);
DataSet ds = 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 to store the JS method function Addajax (name)
{
Demomethods.addajaxtable (name);
Loadgrid ();
}
function Loadgrid ()
{
var Cc=document.getelementbyid ("Uctd");
Cc.innerhtml=demomethods.getajaxtable (). value;
}
6. Build a default.aspx, add a registered thing in the Pageload event
private void Page_Load (object sender, System.EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax (typeof (Ajaxtestprjlib.demomethods));
}
7. The last is Default.aspx HTML and JS, hehe
<%@ Page language= "C #" codebehind= "Default.aspx.cs" autoeventwireup= "false" inherits= "Ajaxtextprjweb._default"% >
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title>default</title>
<meta name= "generator" content= "Microsoft Visual Studio. NET 7.1" >
<meta name= "Code_language" content= "C #" >
<meta name= "vs_defaultClientScript" content= "JavaScript" >
<meta name= "vs_targetschema" content= "http://schemas.microsoft.com/intellisense/ie5" >
<script language= "javascript" src= "Default.js" ></script>
</HEAD>
<body >
<form id= "Form1" method= "POST" runat= "Server" >
<input type= "text" id= "AddTextbox" maxlength= "ten" ><input type= "button" value= "Add" >
<table>
<tr>
<TD id= "UCTD" ></td>
</tr>
</table>
</form>
</body>
</HTML>
This way, you can use the DataGrid to output the table, hehe. The overall idea is actually relatively simple, in the future have brothers to make better, please post it, and I will continue to work hard. Thank you.... Go on with your work ....
Original address: http://jasmineou.cnblogs.com/archive/2006/01/19/244312.html#320070
Three months ago, I wrote a post about Ajax.NET, after reading, the response is very large. So I've been trying to dig deeper and see if I can make some improvements, but at this point in my emotional life something very tragic happened ... So have been inadvertently tired. Three months later, I picked up the vs.net and began to work and study again. Actually I this also is not what research, I to the ajax.net frame understanding and the Ajax principle only is very one-sided, only then I thought other people did not think of the thing, hoped everybody later many exchanges, hoped that I this post is helpful to everybody.
Note: This time use is asp.net2.0, control is the GridView, still use asp.net1.1 friend embarrassed. The Ajax.NET framework was also slightly changed, using Ajaxpro for. net2.0 (in fact).
This time the code I will not be exhaustive, because it is still quite a lot. The settings for Ajax.NET can be referenced in this article ajax.net interaction with the server control (a), the demo can be seen here http://210.21.123.253/AjaxTestPrjWeb/ Default.aspx (This is my machine, we have to cherish ah, do not mess Ah, demo time from the morning 9:30-18:00, but also please pay attention to the upper right corner of the loading words ... loading when you do not press), the source file is here to download http ://www.cnblogs.com/files/jasmineou/ajaxtestprj2.0.rar
I parse the specific idea, in fact, is to put the GridView into a UC, and then separate the UC code into the class library, and then the page Ajax to the server to UC generated HTML, and then use JavaScript to write to the page ... ~ In fact, this is only the use of Ajax to achieve no refresh, in terms of performance and not too much improvement. The Master may laugh I use these essentially not be used Ajax .... Oh ~ this I admit.
PS: Any questions can add my qq:50446177, verify the information write Cnblogs with Ajax