In. net, the general processing program (ashx) is used in Ajax -- Dynamic Level connection of the drop-down list

Source: Internet
Author: User

In the. NET Framework, a file type is generally used to process files (. ashx ). It can be used as a server in Ajax development. In particular, when a request stays on a page, the following example is used to implement cascading update in the HTML drop-down list.

(1) ddlinnerjoin. aspx code:

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "ddlinnerjoin. aspx. cs" inherits = "ddlinnerjoin" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
<SCRIPT type = "text/JavaScript" src = "ddlinnerjoin. js"> </SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Select id = "Major" onchange = "startrequest ();">
<Option value = "1"> Software Technology </option>
<Option value = "2"> network technology </option>
</SELECT>
<Select id = "class">
</SELECT>
</Div>
</Form>
</Body>
</Html>
(2) ddlinnerjoin. js code:

/* Creator: haohai
Created on: 2007-6-4
Version: 1.0
*/
VaR XMLHTTP;
VaR requesttype = "";
Function createxmlhttp ()
{
If (window. XMLHttpRequest)
{
XMLHTTP = new XMLHttpRequest (); // Mozilla Browser
}
Else if (window. activexobject)
{
Try
{
XMLHTTP = new activexobject ("msxml2.xmlhttp"); // old version of IE
}
Catch (E)
{
}
Try
{
XMLHTTP = new activexobject ("Microsoft. XMLHTTP"); // new version of IE
}
Catch (E)
{
}
If (! XMLHTTP)
{
Window. Alert ("the XMLHTTPRequest object cannot be created! ");
Return false;
}
}
}
Function startrequest ()
{
Createxmlhttp ();
// Use the get Method
VaR url = "ddlinnerjoin. ashx? Major = "+ document. getelementbyid (" Major "). Options. value;
XMLHTTP. Open ("get", URL, true );
XMLHTTP. onreadystatechange = bindclass;
XMLHTTP. Send (null );

}
Function bindclass ()
{
If (XMLHTTP. readystate = 4)
{
If (XMLHTTP. Status = 200)
{
VaR selclass = Document. getelementbyid ("class ");
// If the passive (triggered) drop-down has content, clear the content first.
While (selclass. haschildnodes ())
{
VaR node = selclass. Children (0)
Selclass. removechild (node );
}
// Obtain the response content
VaR result = XMLHTTP. responsetext;
// Split to facilitate binding
VaR optiontext = result. Split ('');
// Bind the split content to the passive drop-down list
For (VAR I = 0; I <optiontext. length; I ++)
{
VaR optionnode = Document. createelement ("option ");
Optionnode. Text = optiontext [I];
Selclass. Add (optionnode );
}
}
}
}

(3) ddlinnerjoin. ashx code:

<% @ Webhandler Language = "C #" class = "ddlinnerjoin" %>
// Creator: haohai
// Creation date: 2007-6-4
// Version 1.0
Using system;
Using system. Web;
// Introduce the namespace to access the database
Using system. Data. sqlclient;
Using system. Data;
Public class ddlinnerjoin: ihttphandler {

Public void processrequest (httpcontext context ){
Context. response. contenttype = "text/plain ";
String majorid = context. Request. Params ["Major"]. tostring (). Trim ();
String major = "";
// Avoid not recognizing Chinese Characters
If (majorid = "1 ")
{
Major = "software technology ";
}
Else if (majorid = "2 ")
{
Major = "Network Technology ";
}
// Extract data from the database
Sqlconnection conn = new sqlconnection ("Server =.; database = tuition; uid = sa; Pwd = sa ;");
Sqldataadapter da = new sqldataadapter ("select classname from dictblclass where classname like '" + major + "%'", Conn );
Dataset DS = new dataset ();
Da. Fill (DS );
// Define the response text format to return
String result = "";
Foreach (datarow row in DS. Tables [0]. Rows)
{
Result + = row [0]. tostring (). Trim () + "";
}
Context. response. Write (result. Trim ());
// Context. response. Write ("Hello World ");
}
 
Public bool isreusable {
Get {
Return false;
}
}
}

In this way, you can easily implement cascade updates, especially when the data volume is large.

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.