• 静态页面的值传递


     

    1两窗口之间存在着关系.父窗口parent.htm打开子窗口son.htm
    子窗口可以通过window.opener指向父窗口.这样可以访问父窗口的对象.

    优点:取值方便.只要window.opener指向父窗口,就可以访问所有对象.
           不仅可以访问值,还可以访问父窗口的方法.值长度无限制.
    缺点:两窗口要存在着关系.就是利用window.open打开的窗口.不能跨域.
          

    Post.htm

    <input type=text name=maintext>
    <input type=button onclick="window.open('Read.htm')" value="Open">

    Read.htm

    <script language="javascript" >
    //window.open打开的窗口.
    //利用opener指向父窗口.
    var parentText = window.opener.document.all.maintext.value;
    alert(parentText);
    </script>


    2利用Cookie.Session.Application

    Cookie是浏览器存储少量命名数据.
    它与某个特定的网页或网站关联在一起.
    Cookie用来给浏览器提供内存,
    以便脚本和服务器程序可以在一个页面中使用另一个页面的输入数据.

    优点:可以在同源内的任意网页内访问.生命期可以设置.
    缺点:值长度有限制.

    Post.htm

    <input type="text" name="txt1">
    <input type="button" onclick="setCookie('baobao',document.all.txt1.value)" value="Post">
    <script language="javascript" >
    function setCookie(name,value)
    {
    /*
     *--------------- setCookie(name,value) -----------------
     * setCookie(name,value)
     * 功能:设置得变量name的值
     * 参数:name,字符串;value,字符串.
     * 实例:setCookie('username','baobao')
     * update:2004-6-11 10:30
     *--------------- setCookie(name,value) -----------------
     */
        var Days = 30; //此 cookie 将被保存 30 天
        var exp  = new Date();
        exp.setTime(exp.getTime() + Days*24*60*60*1000);
        document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
        location.href = "Read.htm"; //接收页面.
    }
    </script>


    Read.htm

    <script language="javascript" >
    function getCookie(name)
    {
    /*
     *--------------- getCookie(name) -----------------
     * getCookie(name)
     * 功能:取得变量name的值
     * 参数:name,字符串.
     * 实例:alert(getCookie("baobao"));
     * update:2004-6-11 10:30
     *--------------- getCookie(name) -----------------
     */
        var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
        if(arr !=null) return unescape(arr[2]); return null;
    }
    alert(getCookie("baobao"));
    </script>


     3URL篇

    能过URL进行传值.把要传递的信息接在URL上.

    优点:取值方便.可以跨域.
    缺点:值长度有限制.

    Post.htm

    <input type="text" name="username">
    <input type="text" name="sex">
    <input type="button" onclick="Post()" value="Post">
    <script language="javascript" >
    function Post()
    {
        //单个值 Read.htm?username=baobao;
        //多全值 Read.htm?username=baobao&sex=male;
        url = "Read.htm?username="+escape(document.all.username.value);
        url += "&sex=" + escape(document.all.sex.value);
        location.href=url;
    }
    </script>


    Read.htm

    <script language="javascript" >
    /*
     *--------------- Read.htm -----------------
     * Request[key]
     * 功能:实现ASP的取得URL字符串,Request("AAA")
     * 参数:key,字符串.
     * 实例:alert(Request["AAA"])
     * author:wanghr100(灰豆宝宝.net)
     * update:2004-6-11 10:30
     *--------------- Request.htm -----------------
     */
    var url=location.search;
    var Request = new Object();
    if(url.indexOf("?")!=-1)
    {
        var str = url.substr(1)  //去掉?号
        strs = str.split("&");
        for(var i=0;i<strs.length;i++)
        {
            Request[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
        }
    }
    alert(Request["username"])
    alert(Request["sex"])
    </script>

    转载自:http://blog.csdn.net/wanghr100/archive/2004/07/01/PostValue.aspx 

    四、使用Server.Transfer
    虽然这种方法有点复杂,但也不失为一种在页面传值的方式。
    举个例子看看:
    1、创建一个web form
    2、在新建的web form中放置一个button1,在放置两个TextBox1,TextBox2
    3、为button按钮创建click事件
    代码如下:
    private void Button1_Click
    (object sender, System.EventArgs e)
    {
    Server.Transfer("webform2.aspx");
    }
    4、创建过程来返回TextBox1,TextBox2控件的值代码如下:
    public string Name
    {
    get
    {
    return TextBox1.Text;
    }
    }

    public string EMail
    {
    get
    {
    return TextBox2.Text;
    }
    }
    5、新建一个目标页面命名为webform2
    6、在webform2中放置两个Label1,Label2
    在webform2的Page_Load中添加如下代码:
    private void Page_Load
    (object sender, System.EventArgs e)
    {
    //创建原始窗体的实例
    WebForm1 wf1;
    //获得实例化的句柄
    wf1=(WebForm1)Context.Handler;
    Label1.Text=wf1.Name;
    Label2.Text=wf1.EMail;

    }
    运行,即可看到传递后的结果了。

    祥解:
    ASP.NET Server.Transfer()是在两个页面之间进行传值的好方法,从A页面Transfer到B页面时,就可以在B页面通过Context.Handler获得A页面的一个类的实例,从而在B调用A的各个成员对象。

    下面的示例建立了WebForm1和WebForm2,通过Server.Transfer()方法演示在WebForm2中读取WebForm1的文本框、读取属性、通过Context传值、调用WebForm1的方法等:

    WebForm1上放置一个TextBox1和一个Button1,程序如下:

    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.TextBox TextBox1;
    protected System.Web.UI.WebControls.Button Button1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    Context.Items.Add("Context","Context from Form1");
    }
    public string Time
    {
    get{return DateTime.Now.ToString();}
    }
    public string TestFun()
    {
    return "Function of WebForm1 Called";
    }
    #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    InitializeComponent();
    base.OnInit(e);
    }

    private void InitializeComponent()
    {
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void Button1_Click(object sender, System.EventArgs e)
    {
    Server.Transfer("WebForm2.aspx", true);
    }

    在WebForm2上放置一个Literal1控件,程序如下:

    public class WebForm2 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Literal Literal1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    string strTxt="";
    WebForm1 oForm=(WebForm1)this.Context.Handler;
    strTxt+="Value of Textbox:"+Request.Form["TextBox1"] +"<br>";
    strTxt+="Time Property:"+oForm.Time +"<br>";
    strTxt+="Context String:"+Context.Items["Context"].ToString() +"<br>";
    strTxt+=oForm.TestFun() +"<br>";
    Literal1.Text =strTxt;
    }

    #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    InitializeComponent();
    base.OnInit(e);
    }

    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion
    }

    补充说明,就是Transfer方法的第二个参数指示是否保留页面的Form和QuerryString的值,你可以试着把它设为False,则在WebForm2中将读不到TextBox1的值。

  • 相关阅读:
    JavaScript HTML 输入示例(Input)
    JS Web API
    JavaScript DOM 示例
    JavaScript浏览器对象示例
    JavaScript示例
    JavaScript HTML DOM 事件示例
    JavaScript ES(610)语法大全
    JavaScript HTML 对象示例(Objects Examples)
    Windows下安装RabbitMQ
    ABP框架入门学习(五)——权限配置
  • 原文地址:https://www.cnblogs.com/sutengcn/p/303046.html
Copyright © 2020-2023  润新知