body onunload problem

Last post 06-03-2006 1:51 PM by NC01. 11 replies.

Sort Posts:

  • body onunload problem

    05-06-2006, 11:10 AM
    • Member
      190 point Member
    • jm76
    • Member since 02-27-2006, 1:05 AM
    • Posts 38

    Hello,

    From Form1, Form2 opens.  I make and update to the DB on Form2 and want the datagrid on Form1 to reflect the updates.  To do so, opener.location.href=opener.location.href; is called from
    body onunload.

    My problem is that when I close Form2 to show the updates in form1, FOrm1 screen seems to flash twice.  Is there anyway to prevent this and just have the updates shown on Form1, without the screen flashing?????

    Below is my code.....

    <body onunload= "DoParentRefresh()" >

    function DoParentRefresh()

    {
            opener.location.href=opener.location.href;
      }

     

     

  • Re: body onunload problem

    05-06-2006, 12:06 PM
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 13,868
    • TrustedFriends-MVPs

    Instead of using opener.location.href=opener.location.href; try opener.__doPostBack('PostBackFromChildWindow', '');

    Just make sure that you add: in C#: GetPostBackEventReference(this, string.Empty) or in VB: GetPostBackEventReference(Me, string.Empty) to the Page_Load event handler of the parent.

    You can then tell that the PostBack was caused by the child window by adding this to the Page_Load of the parent:

    C#
    -----
    if ( this.IsPostBack )
    {
         object eventTarget = this.Request["__EVENTTARGET"];
         object eventArgument = this.Request["__EVENTARGUMENT"];

         if ( (eventTarget != null) &&
              (eventArgument != null) &&
              (eventTarget.ToString().Trim() == "PostBackFromChildWindow") )
         {
              string argumentList =
                   "eventTarget: " + eventTarget.ToString().Trim() + "<br>" +
                   "eventArgument: " + eventArgument.ToString().Trim() + "<br>";

              this.Response.Write(argumentList);
         }
    }

    VB
    -----
    If Me.IsPostBack Then
       Dim eventTarget As Object = Me.Request("__EVENTTARGET")
       Dim eventArgument As Object = Me.Request("__EVENTARGUMENT")
      
       If Not (eventTarget Is Nothing) AndAlso Not (eventArgument Is Nothing) AndAlso eventTarget.ToString().Trim() = "PostBackFromChildWindow" Then
          Dim argumentList As String = "eventTarget: " + eventTarget.ToString().Trim() + "<br>" + "eventArgument: " + eventArgument.ToString().Trim() + "<br>"
         
          Me.Response.Write(argumentList)
       End If
    End If

    NC...

  • Re: body onunload problem

    05-06-2006, 12:09 PM
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 13,868
    • TrustedFriends-MVPs

    BTW, the problem that you're going to encounter is that your onunload event will also fire on every PostBack on the child window also.

    NC...

     

  • Re: body onunload problem

    05-08-2006, 11:02 AM
    • Member
      190 point Member
    • jm76
    • Member since 02-27-2006, 1:05 AM
    • Posts 38

    NC,

    Below is the code I added and it didn't update the parent but displayed the following message:

    EventTarget: PostBackFromChilWindow

    EventArgument:

    Why is the EventArgument blank? Could that be why the parent is NOT getting updated?

    'Child Window

    <

    body onunload= "DoParentRefresh()" >

    function

    DoParentRefresh()

    {

    //opener.location.href=opener.location.href;

    opener.__doPostBack(

    'PostBackFromChildWindow', '');

    }

     

    'Parent Window

    Sub

    Page_Load(Source as Object, E as EventArgs)

    ClientScript.GetPostBackEventReference(

    Me, String.Empty)

    If Me.IsPostBack Then

    Dim eventTarget As Object = Me.Request("__EVENTTARGET")

    Dim eventArgument As Object = Me.Request("__EVENTARGUMENT")

    If Not (eventTarget Is Nothing) AndAlso Not (eventArgument Is Nothing) AndAlso eventTarget.ToString().Trim() = "PostBackFromChildWindow" Then

    Dim argumentList As String = "eventTarget: " + eventTarget.ToString().Trim() + "<br>" + "eventArgument: " + eventArgument.ToString().Trim() + "<br>"

    Me.Response.Write(argumentList)

    End If

    End If

    End Sub

  • Re: body onunload problem

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

    Below is the code I added and it didn't update the parent but displayed the following message:

    EventTarget: PostBackFromChilWindow
    EventArgument:

    If you don't want the message, remove or comment out this line in the parent:
         Me.Response.Write(argumentList)

    Why is the EventArgument blank? Could that be why the parent is NOT getting updated?
    The EventArgument is blank because you're sending an empty string as the second argument to __doPostBack here:
         opener.__doPostBack('PostBackFromChildWindow', '');


    'Child Window

    <body onunload= "DoParentRefresh()" >
    function
    DoParentRefresh()
    {
         //opener.location.href=opener.location.href;
         opener.__doPostBack('PostBackFromChildWindow', '');
    }

    'Parent Window

    The parent is NOT getting updated because you are not doing anything here but writing the arguments sent to the screen.

    Sub Page_Load(Source as Object, E as EventArgs)
         ClientScript.GetPostBackEventReference(Me, String.Empty)

         If Me.IsPostBack Then
              Dim eventTarget As Object = Me.Request("__EVENTTARGET")
              Dim eventArgument As Object = Me.Request("__EVENTARGUMENT")

              If Not (eventTarget Is Nothing) AndAlso Not (eventArgument Is Nothing) AndAlso eventTarget.ToString().Trim() = "PostBackFromChildWindow" Then
                   Dim argumentList As String = "eventTarget: " + eventTarget.ToString().Trim() + "<br>" + "eventArgument: " + eventArgument.ToString().Trim() + "<br>"
                   Me.Response.Write(argumentList)

                   ' *** UPDATE YOUR PAGE HERE ***
              End If
         End If
    End Sub

    NC...

     

  • Re: body onunload problem

    05-10-2006, 10:02 PM
    • Member
      190 point Member
    • jm76
    • Member since 02-27-2006, 1:05 AM
    • Posts 38

    Thanks NC,

    I got this to work but for some reason it is doing the same thing as my original code.  It looks as if the parent form posts back twice when the child form closes.

  • Re: body onunload problem

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

    Well, post the code and I'll take a look.

    NC...

     

  • Re: body onunload problem

    06-02-2006, 1:45 PM
    • Member
      190 point Member
    • jm76
    • Member since 02-27-2006, 1:05 AM
    • Posts 38

    Sorry it has taken me so long to respond.

    Form 1 calls to Form 2.  Form 2 performs an update that should be reflected on the grid in Form1.  When I close form2, it looks like Form1 postbacks but I can see that the update was made to Form1, so it seems like it doesn't need to do this additional postback.  Does that make sense?

    <%

    --onunload will refresh the parent page--%>

    <body onunload="javascript:window.opener.location.href=window.opener.location.href;" >
  • Re: body onunload problem

    06-02-2006, 1:52 PM
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 13,868
    • TrustedFriends-MVPs

    Please re-read through this entire thread. For instance does:
    <body onunload="javascript:window.opener.location.href=window.opener.location.href;" >

    look anything like:
    opener.__doPostBack('PostBackFromChildWindow', '');

    NC...

  • Re: body onunload problem

    06-02-2006, 8:33 PM
    • Member
      190 point Member
    • jm76
    • Member since 02-27-2006, 1:05 AM
    • Posts 38

     Hi NC,

    Sorry to be confusing. I was aware that I didn't paste the code your provided in my last thread.  I tried it and ended up getting the same outcome, so I reverted back to my original code.  Below is the code I used from your example.

    At this point, I think the parent screen is just going to flash (or postback) in order to update the grid on the parent form and there is no other solution.

    I appreciate your help.  Below is the code I used that you helped me with.

    'Child Form

    <body onunload="javascript:opener.__doPostBack('PostBackFromChildWindow', '');" >

    'Parent Form

    Sub

    Page_Load(Source as Object, E as EventArgs)

       If Me.IsPostBack Then

          BindGrid()

       End If

    End Sub
  • Re: body onunload problem

    06-02-2006, 8:39 PM
    • Member
      190 point Member
    • jm76
    • Member since 02-27-2006, 1:05 AM
    • Posts 38

    sorry, this is what my parent form looks like...

    Page_Load(Source as Object, E as EventArgs)

    ClientScript.GetPostBackEventReference(

    Me, String.Empty)

    If Me.IsPostBack Then

    Dim eventTarget As Object = Me.Request("__EVENTTARGET")

    Dim eventArgument As Object = Me.Request("__EVENTARGUMENT")

    If Not (eventTarget Is Nothing) AndAlso Not (eventArgument Is Nothing) AndAlso eventTarget.ToString().Trim() = "PostBackFromChildWindow" Then

    Dim argumentList As String = "eventTarget: " + eventTarget.ToString().Trim() + "<br>" + "eventArgument: " + eventArgument.ToString().Trim() + "<br>"

    ' Me.Response.Write(argumentList)

    BindGrid()

    End If

    End If

    End Sub

  • Re: body onunload problem

    06-03-2006, 1:51 PM
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 13,868
    • TrustedFriends-MVPs

    At this point, I think the parent screen is just going to flash (or postback) in order to update the grid on the parent form and there is no other solution.

    Well, of course it's going to PostBack, that's what the call does. Please take a look at this complete sample of a child window that does a PostBack on it's parent.

    http://forums.asp.net/1241427/ShowThread.aspx#1241427

    NC...

Page 1 of 1 (12 items)