webform中,前端如何送数据到后端? 或者后端如何传值到前端?或者后端如何传值到另外一个后端
1 ajax把json字符串传送到后台,也可以有后端传值给前端(返回值就是)
1.1 通过webservice
前台:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "WebService1.asmx/HelloWorld2",
data: "{name:'xiaoxiao'}",
dataType: 'json',
success: function (result) {
alert(result.d);
}
});
后台:
WebService1.asmx
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace asp.net
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld2(string name)
{
return "Hello World" + name + System.DateTime.Now.ToLongTimeString();
}
}
}
1.2
前台:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "aspxpage.aspx/SayHello2",
data: "{name:'xiaoxiao'}",
dataType: 'json',
success: function (result) {
alert(result.d);
}
});
后端:
[WebMethod]
public static string SayHello2(string name)
{
return "Hello"+name;
}
1.3 前台:
跟上面差不多,
但是后台不是使用return方式,
是用 base.Response.Write(s);
base.Response.End();
2 前端 <form id="forPost" method="post"> 还有其他标签 </form>
forPost.action="DestinationPage.aspx";
forPost.submit();
后端也是通过form获取里面的标签值,比如string a = Request.Form["其他标签id"].ToString();
3 后端的url传参给另外一个后台
string url = "DestinationPage5.aspx?parameter1=" + aa + "¶meter2=" + bb;
Response.Redirect(url, false);
另外一个后台: txt1.Value = Request.QueryString["parameter1"].ToString();
txt2.Value = Request.QueryString["parameter2"].ToString();
4 链接传参给后端
<a href="DestinationPage6.aspx?param1=1111¶m2=2222">跳转</a>
后台:
txt1.Value = Request["param1"];
txt2.Value = Request["param2"];
5 后端如何传值到另外一个后端
Context.Items["value"] = txt1.Value;
Server.Transfer("DestinationPage3.aspx");
另一个后端 string a = Context.Items["value"].ToString();
6 后端如何传值到另外一个后端
假设后端有一个值 public string Value
{
get { return txt1.Value; }
}
然后跳去别的页面
Server.Transfer("DestinationPage4.aspx");
另外一个后端:
private Transfer_SourcePage4 sourcePage;
sourcePage = (Transfer_SourcePage4)Context.Handler;
string aa = sourcePage.Value;
7 后端如何传值到另外一个后端
string aa = txt1.Value;
HttpCookie cookie = new HttpCookie("MyCookie", aa);
Response.Cookies.Add(cookie);
string url = "DestinationPage8.aspx";
Response.Redirect(url, false);
另外一个后端
HttpCookie myCookie = Request.Cookies["MyCookie"];
txt1.Value = myCookie.Value;
8 后端如何传值到另外一个后端
Session["value"] = txt1.Value;
Response.Redirect("DestinationPage2.aspx");
另外一个后端
txt1.Value = Session["value"].ToString();
9 后端如何传值到另外一个后端
string aa = txt1.Value;
Application["param1"] = aa;
string url = "DestinationPage7.aspx";
Response.Redirect(url, false);
另外一个后端
txt1.Value = Application["param1"].ToString();