后台获取前台数据的方法-Form表单+ajax
Tips: 1、前台可以直接利用Html标签的id 来获取标签的属性值
2、css可以利用 id 和 class 名来改标签的样式
3、后台不能直接通过div的id和class来获取前台的Html标签的值,只能通过From表单来存需要的html(包括div标签属性、内容等)
4、通过ajax前后台数据交互,ajax通过data把前台页面数据传给后台,后台(一般处理程序.ashx)获取前台data的数据,并return,成功后ajax执行success中的内容
var form = ("表单的name");
form.ajaxSubmit{..........}
5、后台(.ashx)获取前台数据的时候只能通过name属性来取数据
HttpContext.Current.Request.Form["写想要的form中存的html中想要的文本域的 name的值 "] //获取 name="值" 的文本域的value
HttpContext.Current.Request[" 前台ajax中data存的键名"] //获取ajax的data中所存键名对应的键值
如:ajax {.....
data: { "ModuleType": "AddDFinance", "EnterpriseId": $("#hidEnterpriseid").val() },
.......}
var temp = HttpContext.Current.Request[" ModuleType"] //temp的值为AddDFinance
例子:此处通过一个隐藏的from来接收html ,再通过ajax把from.html()传递给后台
1、前台Html(写在.aspx中)
<head>
<script src="../js/PageAjax.js" type="text/javascript"></script>
</head>
<body>
<div id="DFinance" style="display: none;" class="DFinance"> <div> <table id="TFinance"> <tr> <td class="rtdheader" style="border: 0px; border-bottom: 1px solid blue;"> <input type="text" id="YearToFinance" name="txtYearToFinance"/> </td> </tr> </table> </div> </div>
<a class='btn' id="confirm" onclick='confirmMessage(this)' style="margin-left: 30px"> 确定</a>
<--此处替换下面的from表单内容-->
</body>
from表单,用来接收上面的html
<form action="" id="frmForSubmitPart" enctype="multipart/form-data" method="post" style="display: none"></form>
2、PageAjax.js文件 (一点击(onclick)"确定",就执行这个function)
function confirmMessage(obj) { var form = $("#frmForSubmitPart"); //前台可以通过id获取form,后台只能通过name取from的值 var submitHtml = ""; form.html(""); submitHtml = $("#DFinance").formhtml(); form.append(submitHtml); if ($(obj).html() == "确定") { form.ajaxSubmit({ url: "Handel/SaveModuleToDatabase.ashx", global: false, dataType: "json", data: { "ModuleType": "AddDFinance", "EnterpriseId": $("#hidEnterpriseid").val() }, success: function (data) { var thisTxtYear = $("#DFinance").find("#txtYearToFinance").val();
alert("成功!"); }
}
3、后台处理程序(SaveModuleToDatabase.ashx)
var finance = HttpContext.Current.Request.Form["txtYearToFinance"]; //( 表单中存了div input table) 接收表单中存的name="txtYearToFinance"的控件的value
using System; using System.Web; using System.Collections.Generic; using System.Collections; using System.IO; using BLL; using Model; using System.Text.RegularExpressions; public class SaveModuleToDatabase : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string moduleId = context.Request["ModuleType"].ToString();
switch (moduleId) { case "AddDFinance": HttpContext.Current.Response.Write(Save()); break;
}
}
private string Save() { FinanceHeaderBLL efhBll = new FinanceHeaderBLL(); FinanceHeaderModel efhmodel = new FinanceHeaderModel(); var enterpriseId = ""; if (!string.IsNullOrEmpty(HttpContext.Current.Request["EnterpriseId"])) { enterpriseId = HttpContext.Current.Request["EnterpriseId"]; }
var finance = HttpContext.Current.Request.Form["txtYearToFinance"];
efhmodel.EnterpriseID = enterpriseId;
efhmodel.finance= finance;
}
FinanceHeaderBLL efhBll = new FinanceHeaderBLL(); // 业务逻辑层
FinanceHeaderModel efhmodel = new FinanceHeaderModel();// 数据层,写在Model类库中