ASP.NET頁面傳值的方法

來源:互聯網
上載者:User

標籤:cat   start   url   system   form   this   span   blog   pre   

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頁面:


1 <asp:Button ID="Button1" Runat="server" Text="PostToAnotherPage" PostBackUrl="~/Default2.aspx" />
2

  default2.aspx頁面:


1 if (PreviousPage != null)
2        {
3            TextBox textBox1 = (TextBox)PreviousPage.FindControl("TextBox1");
4           Response.write(textBox1.Text );
5        }


六.使用Server.Transfer方法
    這個才可以說是 面象對象開發所使用的方法,其使用Server.Transfer方法把流程從當前頁面引導到另一個頁面中,新的頁面使用前一個頁面的應答流,所以這個方 法是完全面象對象的,簡潔有效。下面這個代碼是展示在需要很多個參數的時候,使用的方法,如果參數比較少就沒必要使用這個方法了.
如果讓所有的查詢頁面都繼承一個介面,在該介面中定義一個方法,該方法的唯一作用就是讓結果頁面獲得構建結果時所需的參數,就可實現多頁面共用一個結果頁面操作! 

1、先定義一個類,用該類放置所有查詢參數:


/// <summary>
/// QueryParams 的摘要說明
/// </summary>
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、介面定義: 


///   <summary > 
    ///   定義查詢介面。 
    ///   </summary > 
    public interface IQueryParams
    {
        ///   <summary > 
        ///   參數 
        ///   </summary > 
        QueryParams Parameters { get;}
    } 


     3、查詢頁面繼承IQueryParams介面(QueryPage.aspx):
QueryPage.aspx


<form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
        <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
         <asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
        <asp:Button ID="btnEnter" runat="server" Text="Button" OnClick="btnEnter_Click" /></div>
    </form>


QueryPage.aspx.cs


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(" <br/ >Lastname: ");
        Response.Write(queryParams.LastName); 
        Response.Write(" <br/ >Age: ");
        Response.Write(queryParams.Age); 

    }
}

ASP.NET頁面傳值的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.