星空网 > 软件开发 > ASP.net

ASP.NET页面传值的方法

ASP.NET页面传值的方法

From:Refresh-air

在面试的时候,经常会遇到这样的问题,其实我们会对其中的几种方法比较熟悉,因为项目中经常使用。但是要全面的回答ASP.NET中页面传值的方式,估计往往很难全面。
一. 使用QueryString变量
    QueryString是一种非常简单也是使用比较多的一种传值方式,但是它将传递的值显示在浏览器的地址栏中,如果是传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法。

         Response.Redirect( "target.aspx?param1=hello&param2=hi ") 
        接收页面:   string   str   =   Request.QueryString["param1"]; 
                               string   str1   =  Request.QueryString["param2];
二.使用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();
 
三. 使用Session变量(session是存放在服务器端的)
  设置Session:      Session["name"] ="hello";
        获取Session:        string name = Session["name"].ToString();
四.使用Application 对象变量
  Application对象的作用范围是整个全局,也就是说对所有用户都有效。此种方法不常使用,因为Application在一个应用程序域范围共享,所有用户可以改变及设置其值,故只应用计数器等需要全局变量的地方。 
        设置Application :    Application["name"] = ="hello";
        获取Application :     string   name = Application["name"].ToString();
五. PostBackUrl()方法
     default.aspx页面:

ASP.NET页面传值的方法
1ASP.NET页面传值的方法 <asp:Button ID="Button1" Runat="server" Text="PostToAnotherPage" PostBackUrl="~/Default2.aspx" />
2ASP.NET页面传值的方法

  default2.aspx页面:

ASP.NET页面传值的方法
1ASP.NET页面传值的方法 if (PreviousPage != null)
2ASP.NET页面传值的方法        {
3ASP.NET页面传值的方法            TextBox textBox1 = (TextBox)PreviousPage.FindControl("TextBox1");
4ASP.NET页面传值的方法           Response.write(textBox1.Text );
5ASP.NET页面传值的方法        }


六.使用Server.Transfer方法
    这个才可以说是 面象对象开发所使用的方法,其使用Server.Transfer方法把流程从当前页面引导到另一个页面中,新的页面使用前一个页面的应答流,所以这个方 法是完全面象对象的,简洁有效。下面这个代码是展示在需要很多个参数的时候,使用的方法,如果参数比较少就没必要使用这个方法了.
如果让所有的查询页面都继承一个接口,在该接口中定义一个方法,该方法的唯一作用就是让结果页面获得构建结果时所需的参数,就可实现多页面共享一个结果页面操作! 

1、先定义一个类,用该类放置所有查询参数:

ASP.NET页面传值的方法
ASP.NET页面传值的方法/// <summary>
ASP.NET页面传值的方法/// QueryParams 的摘要说明
ASP.NET页面传值的方法/// </summary>
ASP.NET页面传值的方法public class QueryParams
ASP.NET页面传值的方法
ASP.NET页面传值的方法  private   string   firstName; 
ASP.NET页面传值的方法        private   string   lastname;
ASP.NET页面传值的方法        private   int    age;
ASP.NET页面传值的方法      
ASP.NET页面传值的方法
ASP.NET页面传值的方法         public string Firstname 
ASP.NET页面传值的方法        {
ASP.NET页面传值的方法            get { return this.firstname; }
ASP.NET页面传值的方法            set { this.firstname = value; } 
ASP.NET页面传值的方法        } 
ASP.NET页面传值的方法        public string LastName 
ASP.NET页面传值的方法        {
ASP.NET页面传值的方法            get { return this.lastname; }
ASP.NET页面传值的方法            set { this.lastname = value; } 
ASP.NET页面传值的方法        }
ASP.NET页面传值的方法        public string Age
ASP.NET页面传值的方法        {
ASP.NET页面传值的方法            get { return this.age; }
ASP.NET页面传值的方法            set { this.age = value; }
ASP.NET页面传值的方法        } 
ASP.NET页面传值的方法 
ASP.NET页面传值的方法}
ASP.NET页面传值的方法
ASP.NET页面传值的方法


2、接口定义: 

ASP.NET页面传值的方法
ASP.NET页面传值的方法///   <summary > 
ASP.NET页面传值的方法    ///   定义查询接口。 
ASP.NET页面传值的方法    ///   </summary > 
ASP.NET页面传值的方法    public interface IQueryParams
ASP.NET页面传值的方法    {
ASP.NET页面传值的方法        ///   <summary > 
ASP.NET页面传值的方法        ///   参数 
ASP.NET页面传值的方法        ///   </summary > 
ASP.NET页面传值的方法        QueryParams Parameters { get;}
ASP.NET页面传值的方法    } 
ASP.NET页面传值的方法


     3、查询页面继承IQueryParams接口(QueryPage.aspx):
QueryPage.aspx

ASP.NET页面传值的方法
ASP.NET页面传值的方法<form id="form1" runat="server">
ASP.NET页面传值的方法    <div>
ASP.NET页面传值的方法        <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
ASP.NET页面传值的方法        <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
ASP.NET页面传值的方法         <asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
ASP.NET页面传值的方法        <asp:Button ID="btnEnter" runat="server" Text="Button" OnClick="btnEnter_Click" /></div>
ASP.NET页面传值的方法    </form>
ASP.NET页面传值的方法


QueryPage.aspx.cs

ASP.NET页面传值的方法
ASP.NET页面传值的方法public partial class QueryPage : System.Web.UI.Page, IQueryParams 
ASP.NET页面传值的方法{
ASP.NET页面传值的方法    private QueryParams queryParams;
ASP.NET页面传值的方法   
ASP.NET页面传值的方法        public   QueryParams   Parameters 
ASP.NET页面传值的方法        { 
ASP.NET页面传值的方法            get 
ASP.NET页面传值的方法            { 
ASP.NET页面传值的方法                 return   queryParams; 
ASP.NET页面传值的方法            } 
ASP.NET页面传值的方法        } 
ASP.NET页面传值的方法       
ASP.NET页面传值的方法        public   void   btnEnter_Click(object   sender,   System.EventArgs   e) 
ASP.NET页面传值的方法        { 
ASP.NET页面传值的方法            //赋值 
ASP.NET页面传值的方法            queryParams   =   new   QueryParams();
ASP.NET页面传值的方法            queryParams.FirstnName = this.txtFirstName.Text;
ASP.NET页面传值的方法            queryParams.Lastname = this.txtLastName.Text;
ASP.NET页面传值的方法            queryParams.Age = this.txtAge.Text;
ASP.NET页面传值的方法            Server.Transfer( "ResultPage.aspx "); 
ASP.NET页面传值的方法        }
ASP.NET页面传值的方法
ASP.NET页面传值的方法    protected void Page_Load(object sender, EventArgs e)
ASP.NET页面传值的方法    {  }
ASP.NET页面传值的方法}
ASP.NET页面传值的方法4、接收页面(ResultPage.aspx):
ASP.NET页面传值的方法ResultPage.aspx.cs
ASP.NET页面传值的方法public partial class ResultPage : System.Web.UI.Page
ASP.NET页面传值的方法{
ASP.NET页面传值的方法    protected void Page_Load(object sender, EventArgs e)
ASP.NET页面传值的方法    {
ASP.NET页面传值的方法        QueryParams queryParams = new QueryParams();
ASP.NET页面传值的方法        IQueryParams queryInterface;
ASP.NET页面传值的方法        //实现该接口的页面 
ASP.NET页面传值的方法        if (Context.Handler is IQueryParams)
ASP.NET页面传值的方法        {
ASP.NET页面传值的方法            queryInterface = (IQueryParams)Context.Handler;
ASP.NET页面传值的方法            queryParams = queryInterface.Parameters;
ASP.NET页面传值的方法        }
ASP.NET页面传值的方法
ASP.NET页面传值的方法        Response.Write("FirstName: ");
ASP.NET页面传值的方法        Response.Write(queryParams.FirstName);
ASP.NET页面传值的方法        Response.Write(" <br/ >Lastname: ");
ASP.NET页面传值的方法        Response.Write(queryParams.LastName); 
ASP.NET页面传值的方法        Response.Write(" <br/ >Age: ");
ASP.NET页面传值的方法        Response.Write(queryParams.Age); 
ASP.NET页面传值的方法
ASP.NET页面传值的方法    }
ASP.NET页面传值的方法}




原标题:ASP.NET页面传值的方法

关键词:ASP.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

shopee财报2022第二季度发布及市场解析:https://www.ikjzd.com/articles/1560099350720319489
要发货的抓紧时间啦!各大船公司春节停航情况汇总!:https://www.ikjzd.com/articles/15601
lazada店铺运营思路,lazada店铺经营战略模式:https://www.ikjzd.com/articles/1560103382347374593
Shopee有哪些出货流程?需要注意事项:https://www.ikjzd.com/articles/1560110005678542849
做外贸注意哪些细节,外贸流程中需要注意的小问题技巧:https://www.ikjzd.com/articles/1560155574807412737
首届国货品牌出海季活动时间主题!有哪些品牌参与?:https://www.ikjzd.com/articles/1560180851520421889
合肥有什么约会的地方好玩的地方 合肥哪里适合情侣好玩的地方:https://www.vstour.cn/a/411237.html
曾家山滑雪场-曾家山滑雪场号码:https://www.vstour.cn/a/411238.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流