【问题标题】:How to get hiddenfield value in child page on page load event如何在页面加载事件中获取子页面中的隐藏字段值
【发布时间】:2011-04-08 08:03:31
【问题描述】:

我在form1.aspx 中有一个hdnField 的值,并打开一个弹出页面form2.aspx。 我想在页面加载事件中的form2.aspx.vb 中获取hdnField 值。

我们如何在不使用查询字符串、会话变量、cookie 的情况下做到这一点?

【问题讨论】:

    标签: vb.net webforms hidden-field


    【解决方案1】:

    如果你使用Server.Transfer,从form1.aspx转到form2.aspx,你可以在你的Form2.aspx页面上使用下面的代码获取form1.aspx上隐藏字段的值。

    HiddenField hdnFieldValue = (HiddenField)PreviousPage.Controls[0].FindControl("hdnField");
    

    编辑 我使用上面的代码是因为我有一个母版页,但如果你不使用母版页,你应该只需要这个。

    HiddenField hdnFieldValue = (HiddenField)PreviousPage.FindControl("hdnField");
    

    【讨论】:

      【解决方案2】:

      根据code happiness

      在 JavaScript 中,window.opener 对象可用于从子窗口(弹出窗口或类似窗口)访问父窗口中存在的 HTML 元素。

      让我们考虑两个 HTML 文件:

      openwindow.htm – 它有一个文本框和一个按钮,单击该按钮会在新的浏览器窗口中打开 target.htm 文件 target.htm - 这有代码来更改打开此文件的父窗口(openwindow.htm)中存在的文本框的值

      openwindow.htm: (parent window) 
      <html> 
      <script language="javascript"> 
      function openwindow() 
      { 
      window.open("target.htm","_blank","height=200,width=400, 
      status=yes,toolbar=no,menubar=no,location=no") 
      } 
      </script> 
      <body> 
      <form name=frm> 
      <input id=text1 type=text> 
      <input type=button onclick="javascript:openwindow()" value="Open window.."> 
      </form> 
      </body> 
      </html>
      

      如果您不熟悉 window.open() 方法,那么您会想知道什么是 "height=200, width=400, status=yes, toolbar=no, menubar=no, location=no", don '不用担心,它只是指定了新窗口的大小和外观,这行代码可以改成window.open("target.htm","_blank") 改了看看输出的区别就行了。

      请注意,如果未提供参数“_blank”,则 IE 和 Firefox 之间的功能将有所不同,在 Firefox 中将打开一个新选项卡而不是一个新窗口。只需使用 window.open("target.htm") 并自己查看差异。有关 window.open() 方法及其参数的更多信息,请参阅http://msdn2.microsoft.com/en-us/library/ms536651(VS.85).aspx。另请注意,在 中,id 属性是必需的,那么只有 target.htm 的代码(如下所示)会在 Firefox 中执行。

      target.htm: (child window) 
      <html> 
      <script language="javascript"> 
      function changeparent() 
      { 
      window.opener.document.getElementById('text1').value="Value changed.." 
      } 
      </script> 
      <body> 
      <form> 
      <input type=button onclick="javascript:changeparent()" value="Change opener's textbox's value.."> 
      </form> 
      </body> 
      </html>
      

      希望您能了解上面代码中发生的事情,在新窗口(target.htm)中,我们使用 window.opener 对象来访问父窗口(openwindow.htm)中存在的文本框。你只需要前缀“window.opener”。并编写您将在父窗口的 HTML 页面中编写的相同代码以访问其元素。

      更新答案

      1. Put a hidden field on the child page.
         <asp:HiddenField ID="hidChild" runat="server"/>
      2. in your javascript function 
         var childHidden = document.getElementById('<%hidChild.ClientID%>');
          childHidden.value = window.opener.document.getElementById('text1').value;
      3. Now access this hidden control on the page load event.
         Response.Write(hidChild.Value); or Session["ParentVal"] =hidChild.Value;
      

      这样你在子页面加载中就会有父页面的值。

      【讨论】:

      • 感谢您的回复,我只是想要这样的东西,但它有点不同:在子窗口上,我想从页面加载事件背后的代码上的父控件访问值。
      • 你试过了吗?因为我已经检查过它并且它不工作。页面加载时隐藏字段 (hidChild.Value) 的值为空。
      • 把它放在 If(!ispostback) 里面。或者还有另一种解决方案。你需要传递什么类型的值。您也可以使用 window.showModalDialog(url,arguments,features);因此,在参数中,您可以发送值,如中小长度文本、所有数值等。您不能通过参数发送引用类型对象。使用模态对话框搜索发送值。
      • 如果我把它放在 If(!ispostback) 中。那么它将如何工作,因为第一页加载事件将在我们的 javascript 之后呈现。如果我将它放在任何其他事件上,例如按钮单击,那么我可以获得隐藏字段值,但不能在 If(!ispostback) 上:(
      【解决方案3】:

      您可以在打开弹出窗口时将 hdnField 的值作为 form2.aspx 的查询字符串参数传递。

      form2.aspx?hdnFieldValue=xxx
      

      【讨论】:

        【解决方案4】:

        当表单 2 打开时,您可以将表单 1 中的字段值存储在会话变量中,然后您可以从会话中访问它。

        您可以创建一个具有使用会话的属性的基本页面,并使两个页面都继承自它。

        例如。

        public class CustomPage : Page
        {
          public string YourProperty
          {
           get
           {
             if (Session["YourProperty"] == null)
             {
               Session["YourProperty"] = string.empty;
             }
        
              return Session["YourProperty"] as string;
            }   
           set
           {
             Session["YourProperty"] = value;
           }
         }
        

        然后从页面中单击一个按钮。

        this.YourProperty = value;
        //Open window
        

        在第 2 页。

        txtTextBox.Text = this.YourProperty;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-02-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多