Using Ajax in ASP.net to realize the DropDownList of continuous motion

Source: Internet
Author: User
Ajax|asp.net

The following code can be used absolutely, or you can implement multilevel menus ....

<script language= "JavaScript" >
function Load (state)
{
var drp2 = document.getElementById ("DropDownList2");
for (Var i=0;i<=drp2.options.length-1;i++)
{
Drp2.remove (i);
}
var ohttpreq = new ActiveXObject ("MSXML2.    XMLHTTP "); Send Request
var odoc = new ActiveXObject ("MSXML2.    DOMDocument "); Response results
var State=document.getelementbyid ("DropDownList1"). Value;
Ohttpreq.open ("Post", "Webform2.aspx?state=" +state, false);
Ohttpreq.send ("");
result = Ohttpreq.responsetext;
Odoc.loadxml (result);

Items = Odoc.selectnodes ("//city/table");
Items = Odoc.selectnodes ("//address/table");
for (var item = Items.nextnode (); item;item = Items.nextnode ())
{
var city = Item.selectsinglenode ("//address"). nodeTypedValue;
var newoption = document.createelement ("option");
Newoption.text = City;
Newoption.value = City;
Drp2.options.add (newoption);
}
}
</script>

============================

WebForm1.aspx

private void Page_Load (object sender, System.EventArgs e)
  {
  // Place user code here to initialize page
   if (! IsPostBack)
   {
    sqlconnection con = new SqlConnection ("Packet Size=4096;user Id=sa ;d ata source=server;persist Security info=false;initial catalog=pubs ");
    SqlDataAdapter da = new SqlDataAdapter ("Select state from authors group by state", con);
    DataSet ds = new DataSet ("state");
    da. Fill (DS);
    this. Dropdownlist1.datasource = ds. Tables[0];
    this. Dropdownlist1.datatextfield = "state";
    this. Dropdownlist1.datavaluefield = "state";
    this.  Dropdownlist1.databind ();
    this.  DROPDOWNLIST1.ATTRIBUTES.ADD ("onchange", "Load ()");
   //dropdownlist1.
  }
 
 }

Webform2.aspx

private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
if (this. request["State"]!=null)
{
String state = this. request["State"]. ToString ();
SqlConnection con = new SqlConnection ("Packet size=4096;user id=sa;data source=server;persist security info=false; Initial catalog=pubs ");
SqlDataAdapter da = new SqlDataAdapter ("Select address from authors where State = '" +state+ "", con);
DataSet ds = new DataSet ("Address");
Da. Fill (DS);
XmlTextWriter writer = new XmlTextWriter (Response.outputstream, response.contentencoding);
Writer. formatting = formatting.indented;
Writer. indentation = 4;
Writer. IndentChar = ';
Ds. WriteXml (writer);
Writer. Flush ();
Response.End ();
Writer. Close ();
}
}



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.