时间:2021-07-01 10:21:17 帮助过:34人阅读
/**//// <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);
}
}