小知识点:

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变量

  1. 在页面里加入必要的控件
  2. 创建能够返回表单的button和链接button
  3. 在button或链接button的单击事件里,把控件的值加入到session变量里
  4. 使用Response.Redirect(或Server.Transfer)方法重定向到还有一个页面
  5. 在还有一个页面提取session的值,在确定不须要使用该session时,要显式清除它

Session["name"]=TextBox.Text;

Server.Transfer("WebForm2.aspx");

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

Session.Remove("name");

3.使用Server.Transfer

  1. 在页面里加入必要的控件
  2. 创建返回值的Get属性过程
  3. 创建能够返回表单的button和链接button
  4. 在button单击事件处理程序中调用Server.Transfer方法转移到指定的页面
  5. 在第二个页面中,我们就能够使用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#:总结页面传值几种方法的更多相关文章

  1. Javascript刷新页面的几种方法

    Javascript刷新页面的几种方法: window.navigate(location)location.reload()location=locationlocation.assign(loca ...

  2. Javascript刷新页面的八种方法

    /** * Javascript刷新页面的八种方法 * 说明一下,jQuery没有发现刷新页面的方法. */ 1 history.go(0) 2 location.reload() 3 locatio ...

  3. JS刷新页面的几种方法(转)

    Javascript刷新页面的几种方法: 1 history.go(0) 2 location.reload() 3 location=location 4 location.assign(locat ...

  4. asp.net跳转页面的三种方法比较

    目前,对于学习asp.net的很多朋友来讲,实现跳转页面的方法还不是很了解.本文将为朋友们介绍利用asp.net跳转页面的三种方法,并对其之间的形式进行比较,希望能够对朋友们有所帮助. ASP.NET ...

  5. php抓取页面的几种方法详解

    本篇文章是对php抓取页面的几种方法进行了详细的分析介绍,需要的朋友参考下 在 做一些天气预报或者RSS订阅的程序时,往往需要抓取非本地文件,一般情况下都是利用php模拟浏览器的访问,通过http请求 ...

  6. Javascript刷新页面的几种方法:

    Javascript刷新页面的几种方法: 1    history.go(0) 2    window.location.reload() window.location.reload(true)  ...

  7. jquery mobile切换页面的几种方法

    jquery mobile切换页面的几种方法 - 不厚道青蛙之焦油潭 - 博客频道 - CSDN.NET jquery mobile切换页面的几种方法 分类: phonegap html5 2012- ...

  8. Nginx实现404页面的几种方法【转】

    一个网站项目,肯定是避免不了404页面的,通常使用Nginx作为Web服务器时,有以下集中配置方式,一起来看看. 第一种:Nginx自己的错误页面 Nginx访问一个静态的html 页面,当这个页面没 ...

  9. Apacheserver自己定义404页面的两种方法以及.htaccess的重要命令总结

    Apacheserver自己定义404错误页面有两种方法: 第一种方法最简单,直接在Apache的httpd.conf下进行配置改动命令,改动的内容请參看.htaccess命令写法中的自己定义错误页面 ...

随机推荐

  1. 在一个数组中是否存在两个数A、B的和为M

    #include <iostream>#include <algorithm>//#include <vector>using namespace std; int ...

  2. HDU 2023题解分析

    我想说这道题我还没弄明白我错哪了,交了20多遍一直都是Runtime Error,改了N次还是不对,后来搜了一下,说是数组开小了,又把数组开大,还不对,又改发现一个平均值求错,再改,还不对,洗洗睡吧. ...

  3. unity之uv贴图画圆弧,圆弧面,不规则图形

    由于最近一直没有时间,所以这篇博客一直没发,下面我说说uv画圆弧,圆面,不规则面拼接. 先来两张效果图 图截的不咋滴,凑合着看吧,画圆弧主要用的贝塞尔曲线画的,我感觉这个比较简单,当然大家也可以使用圆 ...

  4. Kettle之数据抽取、转换、装载

    Kettle 官网 ETL利器Kettle实战应用解析系列 利用kettle组件导入excel文件到数据库 kettle中实现动态SQL查询 java中调用kettle转换文件

  5. Code one 码

    Code one是一种用成像设备识别的矩阵式二维条码.Code one符号中包含可由快速性线性探测器识别的识别图案.每一模块的宽和高的尺寸为X.  Code one符号共有10种版本及时14种尺寸.最 ...

  6. file.encoding到底指的是什么呢?

    转载请注明来源:http://blog.csdn.net/loongshawn/article/details/50918506 <Java利用System.getProperty(“file. ...

  7. 「操作系统」:The most useful condition codes

    CF: Carry Flag.The most recent operation generated a carry out of the most significant bit. Used to ...

  8. CentOS下mysql最大连接数设置 1040 too many connection

    当最大连接数比較小时,可能会出现"1040 too many connection"错误. 能够通过改动配置文件来改动最大连接数,但我连配置文件在哪都不知道,应该怎么办呢? 首先须 ...

  9. Matrix Factorization, Algorithms, Applications, and Avaliable packages

    矩阵分解 来源:http://www.cvchina.info/2011/09/05/matrix-factorization-jungle/ 美帝的有心人士收集了市面上的矩阵分解的差点儿全部算法和应 ...

  10. CentOS6.5 下在Nginx中添加SSL证书以支持HTTPS协议访问

    参考文献: 1. NginxV1.8.0安装与配置 2. CentOS下在Nginx中添加SSL证书以支持HTTPS协议访问 3. nginx配置ssl证书的方法 4.nginx强制使用https访问 ...