1. 程式人生 > >.NET中一般處理程式(ashx)在Ajax中的使用

.NET中一般處理程式(ashx)在Ajax中的使用

NET框架中有一個檔案型別是一般處理檔案(.ashx)。可以在ajax開發中作為伺服器端使用。特別是當在請求停留在一個頁面的時候,下面舉個例子--實現html下拉列表的級連更新。

(1)ddlInnerJoin.aspx程式碼:

<head runat="server">     <title>無標題頁</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">軟體技術</option>             <option value ="2" >網路技術</option>          </select>         <select id="class">          </select>     </div>     </form> </body>

(2)ddlInnerJoin.js程式碼:

var xmlHttp; var requesttype="";     function createXmlHttp()     {     if(window.XMLHttpRequest)     {         xmlHttp=new XMLHttpRequest();//mozilla瀏覽器     }     else if(window.ActiveXObject)     {         try         {             xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");//IE舊版本         }         catch(e)         {         }         try         {             xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");//IE新版本         }         catch(e)         {         }         if(!xmlHttp)         {             window.alert("不能建立XMLHTTPREQUEST物件!");             return false;         }     } } function startRequest() {     createXmlHttp();     //使用GET方式     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");               //如果被動(被激發的)下拉有內容,要首先清空               while(selclass.hasChildNodes())               {                  var node=selclass.children(0)                  selclass.removeChild(node);               }                //獲取響應內容                        var result=xmlHttp.responseText;               //分割以方便繫結               var optiontext=result.split(' ');               //將分割後的內容繫結到被動下拉列表               for(var i=0;i<optiontext.length;i++)               {                     var optionnode=document.createElement("OPTION");                     optionnode.text=optiontext[i];                     selclass.add(optionnode);               }         }     } }

(3)ddlInnerJoin.ashx程式碼:

<%@ WebHandler Language="C#" Class="ddlInnerJoin" %> using System; using System.Web; //由於要訪問資料庫,引入名稱空間 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 = "";         //避免不識別漢字         if (majorid == "1")         {             major = "軟體技術";         }         else if (majorid == "2")         {             major = "網路技術";         }         //從資料庫提取資料         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);         //定義響應文字的格式,以返回         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;         }     } }