Interaction between Ajax and server controls in. net

Source: Internet
Author: User
I tried Ajax components online two days ago. It feels good. But then I began to think about how to make it interact with the server control. For example, if I output a list on it, will I only use js to output an HTML one by one? No !! Now let's talk about how to interact with the DataGrid.

Note: Ajax. NET components can be downloaded from this network. I use for. NET 1.1. Http://ajax.schwarz-interactive.de/

1. Add the reference Ajax. DLL to the reference. (This is nonsense)

2. Create httphandler in Web. config (this is of course in the system. Web string)

<Httphandlers>
<Add verb = "post, get" Path = "ajax/*. ashx" type = "Ajax. pagehandlerfactory, Ajax"/>
</Httphandlers>

3. Add a setting in application_start of Global.

Protected void application_start (Object sender, eventargs E)
{
Ajax. Utility. handlerpath = "ajax ";
}

4. Create a new class demomethods, which provides the method to update the database and output list. In fact, the main idea is to get the HTML generated after the control is run and then output.

1 [Ajax. ajaxmethod]
2 Public int addajaxtable (string name)
3 {
4 // enter a string and update it.
5 sqlconnection conn = new sqlconnection (system. configuration. configurationsettings. etettings ["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 ();
10 cmd. Dispose ();
11 return result;
12}
13
14 [Ajax. ajaxmethod]
15 Public String getajaxtable ()
16 {
17 // This method obtains the HTML generated by the DataGrid.
18 sqlconnection conn = new sqlconnection (system. configuration. configurationsettings. deleettings ["connectionstring"]);
19 sqlcommand cmd = new sqlcommand ("select * From ajaxtable order by ID", Conn );
20 sqldataadapter ap = new sqldataadapter (CMD );
21 datasetds = new dataset ();
22 AP. selectcommand. Connection. open ();
23 AP. Fill (DS, "DB ");
24
25 conn. Dispose ();
26 cmd. Dispose ();
27
28 // instantiate an IRD class and set the data source
29 DataGrid DG = new DataGrid ();
30 DG. datasource = Ds. Tables ["DB"];
31 DG. databind ();
32
33 // instantiate an htmltextwriter class
34 system. Text. stringbuilder strb = new system. Text. stringbuilder ();
35 system. Io. stringwriter Sw = new system. Io. stringwriter (strb );
36 system. Web. UI. htmltextwriter HTW = new htmltextwriter (SW );
37
38 // execute the render of the control and output it to htmltextwriter.
39 DG. rendercontrol (HTW );
40
41 string S = strb. tostring ();
42
43 return s; // finally, this HTML is returned.
44}

5. Create a default. js file 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. Create a default. aspx and add a registered item to the pageload event.

Private void page_load (Object sender, system. eventargs E)
{
Ajax. Utility. registertypeforajax (typeof (ajaxtestprjlib. demomethods ));
}

7. Finally, it is the HTML and Js of default. aspx.

<% @ page Language = "C #" codebehind = "default. aspx. CS "autoeventwireup =" false "inherits =" ajaxtextprjweb. _ default "%>



default </ title>

















In this way, you can use the DataGrid to output the table. The general idea is actually relatively simple. Some brothers will make better decisions in the future. Please post it.

Related Article

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.