HTML表单通过 AJAX 向.NET提交数据(包含下拉框、单选框)

Calchas 发表于 2009, July 16, 9:12 AM

X.ASPX

 

XML/HTML代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml" lang="UTF-8">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  5. <title>AJAX</title>  
  6. <script language="javascript" type="text/javascript">    
  7. function $(objName){   
  8.     if(document.getElementById){   
  9.         return eval('document.getElementById("' + objName + '")');   
  10.     }else if(document.layers){   
  11.         return eval("document.layers['" + objName +"']");   
  12.     }else{   
  13.         return eval('document.all.' + objName);   
  14.     }   
  15. }   
  16. var xmlHttp = false;    
  17. try {    
  18.     xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");    
  19. }    
  20. catch (e)    
  21. {    
  22.     try {    
  23.         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");    
  24.     }    
  25.     catch (e2)   
  26.     {    
  27.         xmlHttp = false;    
  28.     }    
  29. }    
  30. if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {    
  31.     xmlHttp = new XMLHttpRequest();    
  32. }    
  33. function SendData(){   
  34.     var myxml = "<?xml version=\"1.0\"?><CollegeApply><Personal>";   
  35.     myxml += "<loginedusername>"+ $("loginedusername").value +"</loginedusername>";   
  36.     myxml += "<howtoknow>"+ $("txthowtoknow").options[$("txthowtoknow").selectedIndex].value +"</howtoknow>";      
  37.     myxml += "<otherway>"+GetRadioValue("txtotherway")+"</otherway>";   
  38.     myxml += "</Personal></CollegeApply>";   
  39.     xmlHttp.open("POST","/WebHandler/x.ashx?type=personal",false);   
  40.     xmlHttp.onreadystatechange = function() {    
  41.     if (xmlHttp.readyState < 4) {    
  42.         cname.innerHTML="loading...";    
  43.     }    
  44.     if (xmlHttp.readyState == 4) {    
  45.         var response = xmlHttp.responseText;    
  46.         cname.innerHTML = response;    
  47.     }   
  48.     }   
  49.     xmlHttp.send(myxml);       
  50.     //alert(xmlHttp.responseText);   
  51.     return false;   
  52. }   
  53. function GetRadioValue(RadioName){      
  54.     var obj;   
  55.     obj=document.getElementsByName(RadioName);      
  56.     if(obj!=null){   
  57.         var i;   
  58.         for(i=0;i<obj.length;i++){   
  59.             if(obj[i].checked){   
  60.                 return obj[i].value;   
  61.             }   
  62.         }   
  63.     }   
  64.     return null;   
  65. }    
  66. </script>  
  67. </head>  
  68. <body>  
  69. <form name="form1" action="#" method="post">  
  70. <ul>       
  71. <li>当前登陆用户<input type="hidden" id="loginedusername" value="admin" /></li>  
  72. <li class="right"><select id="txthowtoknow">  
  73. <option value="谷歌(Google)" selected="selected">谷歌(Google)</option>  
  74. <option value="雅虎(Yahoo)">雅虎(Yahoo)</option>  
  75. <option value="百度(Baidu)">百度(Baidu)</option>  
  76. <option value="搜狐">搜狐</option>  
  77. <option value="网络新闻">网络新闻</option>  
  78. <option value="论坛帖子">论坛帖子</option>  
  79. <option value="其他网站的链接" >其他网站的链接</option>  
  80. <option value="朋友介绍">朋友介绍</option>  
  81. </select></li>  
  82. <li class="right">你有没有曾经使用别名或者旧护照进入新加坡?   
  83. <input type="radio" id="txtotherway1" name="txtotherway" value="有"  />有   
  84. <input type="radio" id="txtotherway2" name="txtotherway" value="没有" checked />没有   
  85. </li>  
  86. <li><input type="submit" id="btnsubmit" value="提交" onclick="return SendData();"></li>  
  87. </ul>  
  88. </form>  
  89. </body>  
  90. </html>  

 

x.ashx

 

C#代码
  1. <%@ WebHandler Language="C#" Class="CollegeApply" %>   
  2.   
  3. using System;   
  4. using System.Web;   
  5. using System.Xml;   
  6. using System.IO;   
  7. using System.Text;   
  8.   
  9. public class CollegeApply : IHttpHandler {       
  10.     public void ProcessRequest (HttpContext context) {   
  11.         try  
  12.         {   
  13.             context.Response.ContentType = "text/plain";   
  14.             string RequestType = context.Request.QueryString["type"].ToString();   
  15.             if (RequestType == "personal")   
  16.             {   
  17.                 Encoding encode = Encoding.GetEncoding("utf-8");   
  18.                 StreamReader sr = new StreamReader(context.Request.InputStream, encode);   
  19.   
  20.                 Char[] read = new Char[1024];   
  21.                 int count = sr.Read(read, 0, 1024);   
  22.                 String str = null;   
  23.                 String sXML = null;   
  24.                 sXML = "";   
  25.   
  26.                 while (count > 0)   
  27.                 {   
  28.                     str = new String(read, 0, count);   
  29.                     sXML = sXML + str;   
  30.                     count = sr.Read(read, 0, 1024);   
  31.                 }   
  32.   
  33.                 XmlDocument document = new XmlDocument();   
  34.                 document.LoadXml(sXML);   
  35.                 XmlNode rootNode = document.DocumentElement;   
  36.                 string loginedusername = "";   
  37.                 try  
  38.                 {   
  39.                     loginedusername = rootNode.FirstChild.SelectNodes("loginedusername").Item(0).InnerText.ToString();   
  40.                     string howtoknow = rootNode.FirstChild.SelectNodes("howtoknow").Item(0).InnerText.ToString();   
  41.                     string otherway = rootNode.FirstChild.SelectNodes("otherway").Item(0).InnerText.ToString();   
  42.   
  43.                     //此处忽略执行SQL语句,直接输出   
  44.                     context.Response.Write("已经更新资料;用户名:" + loginedusername + " 如何知道我们的:"  + howtoknow + " 其他途径:" + otherway );   
  45.                 }   
  46.                 catch  
  47.                 {   
  48.                 }   
  49.             }   
  50.         }   
  51.         catch  
  52.         {   
  53.         }   
  54.     }    
  55.     public bool IsReusable {   
  56.         get {   
  57.             return false;   
  58.         }   
  59.     }   
  60. }  

« 上一篇 | 下一篇 »

标签: html, ajax, asp.net
相关文章: (最多只显示5条记)
引用: 点击获得Trackback地址,Encode: UTF-8 点击获得Trackback地址,Encode: GB2312 or GBK 点击获得Trackback地址,Encode: BIG5
发表评论:( 你的参与是我最大的动力! )