C#:总结页面传值几种方法
小知识点:
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)
Response.Redirect(url);
Request.QueryString[""];
2.使用Session变量
- 在页面里加入必要的控件
- 创建能够返回表单的button和链接button
- 在button或链接button的单击事件里,把控件的值加入到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属性过程
- 创建能够返回表单的button和链接button
- 在button单击事件处理程序中调用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);

}
}
4.利用某些控件的PostBackUrl属性
演示样例:仍然是源页面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();
}
使用这样的方法存在一个问题:假设在没有单击那个button之前,也就是未处理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");
}
}
5.使用@PreviousPageType指令
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/>";
6. 使用Cookie对象变量
与Session一样,是对每个用户而言的,可是有个本质的差别,即Cookie是存放在client的,而session是存放在server端的。并且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();
7. 使用Application 对象变量
Application对象的作用范围是整个全局,也就是说对全部用户都有效。其经常使用的方法用Lock和UnLock。
Application["name"] = Label1.Text;
Server.Transfer("b.aspx");
string name;
Application.Lock();
name = Application["name"].ToString();
Application.UnLock();
}
C#:总结页面传值几种方法的更多相关文章
- Javascript刷新页面的几种方法
Javascript刷新页面的几种方法: window.navigate(location)location.reload()location=locationlocation.assign(loca ...
- Javascript刷新页面的八种方法
/** * Javascript刷新页面的八种方法 * 说明一下,jQuery没有发现刷新页面的方法. */ 1 history.go(0) 2 location.reload() 3 locatio ...
- JS刷新页面的几种方法(转)
Javascript刷新页面的几种方法: 1 history.go(0) 2 location.reload() 3 location=location 4 location.assign(locat ...
- asp.net跳转页面的三种方法比较
目前,对于学习asp.net的很多朋友来讲,实现跳转页面的方法还不是很了解.本文将为朋友们介绍利用asp.net跳转页面的三种方法,并对其之间的形式进行比较,希望能够对朋友们有所帮助. ASP.NET ...
- php抓取页面的几种方法详解
本篇文章是对php抓取页面的几种方法进行了详细的分析介绍,需要的朋友参考下 在 做一些天气预报或者RSS订阅的程序时,往往需要抓取非本地文件,一般情况下都是利用php模拟浏览器的访问,通过http请求 ...
- Javascript刷新页面的几种方法:
Javascript刷新页面的几种方法: 1 history.go(0) 2 window.location.reload() window.location.reload(true) ...
- jquery mobile切换页面的几种方法
jquery mobile切换页面的几种方法 - 不厚道青蛙之焦油潭 - 博客频道 - CSDN.NET jquery mobile切换页面的几种方法 分类: phonegap html5 2012- ...
- Nginx实现404页面的几种方法【转】
一个网站项目,肯定是避免不了404页面的,通常使用Nginx作为Web服务器时,有以下集中配置方式,一起来看看. 第一种:Nginx自己的错误页面 Nginx访问一个静态的html 页面,当这个页面没 ...
- Apacheserver自己定义404页面的两种方法以及.htaccess的重要命令总结
Apacheserver自己定义404错误页面有两种方法: 第一种方法最简单,直接在Apache的httpd.conf下进行配置改动命令,改动的内容请參看.htaccess命令写法中的自己定义错误页面 ...
随机推荐
- 6月10日-IOS应用-日记本
嗯,经过这几天的学习,我的第一个IOS应用,日记本算是学习完毕了,下面写一篇日记,记录所学到的知识和需要继续学习的地方. 1,首先是ViewController,必须添加两个协议UITableView ...
- 深入浅出—JAVA(10)
10.数字与静态 静态变量是共享的.同一类所有的实例共享一份静态变量. 实例变量:每个实例一个.静态变量:每个类一个. 数字的格式化 唯一必填的项目是类型 package xiao;class Sta ...
- C陷阱与缺陷(二)
第二章 语法陷阱 2.1 理解函数声明 (*(void(*)())0)();任何C变量的声明都由两部分组成:类型以及一组类似表达式的声明符.一旦我们知道了如何声明一个给定类型的变量,那么该类型的类型转 ...
- 一个简单的win32窗口
#include <windows.h>#include <stdio.h> LRESULT CALLBACK WinSunProc( HWND hwnd, // ...
- Lua,Lua API,配置文件
想像一个场景:你的c程序须要有一个窗体,你想让用户能够自己定义窗体大小.方法非常多.比方使用环境变量,或键值对的文件. 无论如何,你须要解析它. 使用lua配置文件是个不错的选择. 首先,你能够定义例 ...
- VS2005+WINDDK+Driver Studio 3.2个人总结
通过在网上搜索大量的资料,终于把环境搭建起来.对于我这样的驱动新手来说,理应把高手们的东西整理并总结下,方便以后的初学者. 这三个软件的安装顺序没有具体规定,也有高手推荐的顺序,我自己也是重复安装卸载 ...
- uVa 714 (二分法)
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description Before th ...
- C++创建动态链接库(*.dll)
1. 从 “文件”菜单中,选择 “新建”,然后选择 “项目…”. 2. 在“项目类型”窗格中,选择“Visual C++”下的“Win32”. 3. 在“模板”窗格中,选 ...
- AFNetworking 保存Cookie Session 和 Webview 共享Cookie
1, 请求并携带SessionId, 代码如下: //当请求成共后调用如下代码, 保存Cookie NSArray *cookies = [[NSHTTPCookieStorage sharedHTT ...
- php抓取ajax页面返回图片。
要抓取的页面:http://pic.hao123.com/ 当我们往下滚动的时候,图片是用ajax来动态获取的.这就需要我们仔细分析页面了. 可以看到,异步加载的ajax文件为: http://pic ...