.net中传值的几种方法
前面一段时间一直在做winform的程序,是关于PPT的二次开放,最近项目放下了,在等新的需求,突然有人问我WEB上几种传值的方法,晕死,居然没说全,郁闷,经验太少了,诶
所以今天把这些东西记下来
1.使用QueryString变量
+--------传递值
private void Button1_Click(object sender, System.EventArgs e)
{
string s_url;
s_url = "xxxx.aspx?name=" + Label1.Text;
Response.Redirect(s_url);
}
+--------接受值
private void Page_Load(object sender, EventArgs e)
{
Label2.Text = Request.QueryString["name"];
}
==========================================
2.使用Application 对象变量
+--------传递值
private void Button1_Click(object sender, System.EventArgs e)
{
Application["name"] = Label1.Text;
Server.Transfer("xxxx.aspx");
}
+--------接受值
private void Page_Load(object sender, EventArgs e)
{
string name;
Application.Lock();
name = Application["name"].ToString();
Application.UnLock();
}
===========================================
3.使用Session变量
+--------传递值
private void Button1_Click(object sender, System.EventArgs e)
{
Session["name"] = Label.Text;
}
+--------接受值
private void Page_Load(object sender, EventArgs e)
{
string name;
name = Session["name"].ToString();
}
============================================
4.使用Cookie对象变量
+--------传递值
private void Button1_Click(object sender, System.EventArgs e)
{
HttpCookie cookie_name = new HttpCookie("name");
cookie_name.Value = Label1.Text;
Reponse.AppendCookie(cookie_name);
Server.Transfer("xxx.aspx");
}
+--------接受值
private void Page_Load(object sender, EventArgs e)
{
string name;
name = Request.Cookie["name"].Value.ToString();
}
=========================================
5.使用Server.Transfer方法
+--------传递值
public string Name
{
get{ return Label1.Text;}
}
private void Button1_Click(object sender, System.EventArgs e)
{
Server.Transfer("xxxx.aspx");
}
+--------接受值
private void Page_Load(object sender, EventArgs e)
{
Passvalues newWeb; //实例传递值窗体
newWeb = (source)Context.Handler;
string name;
name = newWeb.Name;
}