1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="WebApplication1.Home" %> 2 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <title></title> 8 <script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script> 9 <style type="text/css"> 10 .edit_row 11 { 12 width:100%; height:20px;margin-bottom:5px; 13 } 14 .edit_row label 15 { 16 width:100px; text-align:right; display:inline-block; 17 } 18 </style> 19 <script type="text/javascript"> 20 $(document).ready(function () { 21 $("#btnSave").click(function () { 22 $.ajax({ 23 url:"Home.aspx/Save", 24 type:"POST", 25 dataType: "Json", 26 contentType: "application/json; charset=utf-8", 27 success: function (data) { 28 alert("skskskok"); 29 } 30 }) 31 }); 32 }) 33 </script> 34 </head> 35 <body> 36 <form id="form1" runat="server"> 37 <div> 38 <div class="edit_row"> 39 <label>姓:</label> 40 <input type="text" id="" name="" /> 41 </div> 42 <div class="edit_row"> 43 <label>名字:</label> 44 <input type="text" id="Text1" name="" /> 45 </div> 46 <div class="edit_row"> 47 <label>头衔:</label> 48 <input type="radio" value="Vice President, Sales" name="Title" />Vice President, Sales 49 <input type="radio" value="Sales Representative" name="Title" />Sales Representative 50 <input type="radio" value="Sales Manager" name="Title" />Sales Manager 51 <input type="radio" value="Inside Sales Coordinator" name="Title" />Inside Sales Coordinator 52 </div> 53 <div class="edit_row"> 54 <label>称呼:</label> 55 <select id="TitleOfCourtesy" name="TitleOfCourtesy"> 56 <option value="">--请选择--</option> 57 <option value="Ms.">Ms.</option> 58 <option value="Mrs.">Mrs</option> 59 <option value="Mr.">Mr.</option> 60 <option value="Dr.">Dr.</option> 61 </select> 62 </div> 63 <div class="edit_row"> 64 <label>生日:</label> 65 <input type="text" id="Text4" name="" /> 66 </div> 67 <div class="edit_row"> 68 <label>雇佣日期:</label> 69 <input type="text" id="Text5" name="" /> 70 </div> 71 <div class="edit_row"> 72 <label>家庭地址:</label> 73 <input type="text" id="Text6" name="" style="width:300px;" /> 74 </div> 75 76 <div class="edit_row"> 77 <label>国家:</label> 78 <input type="text" id="Text10" name="" /> 79 </div> 80 <div class="edit_row"> 81 <label>地区:</label> 82 <input type="text" id="Text8" name="" /> 83 </div> 84 <div class="edit_row"> 85 <label>城市:</label> 86 <input type="text" id="Text7" name="" /> 87 </div> 88 <div class="edit_row"> 89 <label>邮政编码:</label> 90 <input type="text" id="Text9" name="" /> 91 </div> 92 93 <div class="edit_row"> 94 <label>家庭电话:</label> 95 <input type="text" id="Text11" name="" /> 96 </div> 97 <div class="edit_row"> 98 <label>分机号:</label> 99 <input type="text" id="Text12" name="" /> 100 </div> 101 <div class="edit_row"> 102 <label>相片:</label> 103 <input type="text" id="Text13" name="" /> 104 </div> 105 <div class="edit_row"> 106 <label>爱好:</label> 107 <input type="checkbox" id="" name="hobby" value="游泳" />游泳 108 <input type="checkbox" id="" name="hobby" value="看书" />看书 109 <input type="checkbox" id="" name="hobby" value="睡觉" />睡觉 110 <input type="checkbox" id="" name="hobby" value="上网" />上网 111 <input type="checkbox" id="" name="hobby" value="看电影" />看电影 112 </div> 113 <div style="margin-bottom:5px;width:100%;height:40px;"> 114 <label style="width:100px;height:40px;text-align:right; vertical-align:top; display:inline-block">说明:</label> 115 <textarea style="width:300px;height:40px; overflow-y:auto; margin:0px; padding:0px;"></textarea> 116 </div> 117 <div class="edit_row"> 118 <label>相片路径:</label> 119 <input type="text" id="Text15" name="" /> 120 </div> 121 <div class="edit_row"> 122 <input type="button" id="btnSave" value="提交" /> 123 </div> 124 </div> 125 </form> 126 </body> 127 </html>
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.Web.Services; 8 9 namespace WebApplication1 10 { 11 public partial class Home : System.Web.UI.Page 12 { 13 protected void Page_Load(object sender, EventArgs e) 14 { 15 if (!IsPostBack) 16 { 17 18 } 19 } 20 [WebMethod] 21 public static string Save() 22 { 23 //我是想在这里获得前台传过来的数据
//string firstName= Request.Form["FirstName"];
//string firstName= Request.Form["LastName"];
//当是这里是静态方法就是获取不到Request.Form这里的值 24 return ""; 25 } 26 } 27 }
看我上面标红色的
我是想在
[WebMethod]
public static string Save()
{
return "";
}
把数据写到数据库里
$.ajax({ 23 url:"Home.aspx/Save", 24 type:"POST", 25 dataType: "Json", 26 contentType: "application/json; charset=utf-8", 27 success: function (data) { 28 alert("skskskok"); 29 } 30 })
我没看到这个AJAX有把数据传到后台,你在后台想接受到数据,至少前台要发过去吧?
传过来的FirstName LastName应该作为Save方法的参数 Save(string FirstName,String LastName)
ajax只是数据提交的方式