Ajax interaction with server controls in. Net

Source: Internet
Author: User
Tags add net reference return string
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

private void Page_Load (






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.



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.