Home  >  Article  >  Backend Development  >  asp.net ajax simple example

asp.net ajax simple example

高洛峰
高洛峰Original
2016-12-16 16:28:161186browse

Instance requirements:

Use Ajax technology to implement a page without refreshing and verify whether a user name has been registered.

Requirements:

Add a text box label to the page for entering the user name, and add a button for submitting user data. The results of the verification are printed directly on the page.

The steps are as follows:

1) Open Visual Studio 2005 to create a website

2) Add a control and control in the HTML column of the VS2005 toolbox.

3) Add the tag to in order to write the Ajax engine and basically define an XMLHttpRequest object. However, no initialization operation is performed. As shown in the following code. ​​​​

                                   // Find the text box named "Text1"

             var name=document .getElementById ("Text1");                                                                                                        xmlhttp.open(" Post","AjaxDemo_1.aspx?name="+name.value);

                                                                                                             

                 xmlhttp .send(null);

}

4) Add the content of OnMessageBack() function. The code is as follows:

function OnMessageBack()

                                                                                                                                                                                                                                        // Determine whether the request status and HTTP status can meet the conditions                                                                                                                    Print the returned text to the page

                                                                                                                                                                                                                                                  . ="Button1 " type="button" value="button" onclick="Validation()" />

6) The Ajax engine in the page has been written. In the above code, you can see that the user name is passed through a query string named "name". After the query string is passed to the server, it needs to be processed in the background. Therefore, "AjaxDemo_1.aspx. cs" to add some ADO.NET code to the Page_Load method. The database here uses the "Northwind" sample database. The code is shown below.

protected void Page_Load(object sender, EventArgs e)

                                          SqlConnection con = new SqlConnection(" "); .Value =name;

    con. Open();

int count = (int)com.ExecuteScalar();

con.Close();

                                                                                                                                                                                                                                                                                                                                                                            

     Response.Write("<script>alert('This user has been occupied, please use another username!');</script>");

Get it here Note that when the server uses the Response.Write() method to send a response to the client, the client's Ajax engine intercepts the response stream and processes it in the "OnMessageBack()" method we defined in advance.

For more asp.net ajax simple examples and related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn