zoukankan      html  css  js  c++  java
  • 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)



    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# 获取枚举 Enum 变量值的 Description 属性
    javascript获取网页URL地址及参数等
    也谈用反射实现Enum→String映射:一种重视性能的方法20090412 21:35一、问题的提出
    LinQ 多表查询
    Windows Service得到当前用户的名字和域
    ASP.NET 部署
    加密解密-C#
    Domino Internet邮件
    C#动态创建表
    读取Excel2000文件
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/3893076.html
Copyright © 2011-2022 走看看