目前来说,使用JSON保存数据比较方便,前台可以不用Test.aspx 页面,可以直接用Html页面,使用.aspx页面的弊端就不在这里熬述。
具体步骤如下:
1、新建一个Html页面,命名为Test.html
<script type="text/javascript" src="easyui/jquery.min.js"></script>
<script type="text/javascript" src="easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="easyui/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="js/save.js"></script>
<link rel="stylesheet" type="text/css" href="easyui/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="easyui/themes/icon.css" /> <div>
<input type="text" id="txtFileNames" class="textbox" name="Vassets" style="width:110px;"/>
<a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="objV.saves()">保存</a>
</div>
2、新建一个JS页面,命名为save.js
objV = {
saves:function(){
$.ajax({
type: "POST",
url: "Test.ashx",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(GetJsonData()),
dataType: "json",
success: function (message) {
if (message > 0) {
alert("请求已提交!我们会尽快与您取得联系");
}
},
error: function (message) {
$("#request-process-patent").html("提交数据失败!");
}
}); } }; function GetJsonData() {
var json = {
"FTName": $("#txtFileNames").val()
};
return json;
}
3、新建一个AddTest.ashx页面
int num = ;
context.Response.ContentType = "application/json";
var data = context.Request;
var sr = new StreamReader(data.InputStream);
var stream = sr.ReadToEnd();
var javaScriptSerializer = new JavaScriptSerializer();
var PostedData = javaScriptSerializer.Deserialize<Model.FileType>(stream);
DataAccess<Model.FileType> da = new DataAccess<Model.FileType>(); try
{
num = da.Add(PostedData, "FileType");
}
catch (Exception msg)
{
context.Response.Write(msg.Message);
} context.Response.ContentType = "text/plain";
context.Response.Write(num);