Asp.net quick Ajax tutorial

Source: Internet
Author: User

The function implemented in this tutorial is to change the content of another dropdownlist2 with the value of dropdownlist1. The changed data is called from the database.

1. First download the Ajax. dll library.

2. reference the Ajax. All Class Library

3. Configure Ajax in Web. config

Add the following sentence to <system. Web> </system. Web>:

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

3. register the Ajax page during page_load (), for example:

Private void page_load (Object sender, system. eventargs E)
{
Ajax. Utility. registertypeforajax (typeof (webform2); // webform2 is the class name of this page.
}

4. Write JavaScript on the HTML pageCodeSuch:

<Script language = "JavaScript">
Function swinnoresult ()// This method is called by the HTML control, for example, onchange = "swinnoresult ();"
{
VaR swinno = Document. getelementbyid ("ddl_swinno ");
Webform2.Getdataset(Swinno. value,Get_city_result_callback);

//GetdatasetThis method is the method in webform2.aspx. CS, as follows:

[Ajax. ajaxmethod (Ajax. httpsessionstaterequirement. Read.

Code
[Ajax. ajaxmethod (Ajax. httpsessionstaterequirement. Read)]
Public Dataset getdataset ( String Swinno)
{
String Strsql1 =   " Select authorno, authorname from tbl_sys_authorkind where authorno = " + Swinno;
Dataset DS =   New Dataset ();
Mydata. executedataset (commandtype. Text, strsql1, Null , DS, " Eg_bas_serveritem " );
Return DS;
}

//Get_city_result_callbackThis is the JavaScript method to be called after the data is returned, as follows:

}
Function Get_city_result_callback (Response) // response is the value returned by the background
{
If (response. value! = NULL)
{

Document. All ("ddl_sitemno"). Length = 0;
VaR DS = response. value;
VaR strtext = "";
If (Ds! = NULL & typeof (DS) = "object" & Ds. tables! = NULL)
{
Document. All ("ddl_sitemno"). Options. Add (New Option ("= select = ",""));
For (VAR I = 0; I <Ds. Tables [0]. Rows. length; I ++)
{
VaR name = Ds. Tables [0]. Rows [I]. authorname;
VaR id = Ds. Tables [0]. Rows [I]. authorno;
Document. All ("ddl_sitemno"). Options. Add (New Option (name, ID ));
}
}
}
Return
}
</SCRIPT>

5. Refer to the above Code and try again.

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.