Microsoft Study Bible

December 7, 2009

How to pass parameters between Asp.net pages

Filed under: Server technologies — Tags: , , , , — Jackson @ 5:28 am

 There are many methods to pass parameters between pages, including Get, Post, Session, and Application。However,there are some new and unique methods in Asp.Net.
The page B wants to take the value from Page A.
Page A code:

public partial class Default : System.Web.UI.Page
{

 

protected void Page_Load(object sender, EventArgs e)

  {

 

  }

  public string F

 

  {

 

  get { return this.TextBox1.Text.ToString(); }

  set { this.TextBox1.Text = value; }

  }

  public string M

  {

  get { return this.TextBox2.Text.ToString(); }

  set { this.TextBox2.Text = value; }
 

}

  protected void Button1_Click(object sender, EventArgs e)

  {

  Server.Transfer(“Default2.aspx”);

  // Note :the address bar has not been changed

  //Response.Redirect can not work……

  //Response.Redirect(”Default2.aspx”);

    }

  }

Page B code

public partial class Default2 : System.Web.UI.Page
  {

 

protected void Page_Load(object sender, EventArgs e)

 

  {

 

  Default s;

  if (Context.Handler is Default)

  {

  s = (Default)Context.Handler;

  Label1.Text = s.F + “—”+ s.M;

  }

  }

  }

Note: The page Default.aspx.cs itself is a class .So, you can directly use this class in the second page, and encapsulate the parameters to pass for outside visit. 

One page can receive parameters from multiple pages and deal with the results centrally.

1.       New parameter class and interface

/**////

///QueryParams of the summary

  ///

  public class QueryParams

  {

  private string staDate;

  private string endDate;

  /**////

  /// the start-time

  ///

  public string StaDate

  {

  get { return this.staDate; }

  set { this.staDate = value; }

  }

  /**////

  /// the end time

  ///

  public string EndDate

  {

  get { return this.endDate; }

  set { this.endDate = value; }

  }

  }

  /**////

  /// Define Query Interface

  ///

  public interface IQueryParams

  {

  /**////

  /// parameters

  ///

  QueryParams Parameters{get;}

  }

2. To inherit this interface in multiple pages.

public partial class Default3 : System.Web.UI.Page,IQueryParams

  {

  private QueryParams param;

  public QueryParams Parameters

 

  {

  get

  {

  return param;

  }

  }

  protected void Page_Load(object sender, EventArgs e)

  {

 

  }

  protected void Button1_Click(object sender, EventArgs e)

 

  {

  param = new QueryParams();

  param.StaDate = this.TextBox1.Text.ToString();

 

  param.EndDate = this.TextBox2.Text.ToString();

  Server.Transfer(“Default4.aspx”);

  }

  }

3. Do the same operation in other pages.

public partial class Default4 : 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;

 

  }

  Label1.Text = “StaDate + queryParams.StaDate +   EndDate” + queryParams.EndDate;

  }

  }

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress

Close
E-mail It