zoukankan      html  css  js  c++  java
  • 使用PreviousPage来获取前一页页面的元素

    比如从test1.aspx页面点击按钮进入test2.aspx页面,如果想在test2.aspx页面中得到test1.aspx页面中某些控件的值.

    test1.aspx前台代码,  这里要注意的按钮一定要设置postbackurl="test2.aspx" 属性,不能在它的CS代码中比如用Redirect 的方法


     1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test1.aspx.cs" Inherits="Print_test1" %>
     2
     3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     4
     5<html xmlns="http://www.w3.org/1999/xhtml" >
     6<head runat="server">
     7    <title>无标题页</title>
     8</head>
     9<body>
    10    <form id="form1" runat="server">
    11    <div>
    12        <asp:TextBox ID="TextBox1" runat="server" Text="初值:sadfasdf"></asp:TextBox>
    13        <asp:Button ID="Button1" runat="server" Text="Button"  postbackurl="test2.aspx" /></div>
    14    </form>
    15</body>
    16</html>
    17
    test1.aspx.cs      //这个页面没写代码

     1using System;
     2using System.Data;
     3using System.Configuration;
     4using System.Collections;
     5using System.Web;
     6using System.Web.Security;
     7using System.Web.UI;
     8using System.Web.UI.WebControls;
     9using System.Web.UI.WebControls.WebParts;
    10using System.Web.UI.HtmlControls;
    11
    12public partial class Print_test1 : System.Web.UI.Page
    13{
    14    protected void Page_Load(object sender, EventArgs e)
    15    {
    16    }
    17}
    18





    test2.aspx 前台代码   //这里也没写什么东东

     1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="Print_test2" %>
     2<%@ PreviousPageType VirtualPath="test1.aspx" %>
     3
     4<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     5
     6<html xmlns="http://www.w3.org/1999/xhtml" >
     7<head runat="server">
     8    <title>无标题页</title>
     9</head>
    10<body>
    11    <form id="form1" runat="server">
    12    <div>
    13    
    14    </div>
    15    </form>
    16</body>
    17</html>
    18
    test2.aspx.cs

     1using System;
     2using System.Data;
     3using System.Configuration;
     4using System.Collections;
     5using System.Web;
     6using System.Web.Security;
     7using System.Web.UI;
     8using System.Web.UI.WebControls;
     9using System.Web.UI.WebControls.WebParts;
    10using System.Web.UI.HtmlControls;
    11
    12public partial class Print_test2 : System.Web.UI.Page
    13{
    14    protected void Page_Load(object sender, EventArgs e)
    15    {
    16        //Response.Write(this.PreviousPage.FindControl("TextBox1"));
    17        string txt = ((TextBox)this.PreviousPage.FindControl("TextBox1")).Text;
    18
    19        Response.Write(txt);
    20        //(this.PreviousPage).test();       
    21    }
    22}
    23

    在test2.aspx.cs 代码中使用类似((TextBox)this.PreviousPage.FindControl("TextBox1")).Text 的方法来访问前页的属性或方法.
  • 相关阅读:
    Balance的数学思想构造辅助函数
    1663. Smallest String With A Given Numeric Value (M)
    1680. Concatenation of Consecutive Binary Numbers (M)
    1631. Path With Minimum Effort (M)
    1437. Check If All 1's Are at Least Length K Places Away (E)
    1329. Sort the Matrix Diagonally (M)
    1657. Determine if Two Strings Are Close (M)
    1673. Find the Most Competitive Subsequence (M)
    1641. Count Sorted Vowel Strings (M)
    1679. Max Number of K-Sum Pairs (M)
  • 原文地址:https://www.cnblogs.com/gfwei/p/664195.html
Copyright © 2011-2022 走看看