日期:2014-05-17 浏览次数:20406 次
<script type="text/javascript"> function AddUser() { var userName = $("#txtUserName").val(); var Email = $("#txtEmail").val(); var dpost = "{action:\"AddUser\",UserName:\"" + userName + "\",Email:\"" + Email + "\"}"; alert(dpost); $.ajax({ url: "Ajax/UserAjax.aspx", data: dpost, dataType: "JSON", success: function (result) { alert(result); }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert(XMLHttpRequest.status); alert(XMLHttpRequest.readyState); alert(textStatus); } }); } </script> <input type="text" id="txtUserName" /><br /> <input type="text" id="txtEmail" /><br /> <asp:Button ID="btnAdd" runat='server' Text="添加" OnClientClick="AddUser();return false;" />
protected void Page_Load(object sender, EventArgs e) { if (Request["action"] != null) { string action = Request.Form["action"].ToString(); if (action == "AddUser") { try { string userName = Request.Form["UserName"]; string email = Request.Form["Email"]; DataSet dsUser = Application["dsUser"] as DataSet; DataRow drUser = dsUser.Tables[0].NewRow(); drUser["userName"] = userName; drUser["Email"] = email; dsUser.Tables[0].Rows.Add(drUser); Application["dsUser"] = dsUser; Response.Write("ok"); } catch { Response.Write("Add Error"); } } } }