您的位置:首页 > 编程语言 > C#

C#:总结页面传值几种方法

2014-06-30 18:51 561 查看
小知识点:

1. W7自带 .NetFrameWork 3.5, 兼容模式为 高版本兼容低版本;

2. WF和WPF都是基于XAML的,但是两者的用途不同。

WF是一种开发框架,将工作流嵌入在.NET Framework应用程序中,所主要用于开发创建工作流应用程序。WF:http://msdn.microsoft.com/zh-cn/library/ms734696.aspx

WPF是一种渲染UI的技术是一个用于Windows平台的全新的图形显示系统,它包含在.NET Framework中,使用户能够生成融入了.NET Framework类库的元素的桌面应用程序。WPF:http://msdn.microsoft.com/zh-cn/library/ms742119(v=vs.100)

1. 使用QueryString

Response.Redirect(url);

Request.QueryString[""];

2.使用Session变量

在页面里添加必要的控件
创建可以返回表单的按钮和链接按钮
在按钮或链接按钮的单击事件里,把控件的值添加到session变量里
使用Response.Redirect(或Server.Transfer)方法重定向到另一个页面
在另一个页面提取session的值,在确定不需要使用该session时,要显式清除它

Session["name"]=TextBox.Text;

Server.Transfer("WebForm2.aspx");

Label2.Text=Session["name"].ToString();

Session.Remove("name");

3.使用Server.Transfer

在页面里添加必要的控件
创建返回值的Get属性过程
创建可以返回表单的按钮和链接按钮
在按钮单击事件处理程序中调用Server.Transfer方法转移到指定的页面
在第二个页面中,我们就可以使用Context.Handler属性来获得前一个页面实例对象的引用,通过它,就可以使用存取前一个页面的控件的值了

示例1:
get

{

return TextBox1.Text;

}

private void Button1_Click(object sender,System.EventArgs e)

{

Server.Transfer("WebForm2.aspx");

}


在WebForm2.aspx中务必在第一句话添加<%@ Reference Page="~/WebForm1.aspx" %>或<%@ PreviousPageType VirtualPath="~/WebForm1.aspx" %>

然后在WebForm2.aspx.cs中添加

WebForm1 wf1;

wf1=(WebForm1)Context.Handler;

Label1.Text=wf1.Name;

示例2:

 这个才可以说是 面象对象开发所使用的方法,其使用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);




}


}

[b]4.利用某些控件的PostBackUrl属性


[/b]

示例:仍然是源页面WebForm1.aspx和目标页面WebForm2.aspx.

WebForm1.aspx中的部分代码:

<asp:ButtonID="btnPostBack" Runat="server" Text="PBButton"></asp:Button>

<asp:TextBoxID="txtName" Runat="server" ></asp:TextBox>

<asp:CalendarID="Calendar1" runat="server"></asp:Calendar>

WebForm2.aspx.cs中的部分代码:

protected void Page_Load(objectSender,System.EventArgs e)

{

TextBoxtxtName;

Calendarcalendar1;

txtName=(TextBox)PreviousPage.FindControl("txtName");

calendar1=(Calendar)PreviousPage.FindControl("Calendar1");

Label.Text="Hello,"+txtName.Text+calendar1.SelectedDate.ToShortDateString();

}

使用这种方法存在一个问题:如果在没有单击那个按钮之前,也就是未处理WebForm1.aspx之前,有人请求了WebForm2.aspx,该怎么办?这就需要在WebForm2.aspx中的代码处理之前加一个判断.使用IsCrossPagePostBack属性,这与IsPostBack 属性很相似,它允许检查请求是否来自WebForm1.aspx.如下:

protected void Page_Load(objectSender,System.EventArgs e)

{

if(PreviousPage.IsCrossPagePostBack)

{

TextBox txtName;

Calendar calendar1;

txtName=(TextBox)PreviousPage.FindControl("txtName");

calendar1=(Calendar)PreviousPage.FindControl("Calendar1");

Label.Text="Hello,"+txtName.Text+calendar1.SelectedDate.ToShortDateString();

}

else

{

Response.Redirect("WebForm1.aspx");

}

}

[b][b]5.[b]使用@PreviousPageType指令[/b]

[/b][/b]

TypeName:设置回送时的派生类名

VirtualPath:设置回送时所传送页面的地址.

WebForm1.aspx

get{returnthis.txtName;}//返回一个控件对象

<%@ PreviousPageTypeVirtualPath="~/Page1.aspx"%>,

然后就能引用WebForm1.aspx中定义的属性了.

在WebForm2.aspx.cs中可以有如下引用形式(假设WebForm2.aspx中有一个ID为lblName的Label):

lblName.Text="Hello"+PreviousPage.Name.Text+"<br/>";

[b][b][b]6. 使用Cookie对象变量

[/b][/b][/b]

与Session一样,是对每一个用户而言的,但是有个本质的区别,即Cookie是存放在客户端的,而session是存放在服务器端的。而且Cookie的使用要配合ASP.NET内置对象Request来使用

设置Cookie: HttpCookie cookie_name = new HttpCookie("name");

cookie_name.Value = Label1.Text;

Reponse.AppendCookie(cookie_name);

获取Cookie:

string name= Request.Cookie["name"].Value.ToString();

[b][b][b][b]7. 使用Application 对象变量

[/b][/b][/b][/b]

Application对象的作用范围是整个全局,也就是说对所有用户都有效。其常用的方法用Lock和UnLock。

Application["name"] = Label1.Text;

Server.Transfer("b.aspx");

string name;

Application.Lock();

name = Application["name"].ToString();

Application.UnLock();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: