所有栏目 | 云社区 美国云服务器[国内云主机商]
你的位置:首页 > 云社区 » 正文

NET中传值和接传过来的值的方式都有哪些?

发布时间:2020-04-15 16:50:02

资讯分类:net  页面  设置  方式
NET中传值和接传过来的值的方式都有哪些?

  

1.querystring的方式:将需要的值直接写到链接的后面,这些内容将直接显示到地址栏中,在传递安全性要求不高的一个或多个值或者是结构简单的值就可以使用这样的方法。   如: Response.Redirect( "target.aspx?param1=hello¶m2=hi ")   在接收页面可以通过request的方式得到所传递的值: string str = Request.QueryString["param1"];   

2.cookie方式,使用cookie对象方式,cookie是放在客户端的   设置Cookie: HttpCookie cookie_name = new HttpCookie("name");   cookie_name.Value = Label1.Text;   Reponse.AppendCookie(cookie_name);   获取Cookie:   string name= Request.Cookie["name"].Value.ToString();   

3.session变量,session是放置于服务器端的   设置Session: Session["name"] ="hello";   获取Session: string name = Session["name"].ToString();   

4.使用Application 对象变量   Application对象的作用范围是整个全局,也就是说对所有用户都有效。此种方法不常使用,因为Application在一个应用程序域范围共享,所有用户可以改变及设置其值,故只应用计数器等需要全局变量的地方。   设置Application : Application["name"] = ="hello";   获取Application : string name = Application["name"].ToString();   

5. PostBackUrl()方法   default.aspx页面:   Code   1

  public class QueryParams   {   private string firstName;   private string lastname;   private int age;   public string Firstname   {   get { return this.firstname; }   set { this.firstname = value; }   }   public string LastName   {   get { return this.lastname; }   set { this.lastname = value; }   }   public string Age   {   get { return this.age; }   set { this.age = value; }   }   }   2、接口定义:   Code   /**////  public interface IQueryParams   {   /**////  QueryParams Parameters { get;}   }   3、查询页面继承IQueryParams接口(QueryPage.aspx):   QueryPage.aspx   Code     QueryPage.aspx.cs   Code   public partial class QueryPage : System.Web.UI.Page, IQueryParams   {   private QueryParams queryParams;   public QueryParams Parameters   {   get   {   return queryParams;   }   }   public void btnEnter_Click(object sender, System.EventArgs e)   {   //赋值   queryParams = new QueryParams();   queryParams.FirstnName = this.txtFirstName.Text;   queryParams.Lastname = this.txtLastName.Text;   queryParams.Age = this.txtAge.Text;   Server.Transfer( "ResultPage.aspx ");   }   protected void Page_Load(object sender, EventArgs e)   { }   }   4、接收页面(ResultPage.aspx):   ResultPage.aspx.cs   public partial class ResultPage : System.Web.UI.Page   {   protected void Page_Load(object sender, EventArgs e)   {   QueryParams queryParams = new QueryParams();   IQueryParams queryInterface;   //实现该接口的页面   if (Context.Handler is IQueryParams)   {   queryInterface = (IQueryParams)Context.Handler;   queryParams = queryInterface.Parameters;   }   Response.Write("FirstName: ");   Response.Write(queryParams.FirstName);   Response.Write(" Lastname: ");   Response.Write(queryParams.LastName);   Response.Write(" Age: ");   Response.Write(queryParams.Age);   }   }

留言与评论(共有 0 条评论)
   
验证码:
Top