页面加参数传递

来源:互联网 发布:修复windows图片查看器 编辑:程序博客网 时间:2024/04/28 19:16

 ------------------ Windows Phones 7手机开发、.Net培训、期待与您交流! ----------------------

 本文章参考其他博客然后自己测试过部分而写的

1 使用QueryString
2
使用Session变量
3
使用ServerTransfer
4
使用类的静态属性
5
使用Application全局变量
6
使用隐藏域

1 使用QueryString:
  
描述使用URL传值
  
优点:占用资源低
  
缺点:传递类型单一;安全性差
  
例子:

Private void Button1_ Click(object senderSystemEventArgs e)
{
//定义一个字符串,此字符串中包含源页面向目标页面传送的数据
     string strUrl;
//从源页面中获得传送的数据
     strUrl=”WebForm2.aspx? userid = ” + TextBox1.Text+ ”username=”+TextBox2.Text;
//向目标页面传送数据
     Response.Redirect(strUr1);
)

private void Page_ Ioad(object senderSystemEventArgs e)

{
//使用QueryString从源页面中接受useridusername变量数据,并通过Label1、Label2显示

    Label1.Text  = RequestQueryString["userid"]
    Label2.Text  =  Request
QueryString["username"];
)

使用Session变量
描述:
服务器端控件可以保存各种类型变量
优点:可以传递任何类型的变量,安全性高
缺点:占用系统资源多

private void Button1_Click(object senderSystemEventArgs c)
{
//创建Session变量,用以存放TextBoxlTextBox2组件中的数据
    Session[“userid”] =  TextBox1.Text
    Session[“username”] = TextBox2.Text

//向目标页面传送数据
    Server.Transfer(“WebForm2aspx”)
}

private void Page_ Ioad(object senderSystemEventArgs e)
{
//从源页面中接受数据并显示出来
    Labe1Text=SessionE“userid”]ToString()
    Labe2
Text-Session[“usernflme”]ToString()
//清除创建的Session变量
   SessionRemove(“userid”);
   Session
Remove(“username”)
)

3 使用ServerTransfer
描述:以页面对象属性的方式传递信息到另一个页面。
优点:不占用内存,适合传递大容量的数据。
缺点:只能在一个服务器传递数据。

public class WehFormlSystem.Web.UI.Page
{
      
protected System.Web.UI.WebControls.TextBox TextBoxl
      
protected System.Web.UI.WebControls.TextBox FextBox2
      
protected System.Web.UI.WebControls.Button Button1

private void Page_Ioad(object senderSystem.EventArgs e)
{
)
public string userid
(
    
get
     
{
        
return TextBox1Text
     }
}
public string username
{
     
get
    
{
      
return TextBox2Text
    }
}
private void Button1_Click(object senderSystem.EventArgs e)
{
    Server
Transfer(”WebForm2.aspx”,true);
}
}


public class WebForm2 SystemWebUIPage
{
private void Page_Ioad(object senderSystemEventArgs e)
{
   WebForml webform1;
   webform1= (W ebForm1)Context
Handler
   Iabel1.Text  = webform1.userid

   label2.Text = wehform1.username

}
}

4 使用类的静态属性
描述:
1
在页面里添加必要的控件。
2
定义一个包含静态属性的类.
3
创建可以返回表单的按钮和链接按钮.
4
在按钮单击事件中将要传送的值赋给静态属性并用ResponseRedirect方法重定向到目标页面.
5
目标页面中可以通过静态属性获得源页面中要传送过来的值

//源页面
public class SProperty
{
   
public static string userid=””
   pubic 
static string username=’”’
   
private void Buttonl_Click(object senderSystemEventArgs e)
   
{
   
//将要传送到目标页面的值赋给类的静态属性
    SProperty.userid = TextBox1Text
    SProperty.username = TextBox2. Text

    Response.Redirect(”W ebForm2
aspx”)
    }
}

//目标页面
private void Page_ I oad(object senderSystemEventArgs e)
{
    Iabel1.Text = SProperty.userid

    Iabel2.Text=  SProperty.username

}

5 使用Application全局变量
描述:
Application
变量是全局性的,一旦定义,它将影响到程序的所有部分。

//源页面WebForml中代码:
Application[“userid”] = TextBox1.Text
Application[“username”] = TextBox2.Text
{
Server.Transfer[WebForm2
aspx]
//目标页面WebForm2中代码:
Label1.Text=Application[“userid”].ToString()
Label2.Text=Application[“username”].ToString()

6 使用隐藏域
描述:当一个网页提交给服务器时,隐藏域的内容和其它控件的值一块儿被送到HTTP Form集合中;,而且必须被显式地添加在网页上.使用隐藏域,必须使用HTTP—Post方法提交网页

//ASPNET中的HtmlInputHidden组件提供
//
了隐藏域的功能.该组件用来设置隐藏字段的值,其
//
语法格式是:
<input type=”hidden”id=”组件名称”runat=”server”>

//利用隐藏域传值的程序语句如下:
protected System.Web.UI.HtmlControls.HtmllnputHidden Hidden1;
protected System.Web.UI.HtmlControls.HtmllnputHidden Hidden2;
//给隐藏域赋值
Hidden1.Value = Text1.Text;
Hidden2.Value = Text2.Text;
//获得隐藏域的值
Iabel1.Text = Hidden1.Value;
Iabe2.Text = Hidden2.Va1ue;

 

 

cookie 常用于识别用户。cookie是服务器留在用户计算机中的小文件。每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie。通过 PHP,您能够创建并取回 cookie的值。

 

 

 ------------------ Windows Phones 7手机开发、.Net培训、期待与您交流! ----------------------

原创粉丝点击