Passing data from popup to parent window using session variables

Rate It (1)

Last post 03-28-2009 1:44 PM by nikhil20980. 16 replies.

Sort Posts:

  • Passing data from popup to parent window using session variables

    08-27-2008, 1:44 AM
    • Member
      2 point Member
    • ManjunathMR
    • Member since 07-31-2008, 10:01 AM
    • Posts 47

     Hi,

    I want to pass data from the popup to the parent window using session variables.

    The scenario is this. When I click on the button in the parent window i want a child window to popup. I enter the value in the textbox in the popup and click the button in the popup. This should close the popup and I should be able to access the value entered in the popup textbox in the codebehind(.cs) click event of the button in parent.

     

    How can I do this. Please reply asap.

    Thanks,

    Manjunath M. R.
     

  • Re: Passing data from popup to parent window using session variables

    08-27-2008, 3:44 AM
    • Contributor
      2,861 point Contributor
    • bhadelia.imran
    • Member since 08-04-2008, 6:59 AM
    • India
    • Posts 477

    If you just need that value in parent then you can directly do that, no need to create session, but if you want to run some code then parent page must get refresh.

    in parent

    var parentValue;

    and in child you can assign this value before closing the window by

    window.parent.parentValue = 'any value'

    this way you can get the value in parent page

     

    Imran
    [MCTS]
    Born to fly High
    http://knowledgebaseworld.blogspot.com/
  • Re: Passing data from popup to parent window using session variables

    08-27-2008, 3:57 AM
    • Contributor
      5,079 point Contributor
    • NHOQUE
    • Member since 04-02-2008, 9:00 AM
    • Kumamoto, Japan
    • Posts 849

    In Parent window,

    create an element like this,

    <input id="parentID" value="" runat="server">

    In Child window,

    using javascript,

    window.opener.document.getElementById("parentID")= document.getElementById("your_TextBox_ID_in_Child_window");

     

    then in parent page.,in the codebhind

    you can achieve it by,

    String myPopupData=parentID.value;

    Thanks.

    HOQUE MD.NAZMUL
    [document.getReaders]
  • Re: Passing data from popup to parent window using session variables

    08-27-2008, 9:43 AM
    • Member
      404 point Member
    • satish4asp
    • Member since 05-15-2008, 10:34 AM
    • Posts 72

    Hi,
    Try with the following...
    If the parent page is like this then......
    <form id="form1" runat="server">
        <div>
        <input type="text" id="txtParentBox" runat="server" /> <br />
        <input type="button" id="btnOpenChild" value="Open" runat="server" onclick="PopupOpen();" onserverclick="btnOpenChild_click" />   
        </div>
        </form>

    and Javascript function in parent form ....
    function PopupOpen()
        {
            var sReturnvalue,vUrl;
            var vWidth, vHeight;
            vWidth = 250;
      vHeight = 200;
      vUrl="Child.aspx";
            sReturnvalue=window.showModalDialog(vUrl, this, 'dialogwidth:300px;dialogHeight:125px;dialogLeft:' + (((screen.width - vWidth) / 2) + 200) + ';dialogTop:' + (((screen.height - vHeight) / 2) + 100) + ';help: No; status:no'+';scroll:No');
            if(sReturnvalue!=null && sReturnvalue!="")
                document.getElementById('txtParentBox').value=sReturnvalue;  
        }

    then codebehind of Parent Page
    protected void btnOpenChild_click(object sender, EventArgs e)
        {
            string str;
            str = txtParentBox.Value;
        }

    This is the child page...
    <form id="form1" runat="server">
        <div>
         <input type="text" id="txtchild" runat="server" /> <br />
        <input type="button" id="btnOpenChild" runat="server" value="Close" onclick="return PopupClose();return false;"  />
        </div>
        </form>
    and Javascript function in child page....
    function PopupClose()
        {
            var sValue=document.getElementById('txtchild').value;
            if(sValue!=null && sValue!="")
            {
                window.returnValue = sValue;
                window.close();
            }  
        }

     If I misunderstood your problem ,feel free to let us know.

    Regards,

    Satish.

     

  • Re: Passing data from popup to parent window using session variables

    08-27-2008, 9:53 AM
    • Member
      2 point Member
    • ManjunathMR
    • Member since 07-31-2008, 10:01 AM
    • Posts 47

    Hi,

    Thanks for the reply. What u said is right. But the problem is that I should be able to access the value in the popup in the same button event that caused the popup on the client side.

    What I mean is I want to access the value set in the child popup in the butt0n_click event in the parent.aspx.cs .

    I'm using the following approach.

    1. When the user clicks on the button I have the following java script executed on the client side

    function openwin()

    {

    window.open(........);

    }

    In the click event

    button_click(....)

    {

    t1.text= Request["hidden1"];

    }

    in the child page

    I have a text box t2 and button b

    in the button b clientclickevent I have the followin javascript

    function set()

    {

    window.opener.document.getelementbyid('hidden1').value=document.getelementbyid('t2').value;

    window.close();

    }

     

    The problem Is that the value of t1 gets updated when I click the button for the second time.

    What's the solution for the value to get updated on the first click?

    Thanks,

    Manjunath M. R.

     

     

  • Re: Passing data from popup to parent window using session variables

    08-27-2008, 10:49 AM
    • Member
      2 point Member
    • ManjunathMR
    • Member since 07-31-2008, 10:01 AM
    • Posts 47

    Hi,

    Thanks for the reply. In the javascript i'm using i get only the option for window.open() and not for window.showmodaldialog().

    What may be the reason? Does it come for only ajax enabled website? Mine is not.

    Thanks,

    Manjunath M. R.

  • Re: Passing data from popup to parent window using session variables

    08-27-2008, 11:16 AM
    Answer
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 13,868
    • TrustedFriends-MVPs

    Here's a complete sample. Try this.

    ParentWindow.aspx:

    <form id="Form1" method="post" runat="server">
     <input type="button" onclick="window.open('ChildWindow.aspx');" value="Open Child Window">
    </form>

    ParentWindow.aspx.cs:

    private void Page_Load(object sender, System.EventArgs e)
    {
     // Insure that the __doPostBack() JavaScript method is created...
     this.GetPostBackEventReference(this, string.Empty);

     if ( this.IsPostBack )
     {
      string eventTarget = (this.Request["__EVENTTARGET"] == null) ? string.Empty : this.Request["__EVENTTARGET"];

      if ( eventTarget == "ChildWindowPostBack" )
       Response.Write("Session: [" + this.Session["ChildWindowKey"] + "]<br>");
     }
     else
     {
     }
    }

    ChildWindow.aspx:

    <form id="Form1" method="post" runat="server">
     <asp:button id="postBackButton" onclick="postBackButton_Click" runat="server" text="Post Back"></asp:button>
    </form>

    <script type="text/javascript">
    <!--
    function closeWindow()
    {
     window.opener.__doPostBack('ChildWindowPostBack', '');
     window.close();
    }
    // -->
    </script>

    ChildWindow.aspx.cs:

    protected void postBackButton_Click(object sender, EventArgs e)
    {
     this.Session["ChildWindowKey"] = "Value from child window";

     this.RegisterStartupScript("CloseWindowScript", "<script type='text/javascript'>closeWindow();</script>");
    }

    Note that RegisterOnSubmitStatement, RegisterStartupScript, RegisterClientScriptBlock, etc have changed since version 1.1 and you will get a compiler warning with the above.
    See http://msdn2.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.aspx for more information.

    Also, if using AJAX, you must substitute a ScriptManager object call for the ClientScript object call. See http://tech-review.org/blogs/.net_2.0/archive/2007/02/08/lets-talk-scriptmanger.aspx for more information.
     

    BTW, showModalDialog is only available in Internet Explorer.

    NC...

  • Re: Passing data from popup to parent window using session variables

    08-28-2008, 2:02 AM
    • Member
      404 point Member
    • satish4asp
    • Member since 05-15-2008, 10:34 AM
    • Posts 72

    Hi All,

    First of all let me explain about my previous post.

    As everybody knows Client script executes first then only follows serverside code.

    By using window.showModalDialog for open child popup ,it waits for that chld return value and then executes remaining client side code and then server side exode executes for that button.

    As NC01 told that window.showModalDialog work only for IE and that posted solution was good.

    Also Iam suggesting a simle way without using sessions

    Look at the following also...

    ParentPage is like this...

    <form id="form1" runat="server">
        <div>
        <input type="hidden" id="hidden1" runat="server" /> <br />
        <input type="button" id="btnOpenChild" value="Open" runat="server" onclick="return window.open('Child.aspx');return false;"  />   
        <input type="button" id="btnhidOpenChild" runat="Server" style="display:none" onserverclick="btnOpenChild_click" />
        </div>
        </form>

    Code behind for ParentPage..

    protected void btnOpenChild_click(object sender, EventArgs e)
        {
            string str;
            str = this.hidden1.Value;
            Response.Write(str);
        }

    and Child Page ...

    <form id="form1" runat="server">
        <div>
         <input type="text" id="txtchild" runat="server" /> <br />
        <input type="button" id="btnOpenChild" runat="server" value="Close" onclick="return PopupClose();return false;"  />
        </div>
        </form>

     Javascript for ChildPage..

    function PopupClose()
        {
            var sValue=document.getElementById('txtchild').value;
            if(sValue!=null && sValue!="")
            {           
                  window.opener.document.getElementById('hidden1').value=sValue;           
                  window.opener.document.getElementById('btnhidOpenChild').click();
                  window.close();
            }  
        }

    Nothing is new here...I just called that Parentpage button click event in child page...

    Let us know if You have any concerns on this...

    Regards,

    Satish

     

  • Re: Passing data from popup to parent window using session variables

    08-28-2008, 7:28 AM
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 13,868
    • TrustedFriends-MVPs

    window.opener.document.getElementById('btnhidOpenChild').click();

    This does not work in all browsers. In some browsers you will get a security violation instead.

    NC...

     

  • Re: Passing data from popup to parent window using session variables

    08-28-2008, 9:38 AM
    • Member
      404 point Member
    • satish4asp
    • Member since 05-15-2008, 10:34 AM
    • Posts 72

    Hi NC01,

    Thank you for your valuable suggessions,and I follow your posts regularly,It was really so much impressing.

    If possible let me clarify a few things on this...

    1) How should we know  whether the piece of code works for all browsers (Browser compatibility issues)

    For ex: event.keyCode does n't work  for Mozilla (like this....)

    Is there any simple way to find out that code will works for all browsers or not ?

    2)What are the diff ways for calling code behind methods from client side?

    Presently I know Calling Ajax static methods,_doPostBack('','')..Raisepostback event ,ICallBackEvent and click() event..

    I think this would be useful for others also....

    Thanks in advance..

    Regards,

    Satish. 

     

  • Re: Passing data from popup to parent window using session variables

    08-28-2008, 10:21 AM
    Answer
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 13,868
    • TrustedFriends-MVPs

    You need to code for browser compatibility issues. Test for the object before calling it. For example:
         var keyStroke = (event.which) ? event.which : (window.event) ? window.event.keyCode : -1;

    As for calling server-side code from the client, there are really only 2 ways. A full PostBack using __doPostBack(targ, arg) or a partial PostBack (AJAX). The methods that you mentioned are really offshoots of AJAX. A Google search should tell you all that you want to know about any of them.

    NC...

  • Re: Passing data from popup to parent window using session variables

    03-26-2009, 8:03 AM
    • Member
      12 point Member
    • nikhil20980
    • Member since 03-26-2009, 7:58 AM
    • Posts 6

     Hi NC01,

     Is there a way to implement this with ModalDialog?

     I tried but it throws window.opener is NULL or not an object.

    Can't get it throgh. Works fine with the normal popup but my requirement is a Modal popup window.

     

  • Re: Passing data from popup to parent window using session variables

    03-26-2009, 9:02 AM
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 13,868
    • TrustedFriends-MVPs

    The window.opener does NOT work if you use window.showModalDialog to open the window. Read the previous posts in this thread. Here's an example of what you need to do:

    Parent.aspx:

    <form id="Form1" method="post" runat="server">
     <input type="button" onclick="openDialog();" value="Open">
    </form>

    <script type="text/javascript">
    <!--
    function openDialog()
    {
     var dialogFeatures =
      'dialogHeight: 600px; dialogWidth: 800px; help: no; center: yes; resizable: no; scroll: no; ' +
      'edge: sunken; ' + // raised
      'status: no; ' +
      'unadorned: yes; ';
      // The status and unadorned properties only work when the dialog is opened from a trusted application
     var dialogReturn = window.showModalDialog('Dialog.aspx', null, dialogFeatures);

     if ( !dialogReturn )
     {
      // Dialog closed by the "X" closer or no value sent...
      alert('NO return from dialog');
     }
     else
     {
      //alert('Return from dialog: ' + dialogReturn);
      __doPostBack('ReturnFromDialogPostBack', dialogReturn);
     }
    }
    // -->
    </script>

    Parent.aspx.cs:

    private void Page_Load(object sender, System.EventArgs e)
    {
     // Insure that the __doPostBack() JavaScript method is created...
     this.GetPostBackEventReference(this, string.Empty);

     if ( this.IsPostBack )
     {
      string eventTarget = (this.Request["__EVENTTARGET"] == null) ? string.Empty : this.Request["__EVENTTARGET"];
      string eventArgument = (this.Request["__EVENTARGUMENT"] == null) ? string.Empty : this.Request["__EVENTARGUMENT"];

      if ( eventTarget == "ReturnFromDialogPostBack" )
       this.Session["TestSessionObject"] = eventArgument;
     }
     else
     {
     }

     string testSessionObject = (this.Session["TestSessionObject"] == null) ? string.Empty : this.Session["TestSessionObject"].ToString();
     this.Response.Write("TestSessionObject: [" + testSessionObject + "]<br>");
       
    }

    Dialog.aspx
    (Using the value from the TextBox to return to the parent as the new Session value):

    <form id="Form1" method="post" runat="server">
     <asp:textbox id="TextBox1" runat="server"></asp:textbox>
     <input type="button" onclick="closeDialog();" value="Close">
    </form>

    <script type="text/javascript">
    <!--
    function closeDialog()
    {
     
     window.returnValue = document.getElementById('<%= TextBox1.ClientID %>').value;
     window.close();
    }
    // -->
    </script>

    NC...

     

  • Re: Passing data from popup to parent window using session variables

    03-26-2009, 9:59 AM
    • Member
      12 point Member
    • nikhil20980
    • Member since 03-26-2009, 7:58 AM
    • Posts 6

     I get object required error now.

    Also i could not find a window.returnValue. Embarrassed

  • Re: Passing data from popup to parent window using session variables

    03-26-2009, 10:23 AM
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 13,868
    • TrustedFriends-MVPs

    The example that I posted was tested and works witout errors. It will only work in Internet Explorer however, since it uses window.showModalDialog and window.returnValue.

    NC...

     

Page 1 of 2 (17 items) 1 2 Next >