开发者

How to get hiddenfield value in child page on page load event

开发者 https://www.devze.com 2023-02-23 03:47 出处:网络
I have a value in hdnField in form1.a开发者_如何学运维spx and opening a popup page form2.aspx.

I have a value in hdnField in form1.a开发者_如何学运维spx 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?


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");


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.


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

form2.aspx?hdnFieldValue=xxx


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

精彩评论

暂无评论...
验证码 换一张
取 消