您的位置:首页 > 编程语言 > ASP

Asp.net页面间通过Post方式传递参数

2015-08-05 16:58 766 查看

1、通过页面间传递参数有get,post,cache,session,文件等级。这里讲的是通过Post方式传递参数,好处是页面间传递的参数不直接在url中给出,提高了安全性,当然通过第三方软件

还是可以拦截。

2、应用场景,如我们调用第三方支付接口,他们通过Request方式获取值,参数众多,如果在url中给出那会比较麻烦。

3、实现方法:

步骤1

js方法,用于动态创建form,同时需引用jquery类库

<span style="font-size:18px;">function PostForm(id, action, config) {
var form1 = $("<form id='" + id + "'></form>");
form1.attr('action', action);
form1.attr('method', 'post');
$.each(config, function (index, item) {
var input1 = $("<input type='hidden' id='" +
item.id + "' name='" + item.id + "' />");
input1.attr('value', item.value);
form1.append(input1);
});
form1.appendTo("body");
form1.css('display', 'none');
form1.submit();
}
</span>


步骤2

Buy.aspx,即调用页面,在调用方法中增加方

<span style="font-size:18px;">	protected void lbtnSave_Click(object sender, EventArgs e)
{
List<JsonItem> list = new List<JsonItem>() { new JsonItem("tid", "11220044") };
string json = JsonConvert.SerializeObject(list);
eval("$(function(){$.PostForm('form3','/CreateOrder.aspx', " + json + ");})");
}

public void eval(string js)
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), new Guid().ToString("N"), js, true);
}</span>


步骤3

CreateOrder.aspx,获取参数页面

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

if (Request["tid"] != null){

//处理事务。。

}

}

}

步骤4:创建JsonItem.cs类

<span style="font-size:18px;"> [Serializable]
public class JsonItem
{
public JsonItem(string _id, string _value)
{
this.id = _id;
this.value = _value;
}
public string id { set; get; }
public string value { set; get; }
}</span>


欢迎加入群探讨问题:26055929
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: