Ajax implements two-level drop-down box interaction applications

Source: Internet
Author: User
<Asp: DropDownList onChange = "GetChange (this);" id = "drp1" runat = "server"> </asp: DropDownList>
<Asp: DropDownList id = "drp2" runat = "server"> </asp: DropDownList>

Js Script: return data
Function GetChange (drp)
{
Var num = drp. options [drp. selectedIndex]. value;
WebForm3.GetDrp (num, drp_CallBack );
}
Function drp_CallBack (response)
{
If (response. error! = Null)
{
Alert (response. error );
Return;
}
Var drp = document. getElementById ("<% = drp2.ClientID %> ");
VaR DATA = response. value;
If (Data = NULL | typeof (data )! = 'Object ')
{
Return;
}
DRP. Options. Length = 0;
For (I = 0; I <data. length; I ++)
{
Var o = data [I];
Drp. options [drp. options. length] = new Option (data [I], data [I]);
}
}

Background code implementation
Private void Page_Load (object sender, System. EventArgs e)
{
// Place user code here to initialize the page
Ajax. Utility. RegisterTypeForAjax (typeof (ZKMarketing. WebForm3 ));
If (! Page. IsPostBack)
{
For (int I = 5; I <11; I ++)
Drp1.Items. Add (new ListItem (I. ToString (), I. ToString ()));
Drp2.Items. Add (new ListItem ("select", "-1 "));
}
}
[Ajax. AjaxMethod ()]
Public ArrayList GetDrp (int num)
{
ArrayList al = new ArrayList (num );
Al. Clear ();
For (int I = 0; I <num; I ++)
{
ListItem li = new ListItem (I. ToString (), I. ToString ());
Al. Add (li );
}
Return al;
}

You also need to set the web. Config file,
<HttpHandlers>
<Add verb = "POST, GET" path = "ajax/*. ashx" type = "Ajax. PageHandlerFactory, Ajax"/>
</HttpHandlers>

The above is a test page when I was working on a specific project. because it was the first time, I wrote such a simple test page to implement AJAX, now, the functions of a specific project are basically completed.

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.