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.