如何在页面加载事件中获取子页面中的隐藏字段值

发布于 2024-10-31 13:15:49 字数 211 浏览 0 评论 0原文

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

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

I have a value in hdnField in form1.aspx and opening a popup page form2.aspx.
I want to get that hdnField value in form2.aspx.vb on page load event.

How can we do this without using query string, session variable, cookies?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浪菊怪哟 2024-11-07 13:15:49

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

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

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

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

If you use Server.Transfer, to go to form2.aspx from form1.aspx, you can use the following code on your Form2.aspx page get the value of the hidden field on form1.aspx.

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

EDIT
I use the code above because I have a masterpage, but if you don't use a masterpage you should only need this.

HiddenField hdnFieldValue = (HiddenField)PreviousPage.FindControl("hdnField");
A君 2024-11-07 13:15:49

根据 代码幸福

在 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”,不用担心,它只是指定了新窗口的大小和外观,这行代码可以改为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;

这样您就可以在子页面加载中获得父页面值。

As per code happiness

In JavaScript, the window.opener object can be used to access the HTML elements present in the parent window from child window(popup or similar).

Let us consider two HTML files:

openwindow.htm – this has a text box and a button, clicking the button opens the target.htm file in a new browser window
target.htm – this has code to change the value of the text box present in the parent window(openwindow.htm) that opens this file

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>

If you are not familiar with window.open() method, then you would be wondering what is "height=200, width=400, status=yes, toolbar=no, menubar=no, location=no", don’t worry, it just specifies the size and appearance of the new window, this line of code can be changed to window.open("target.htm","_blank") just see the difference in output if you change.

Note that if the parameter ”_blank” is not provided then the functionality will differ between IE and Firefox, in firefox a new tab will be opened instead of a new window. Just use window.open("target.htm") and see the difference yourself. For more info about window.open() method and its parameters, see http://msdn2.microsoft.com/en-us/library/ms536651(VS.85).aspx. Also note, in , the id attribute is necessary, then only the code of target.htm(shown below) will execute in 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>

Hope you can get what is going on in the above code, in the new window(target.htm) we are using the window.opener object to access the text box present in the parent window(openwindow.htm). You just need to prefix “window.opener.” and write the same code that you will write in the parent window’s HTML page to access its elements.

Updated Answer

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;

so in this way you will have the parent page value in the page load of child.

瑶笙 2024-11-07 13:15:49

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

form2.aspx?hdnFieldValue=xxx

You could pass the value of hdnField as a querystring parameter of form2.aspx when you open the popup.

form2.aspx?hdnFieldValue=xxx
萌辣 2024-11-07 13:15:49

当打开表单 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;

You could store the value of the field from form 1 in a Session Variable when form 2 is opened you can then access it from the session.

you could create a base page with a property that uses the session and make both pages inheriet from it.

eg.

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;
   }
 }

Then from page one button click.

this.YourProperty = value;
//Open window

on page 2.

txtTextBox.Text = this.YourProperty;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文