Javascript works fine until an asp.net button pushed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tshad

    #1

    Javascript works fine until an asp.net button pushed

    I posted this on the asp.net group, also. I wasn't sure whether this was an
    asp.net problem or a javascript problem.

    I have a page that was originally created from a program I found on the net
    that works well as an html page.

    It brings up a modal popup window that I have been trying to work out for
    days now and this was the closest I have been able to come.

    I added a little asp.net code and an asp.net button and cannot get it to
    work with that button. It seems to break the javascript code.

    The page works fine with or without the asp.net code (as long as the asp.net
    button is not pushed).

    This works fine (as before) until I press the asp.net button. I am opening
    alert boxes in various locations that all fire until the asp.net button is
    pushed. From that time, all links and buttons will bring up the alert box
    in the first javascript funtion, but the ModalDialogShow never gets called
    or it just ignores it and therefore the popup window never gets called.

    Why would the asp.net button cause the Javascript to quit functioning?

    Why does the popup window not close when the buttons or links are pressed on
    them?

    *************** *************** *************** *************** *************** ***********
    <%@ Page Language="VB" trace="false" debug="true" AutoEventWireup ="true"
    ContentType="te xt/html" ResponseEncodin g="iso-8859-1" %>
    <html>
    <script language=JavaSc ript>

    var ModalDialogWind ow;
    var ModalDialogInte rval;
    var ModalDialog = new Object;

    ModalDialog.val ue = '';
    ModalDialog.eve nthandler = '';


    function ModalDialogMain tainFocus()
    {
    try
    {
    if (ModalDialogWin dow.closed)
    {
    alert("at ModalDialogWind ow.closed");
    window.clearInt erval(ModalDial ogInterval);
    eval(ModalDialo g.eventhandler) ;
    return;
    }
    ModalDialogWind ow.focus();
    }
    catch (everything) { }
    }

    function ModalDialogRemo veWatch()
    {
    ModalDialog.val ue = '';
    ModalDialog.eve nthandler = '';
    }

    function ModalDialogShow (Title,BodyText ,Buttons,EventH andler)
    {
    alert("inside ModalDialogShow ");

    ModalDialogRemo veWatch();
    ModalDialog.eve nthandler = EventHandler;

    var args='width=350 ,height=125,lef t=325,top=300,t oolbar=0,';
    args+='location =0,status=0,men ubar=0,scrollba rs=1,resizable= 0';

    ModalDialogWind ow=window.open( "","",args) ;
    ModalDialogWind ow.document.ope n();
    ModalDialogWind ow.document.wri te('<html>');
    ModalDialogWind ow.document.wri te('<head>');
    ModalDialogWind ow.document.wri te('<title>' + Title + '</title>');
    ModalDialogWind ow.document.wri te('<script' + ' language=JavaSc ript>');
    ModalDialogWind ow.document.wri te('function CloseForm(Respo nse) ');
    ModalDialogWind ow.document.wri te('{ ');
    ModalDialogWind ow.document.wri te(' window.opener.M odalDialog.valu e =
    Response; ');
    ModalDialogWind ow.document.wri te(' window.close(); ');
    ModalDialogWind ow.document.wri te('} ');
    ModalDialogWind ow.document.wri te('</script' + '>');
    ModalDialogWind ow.document.wri te('</head>');
    ModalDialogWind ow.document.wri te('<body onblur="window. focus();">');
    ModalDialogWind ow.document.wri te('<table border=0 width="95%"
    align=center cellspacing=0 cellpadding=2>' );
    ModalDialogWind ow.document.wri te('<tr><td align=left>' + BodyText +
    '</td></tr>');
    ModalDialogWind ow.document.wri te('<tr><td align=left><br> </td></tr>');
    ModalDialogWind ow.document.wri te('<tr><td align=center>' + Buttons +
    '</td></tr>');
    ModalDialogWind ow.document.wri te('</body>');
    ModalDialogWind ow.document.wri te('</html>');
    ModalDialogWind ow.document.clo se();
    ModalDialogWind ow.focus();
    ModalDialogInte rval = window.setInter val("ModalDialo gMaintainFocus( )",5);

    }

    </script>

    <script language=JavaSc ript>


    function YesNoCancel(Bod yText,EventHand ler)
    {
    alert("inside YesNoCancel")
    var Buttons='';
    // Buttons = '<a href=javascript :CloseForm("Yes ");>Yes</a> ';
    // Buttons += '<a href=javascript :CloseForm("No" );>No</a> ';
    // Buttons += '<a href=javascript :CloseForm("Can cel");>Cancel</a> ';
    Buttons = '<BUTTON onclick="javasc ript:CloseForm( \'Yes\');"
    type="button">Y es</BUTTON> ';
    Buttons += '<BUTTON onclick="javasc ript:CloseForm( \'No\');" type =
    "button">No </BUTTON> ';
    Buttons += '<BUTTON onclick="javasc ript:CloseForm( \'Cancel\');" type =
    "button">Cancel </BUTTON> ';
    ModalDialogShow ("Dialog",BodyT ext,Buttons,Eve ntHandler);
    }

    function YesNoMaybe(Body Text,EventHandl er)
    {
    var Buttons='';
    Buttons = '<a href=javascript :CloseForm("Yes ");>Yes</a> ';
    Buttons += '<a href=javascript :CloseForm("No" );>No</a> ';
    Buttons += '<a href=javascript :CloseForm("May be");>Maybe</a> ';
    ModalDialogShow ("Dialog",BodyT ext,Buttons,Eve ntHandler);
    }

    function YesNoCancelRetu rnMethod()
    {
    alert("inside YesNoCancelRetu rnMethod");
    document.getEle mentById('modal return1').value = ModalDialog.val ue;
    ModalDialogRemo veWatch();
    }

    function YesNoMaybeRetur nMethod()
    {
    document.getEle mentById('modal return2').value = ModalDialog.val ue;
    ModalDialogRemo veWatch();
    }

    </script>

    <script runat="server">
    sub page_load(s as object, e as eventArgs)
    if not isPostBack then
    Button1.Attribu tes.Add("onclic k", "YesNoCancel('Y es, no, or cancel
    me','YesNoCance lReturnMethod() ');")
    end if
    end sub
    </script>

    <BODY >

    <table border=1 cellpadding=2 cellspacing=2 align=center width="60%">
    <tr><td align=left></td></tr>
    <tr><td align=left></td></tr>
    <tr><td align=left></td></tr>
    <tr>
    <td align=left><a href="javascrip t:YesNoCancel(' Yes, no, or cancel
    me','YesNoCance lReturnMethod() ');">Show Modal #1</a>
    1. <input type=text id=modalreturn1 name=modalretur n1
    value=''></td>
    </tr>
    <tr>
    <td align=left><a href="javascrip t:YesNoMaybe('Y es, no, or maybe
    me','YesNoMaybe ReturnMethod()' );">Show Modal #2</a>
    2. <input type=text id=modalreturn2 name=modalretur n2
    value=''></td>
    </tr>
    <tr>
    <td>
    <BUTTON onclick="javasc ript:YesNoMaybe ('Yes, no, or maybe
    me','YesNoMaybe ReturnMethod()' );" type="button">O pen Child Window</BUTTON>
    </td>
    </tr>
    </table>
    <form runat="server">
    <asp:Button ID="Button1" text="submit" runat="server"/>
    </form>
    </BODY>
    </HTML>
    *************** *************** *************** *************** *************** *********

    Thanks,

    Tom


  • kaeli

    #2
    Re: Javascript works fine until an asp.net button pushed

    In article <wd%%d.405$zl.3 36@newssvr13.ne ws.prodigy.com> ,
    tscheiderich@ft solutions.com enlightened us with...[color=blue]
    >
    > The page works fine with or without the asp.net code (as long as the asp.net
    > button is not pushed).
    >[/color]

    My guess, not having .net to test this with...

    When the button is pushed, the page is submitted to the server and then comes
    back to the client, which runs all the javascript all over again.

    (put an alert("boom") or something all the way at the beginning and see if it
    gets shown twice)

    When that happens, the handle to the child window is lost, as all client-side
    variables lose their previous values.
    That means your child window is calling code that doesn't exist and your
    parent window has no reference to the original child. When the child hits the
    error (window.opener. ModalDialog.val ue -- there is no object reference any
    more, so there can be no value property), all script stops functioning.
    Hence, it won't even self.close.

    HTH

    --
    --
    ~kaeli~
    Local Area Network in Australia:... the LAN down under.



    Comment

    • Brian Munroe

      #3
      Re: Javascript works fine until an asp.net button pushed

      Well, for one thing all the Javascript has to be properly terminated
      and on one line. Hopefully it is just the way it is posted in the
      newsgroup, because if I try and run it cut and paste, I get a bunch of
      ''unterminated string literal' errors. I am still fix all those
      firstly.

      (also, I find the Firefox javascript console 100x more powerful then IE
      script dialog error box)

      Comment

      • Brian Munroe

        #4
        Re: Javascript works fine until an asp.net button pushed

        I just straighted out all the Javascript as I mentioned above and it
        works fine for me using ASP.NET 1.1 and Cassini (in Firefox 1.1 and IE
        6)

        Comment

        • Brian Munroe

          #5
          Re: Javascript works fine until an asp.net button pushed

          Are you getting any Javascript errors? I am trying it in IE 6, Firefox
          using ASP.NET and Cassini - it seems to work fine, but I did have to
          unwrap some of the JavaScript due to what I am assuming is the fault of
          the newsreader.

          I've found that the JavaScript console in Firefox is 100x more verbose
          then IEs script error dialog, so if you are getting a javascript error,
          try looking there.

          Also, since the popup window doesn't have a status bar, you may be
          getting an error and not even know it.

          Comment

          • tshad

            #6
            Re: Javascript works fine until an asp.net button pushed

            "Brian Munroe" <brian.e.munroe @gmail.com> wrote in message
            news:1111596081 .055774.281340@ l41g2000cwc.goo glegroups.com.. .[color=blue]
            > Well, for one thing all the Javascript has to be properly terminated
            > and on one line. Hopefully it is just the way it is posted in the
            > newsgroup, because if I try and run it cut and paste, I get a bunch of
            > ''unterminated string literal' errors. I am still fix all those
            > firstly.
            >
            > (also, I find the Firefox javascript console 100x more powerful then IE
            > script dialog error box)[/color]

            That doesn't matter, unfortunately, as I need to use one that works on all
            the browsers.

            Tom


            Comment

            • Brian Munroe

              #7
              Re: Javascript works fine until an asp.net button pushed

              I'm not saying anything about cross-browser compatiblity, just that the
              Firefox javascript console is more verbose if by chance you are getting
              javascript errors.

              Comment

              • Brian Munroe

                #8
                Re: Javascript works fine until an asp.net button pushed

                You know, the way you explained it in the OP makes it sound like once
                you click the ASP.NET button, 'everything' stops working, but what I
                think you mean 'when you click the ASP.NET button to launch the
                dialog, the return value is lost'?

                Well, kaeli is correct because the ASP.NET button is posting back and
                all the JavaScript is reinitialized and the parent window knows nothing
                of the dialog at this point.

                As far as a work-around:

                1) don't use an ASP.NET control
                2) add a 'return false;' to the onclick binding to prevent the button
                from submitting:

                sub page_load(s as object, e as eventArgs)
                if not isPostBack
                Button1.Attribu tes.Add("onclic k", "YesNoCancel('Y es, no, or cancel
                me','YesNoCance lReturnMethod() '); return false;")
                end if
                end sub

                Comment

                • tshad

                  #9
                  Re: Javascript works fine until an asp.net button pushed

                  "Brian Munroe" <brian.e.munroe @gmail.com> wrote in message
                  news:1111616792 .439040.222420@ l41g2000cwc.goo glegroups.com.. .[color=blue]
                  > You know, the way you explained it in the OP makes it sound like once
                  > you click the ASP.NET button, 'everything' stops working, but what I
                  > think you mean 'when you click the ASP.NET button to launch the
                  > dialog, the return value is lost'?
                  >
                  > Well, kaeli is correct because the ASP.NET button is posting back and
                  > all the JavaScript is reinitialized and the parent window knows nothing
                  > of the dialog at this point.
                  >
                  > As far as a work-around:
                  >
                  > 1) don't use an ASP.NET control
                  > 2) add a 'return false;' to the onclick binding to prevent the button
                  > from submitting:[/color]

                  My problem is that I am trying to set up a popup window that asks the user
                  what they want to do based on something happening on the page. It is not
                  just a "do you really want to delete this" type of question where you can
                  use the standard "confirm" window. The problem with that is you either go
                  on or do nothing. I need to know whether to do one thing or another in my
                  asp page. I need to save the value somewhere and then postback to handle the
                  situation. If I don't use an asp.net control, I am not going to post back.
                  [color=blue]
                  >
                  > sub page_load(s as object, e as eventArgs)
                  > if not isPostBack
                  > Button1.Attribu tes.Add("onclic k", "YesNoCancel('Y es, no, or cancel
                  > me','YesNoCance lReturnMethod() '); return false;")
                  > end if
                  > end sub[/color]

                  I thought you said that you made a couple of changes and it was working for
                  you?

                  I made a bunch of changes and am trying this with RegisterClientS criptBlock
                  to see if that works better and it seems to. But I still lose in on
                  postback, as we mentioned before.

                  I have the page in:


                  The page goes all the way through in IE and you can see the textbox getting
                  the value YES after the alert box that says:

                  "inside YesNoCancelRetu rnMethod"

                  Then it disapears when the page is posted back.

                  In Mozilla, I only get to the if test where it is checking if the window is
                  closed - "ModalDialogWin dow.closed" and I get an error:

                  popupScript += "function ModalDialogMain tainFocus()"
                  popupScript += "{"
                  popupScript += " try"
                  popupScript += " {"
                  popupScript += " alert(""in ModalDialogMain tainFocus"");"
                  popupScript += " if (ModalDialogWin dow.closed)"

                  This goes to the Catch clause and gives me:

                  Type Error: ModalDialogWind ow has no properties

                  And then it dies and window is still open. Not sure why Mozilla has this
                  problem.

                  Here is the whole page:

                  *************** *************** *************** *************** ***************
                  <%@ Page Language="VB" trace="true" debug="true" AutoEventWireup ="true"
                  ContentType="te xt/html" ResponseEncodin g="iso-8859-1" %>
                  <html>
                  <head>
                  <script language="VB" runat="server">
                  Sub Page_Load( sender as Object,e as EventArgs)
                  trace.warn("sho w = " & show.value)
                  if not IsPostBack then
                  Button1.Attribu tes.Add("onclic k", "YesNoCancel(); ")
                  end if

                  'Form the script that is to be registered at client side.'
                  Dim scriptString as String = "<script language=JavaSc ript> function
                  DoClick() {"
                  scriptString += "myForm.show.va lue='Welcome to Microsoft .NET'}<"
                  scriptString += "/"
                  scriptString += "script>"

                  If(Not IsClientScriptB lockRegistered( "clientScript") )
                  RegisterClientS criptBlock("cli entScript", scriptString)
                  End If

                  Dim popupScript As String = "<script language='javas cript'>"
                  popupScript += "var ModalDialogWind ow;"
                  popupScript += "var ModalDialogInte rval;"
                  popupScript += "var ModalDialog = new Object;"
                  popupScript += "ModalDialog.va lue = 'The Value';"
                  popupScript += "ModalDialog.ev enthandler = '';"
                  popupScript += "function ModalDialogMain tainFocus()"
                  popupScript += "{"
                  popupScript += " try"
                  popupScript += " {"
                  popupScript += " alert(""in ModalDialogMain tainFocus"");"
                  popupScript += " if (ModalDialogWin dow.closed)"
                  popupScript += " {"
                  popupScript += " alert(""at window.clearInt erval"");"
                  popupScript += " window.clearInt erval(ModalDial ogInterval);"
                  popupScript += " alert(""Before ModalDialog.eve nthandler"");"
                  popupScript += " eval(ModalDialo g.eventhandler) ;"
                  popupScript += " alert(""after ModalDialog.eve nthandler"");"
                  popupScript += " return;"
                  popupScript += " }"
                  popupScript += " alert(""Before ModalDialogWind ow.focus"");"
                  popupScript += " ModalDialogWind ow.focus(); "
                  popupScript += " }"
                  popupScript += " catch (everything) { alert(everythin g); }"
                  popupScript += "}"
                  popupScript += "function ModalDialogRemo veWatch()"
                  popupScript += "{"
                  popupScript += " ModalDialog.val ue = '';"
                  popupScript += " ModalDialog.eve nthandler = '';"
                  popupScript += "}"
                  popupScript += "function ModalDialogShow (Title,Buttons) "
                  popupScript += "{"
                  popupScript += " alert(""inside YesNoCancel""); "
                  popupScript += " ModalDialogRemo veWatch();"
                  popupScript += " alert(""Before ModalDialogWind ow open"");"
                  popupScript += " ModalDialog.eve nthandler =
                  'YesNoCancelRet urnMethod()';"
                  popupScript += " var Title='Test Pop Up Window'; "
                  popupScript += " var
                  args='width=350 ,height=125,lef t=325,top=300,t oolbar=0,';"
                  popupScript += "
                  args+='location =0,status=0,men ubar=0,scrollba rs=1,resizable= 0'; "
                  popupScript += " if (window.opener && !window.opener. closed)"
                  popupScript += " window.opener.d ocument.bgColor = ""beige"";"
                  popupScript += " ModalDialogWind ow = window.open('', '', args);"
                  popupScript += " ModalDialogWind ow.document.wri te('<html>');"
                  popupScript += " ModalDialogWind ow.document.wri te('<head>'); "
                  popupScript += " ModalDialogWind ow.document.wri te('<title>' + Title +
                  '</title>');"
                  popupScript += " ModalDialogWind ow.document.wri te('<script' + '
                  language=JavaSc ript>');"
                  popupScript += " ModalDialogWind ow.document.wri te('function
                  CloseForm(Respo nse) ');"
                  popupScript += " ModalDialogWind ow.document.wri te('{ ');"
                  popupScript += " ModalDialogWind ow.document.wri te(' alert(""inside
                  CloseForm""); ');"
                  popupScript += " ModalDialogWind ow.document.wri te(' if (window.opener
                  && !window.opener. closed)alert (""window.opene r still valid""); ');"
                  popupScript += " ModalDialogWind ow.document.wri te('
                  window.opener.M odalDialog.valu e = Response; ');"
                  popupScript += " ModalDialogWind ow.document.wri te(' alert(""after
                  setting ModalDialog.val ue and before window.close Response = "" + Response);
                  ');"
                  popupScript += " ModalDialogWind ow.document.wri te(' alert(""before
                  window.close window.opener.M odalDialog.valu e = "" +
                  window.opener.M odalDialog.valu e); ');"
                  popupScript += " ModalDialogWind ow.document.wri te(' window.close();
                  ');"
                  popupScript += " ModalDialogWind ow.document.wri te('} ');"
                  popupScript += " ModalDialogWind ow.document.wri te('</script' + '>');
                  "
                  popupScript += " ModalDialogWind ow.document.wri te('</head>'); "
                  popupScript += " ModalDialogWind ow.document.wri te('<body
                  onblur=""window .focus();"">'); "
                  popupScript += " ModalDialogWind ow.document.wri te(Buttons);"
                  popupScript += " ModalDialogWind ow.document.wri te('</body>');"
                  popupScript += " ModalDialogWind ow.document.wri te('</html>'); "
                  popupScript += " ModalDialogWind ow.document.clo se(); "
                  popupScript += " ModalDialogWind ow.focus(); "
                  popupScript += " alert(""at setInterval ModalDialogWind ow.closed = ""
                  + ModalDialogWind ow.closed);"
                  popupScript += " ModalDialogInte rval =
                  window.setInter val(""ModalDial ogMaintainFocus ()"",5);"
                  popupScript += "}"
                  popupScript += "function YesNoCancel()"
                  popupScript += "{"
                  popupScript += " alert(""inside YesNoCancel "");"
                  popupScript += " var Buttons=''; "
                  popupScript += " Buttons = '<BUTTON
                  onclick=""javas cript:CloseForm (\'Yes\');"" type=""button"" >Yes</BUTTON> ';"
                  popupScript += " ModalDialogShow ('Test Pop Up Window',Buttons );"
                  popupScript += "}"
                  popupScript += "function YesNoCancelRetu rnMethod()"
                  popupScript += "{"
                  popupScript += " alert(""inside YesNoCancelRetu rnMethod"");"
                  popupScript += " document.getEle mentById('theTe xtBox').value = 'Yes';"
                  popupScript += " ModalDialogRemo veWatch();"
                  popupScript += "}"
                  popupScript += "<"
                  popupScript += "/"
                  popupScript += "script>"

                  If(Not IsClientScriptB lockRegistered( "PopupScrip t"))
                  RegisterClientS criptBlock("Pop upScript", popupScript)

                  end if

                  End Sub
                  </script>
                  </head>
                  <body topmargin="20" leftmargin="10" >
                  <form id="myForm" runat="server">
                  <input type="text" id="show" value="a test" style="width=20 0"
                  runat="server">
                  <input type="button" value="ClickMe" onclick="YesNoC ancel();">
                  <asp:Button ID="Button1" text="submit" runat="server"/>
                  <asp:textbox ID="theTextBox " runat="server"/>
                  </form>
                  <input type="text" id="theValue">
                  </body>
                  </html>
                  *************** *************** *************** *************** **************

                  Thanks,

                  Tom


                  Comment

                  • Brian Munroe

                    #10
                    Re: Javascript works fine until an asp.net button pushed

                    tshad wrote:
                    [color=blue]
                    > My problem is that I am trying to set up a popup window that asks the[/color]
                    user[color=blue]
                    > what they want to do based on something happening on the page. It is[/color]
                    not[color=blue]
                    > just a "do you really want to delete this" type of question where you[/color]
                    can[color=blue]
                    > use the standard "confirm" window. The problem with that is you[/color]
                    either go[color=blue]
                    > on or do nothing. I need to know whether to do one thing or another[/color]
                    in my[color=blue]
                    > asp page. I need to save the value somewhere and then postback to[/color]
                    handle the[color=blue]
                    > situation. If I don't use an asp.net control, I am not going to post[/color]
                    back.[color=blue]
                    >[/color]

                    Do you have to use an ASP.NET control? If so, I really don't see a
                    solution.

                    But if all you need to do is submit the form once the user clicks a
                    button in the dialog, you can use javascript to accomplish this:

                    In the CloseForm function, add the submit():

                    window.opener.M odalDialog.valu e = Response;
                    window.opener.d ocument.forms[0].submit();
                    window.close();

                    this will set the ModalDialog value and then submit (postback) the form
                    [color=blue]
                    >
                    > I thought you said that you made a couple of changes and it was[/color]
                    working for[color=blue]
                    > you?
                    >[/color]

                    I misunderstood your post and the changes I made are irrelevant

                    Comment

                    • tshad

                      #11
                      Re: Javascript works fine until an asp.net button pushed

                      "Brian Munroe" <brian.e.munroe @gmail.com> wrote in message
                      news:1111622445 .458948.212830@ o13g2000cwo.goo glegroups.com.. .[color=blue]
                      > tshad wrote:
                      >[color=green]
                      >> My problem is that I am trying to set up a popup window that asks the[/color]
                      > user[color=green]
                      >> what they want to do based on something happening on the page. It is[/color]
                      > not[color=green]
                      >> just a "do you really want to delete this" type of question where you[/color]
                      > can[color=green]
                      >> use the standard "confirm" window. The problem with that is you[/color]
                      > either go[color=green]
                      >> on or do nothing. I need to know whether to do one thing or another[/color]
                      > in my[color=green]
                      >> asp page. I need to save the value somewhere and then postback to[/color]
                      > handle the[color=green]
                      >> situation. If I don't use an asp.net control, I am not going to post[/color]
                      > back.[color=green]
                      >>[/color]
                      >
                      > Do you have to use an ASP.NET control? If so, I really don't see a
                      > solution.[/color]

                      I am not sure. I have to think it through.

                      What I want to do is attach it to one of my buttons if there is a problem,
                      then execute it. It would go to the popup, get a user response (press one
                      of the 2 or 3 buttons) and return with the button that was pushed. Normally
                      this would be some event on the button which would go to a function that
                      would tell me which button was pushed.
                      [color=blue]
                      >
                      > But if all you need to do is submit the form once the user clicks a
                      > button in the dialog, you can use javascript to accomplish this:
                      >
                      > In the CloseForm function, add the submit():
                      >
                      > window.opener.M odalDialog.valu e = Response;
                      > window.opener.d ocument.forms[0].submit();
                      > window.close();[/color]

                      But where is the value at this point?

                      And where do I put it so I can get to it on the postback?

                      We put it in ModalDialog.val ue, but won't I lose it when I run the
                      "window.opener. document.forms[0].submit()"?

                      Also, which event would the Postback go to?

                      Thanks,

                      Tom[color=blue]
                      >
                      > this will set the ModalDialog value and then submit (postback) the form
                      >[color=green]
                      >>
                      >> I thought you said that you made a couple of changes and it was[/color]
                      > working for[color=green]
                      >> you?
                      >>[/color]
                      >
                      > I misunderstood your post and the changes I made are irrelevant
                      >[/color]


                      Comment

                      • Brian Munroe

                        #12
                        Re: Javascript works fine until an asp.net button pushed

                        tshad wrote:
                        [color=blue][color=green]
                        > > window.opener.M odalDialog.valu e = Response;
                        > > window.opener.d ocument.forms[0].submit();
                        > > window.close();[/color]
                        >
                        > But where is the value at this point?
                        >[/color]

                        Crap, the value of what the user 'chose' from the dialog will be stored
                        in the object ModalDialog - which now that I think about it, it might
                        and probably isn't submitted with the form.

                        To fix this, in the parent window, you will have to create a hidden
                        variable to store the user's dialog choice. For example:

                        (Make sure this is inside the <form></form> tag, btw)

                        <input type="hidden" name="modalDial ogValue" value="" />

                        Then in the Dialog window function CloseForm(), instead of what I have
                        above put:

                        window.opener.d ocument.forms[0].modalDialogVal ue.value = Response;
                        window.opener.d ocument.forms[0].submit();
                        window.close();

                        That way, when the form is posted, the value gets included with the
                        form submission. Does that make sense?

                        [color=blue]
                        > And where do I put it so I can get to it on the postback?
                        >[/color]

                        See above
                        [color=blue]
                        > Also, which event would the Postback go to?
                        >[/color]

                        By default, ASP.NET pages are designed to postback to themselves, and
                        then you control what happens by using page events (for example, you
                        would tie an event to an ASP.NET button that on submission, saves the
                        postback data to a database).

                        Once a user picks a choice from the dialog and the parent form is
                        submitted, I am assuming you are processing that choice and directing
                        the user accordingly?

                        True ASP.NET development is completely different then ASP, PHP, JSP,
                        etc. I don't know what experience you have with other web development
                        languages, but I'd suggest picking up a book on ASP.NET to learn the
                        basics of the architecture, it will save you a lot of hassle.

                        -- brian

                        Comment

                        • tshad

                          #13
                          Re: Javascript works fine until an asp.net button pushed

                          First of all, Brian, thanks for all the help.
                          [color=blue]
                          > To fix this, in the parent window, you will have to create a hidden
                          > variable to store the user's dialog choice. For example:
                          >
                          > (Make sure this is inside the <form></form> tag, btw)
                          >
                          > <input type="hidden" name="modalDial ogValue" value="" />
                          >[/color]

                          I did have to actually set it up this way for asp.net to see it:

                          <input type="hidden" name="modalDial ogValue" value=""
                          id="ModalDialog Value" runat="Server"/>

                          You need the ID and runat="Server" for asp.net to see it.

                          I didn't realize at first that you had changed the access to the variable to
                          have document.forms[0] on it, so it took a little futzying (technical term)
                          around until I realized I was missing that. I know you told me it had to be
                          done, but I missed that difference (brain fade).

                          It does work now, which you can see on the samples site (I still have all
                          the alerts in it - but you can see the value in the trace window).

                          http://www.payrollworkshop.com/samples/ScriptTest.aspx (With all the
                          alerts)
                          http://www.payrollworkshop.com/samples/ScriptTest2.aspx (alerts taken out)

                          [color=blue]
                          > Then in the Dialog window function CloseForm(), instead of what I have
                          > above put:
                          >
                          > window.opener.d ocument.forms[0].modalDialogVal ue.value = Response;
                          > window.opener.d ocument.forms[0].submit();
                          > window.close();
                          >
                          > That way, when the form is posted, the value gets included with the
                          > form submission. Does that make sense?[/color]

                          Yes it does. I also made a change to add to the "show" variable which also
                          works.
                          [color=blue]
                          >
                          >[color=green]
                          >> And where do I put it so I can get to it on the postback?
                          >>[/color]
                          >
                          > See above
                          >[color=green]
                          >> Also, which event would the Postback go to?
                          >>[/color]
                          >
                          > By default, ASP.NET pages are designed to postback to themselves, and
                          > then you control what happens by using page events (for example, you
                          > would tie an event to an ASP.NET button that on submission, saves the
                          > postback data to a database).[/color]

                          I understand how events work (what database does it get saved to BTW?). My
                          problem was how to get to the event I want on return. It is really evident
                          in my sample http://www.payrollworkshop.com/samples/ScriptTest2.aspx.

                          If you look at the page you will see that I have the button set as:

                          <asp:Button ID="Button1" text="submit" onClick="SomeFu nction"
                          runat="server"/>

                          So now, when I click the button I should go to the "SomeFuncti on" function
                          on Postback.

                          I also have another function on the button that is put there when the Page
                          is first opened.

                          if not IsPostBack then
                          Button1.Attribu tes.Add("onclic k", "YesNoCancel(); ")
                          end if

                          So now there are 2 functions connected to the button.
                          1. The asp.net function "SomeFuncti on"
                          2. The Javascript function "YesNoCance l()" (although I don't know how
                          asp.net knows that this is a Javascript function - I don't have
                          "Javascript :" in front of it)

                          Both events are firing.

                          The problem is that the postback function is firing first (or at the same
                          time) as the Javascript function. This is easily seen in the trace window.
                          When the popup comes up, you can see the page being posted back and the
                          trace.warn statements in the "SomeFuncti on" function are in the trace
                          window - so that event has already fired (which is really too early).

                          If I were to put a Javascript confirm box on the button, it would get
                          handled before the asp.net function as shown in the


                          The change would be:

                          Button1.Attribu tes.Add("onclic k", "return confirm('Are you sure you want
                          finish this test?');")

                          Also, if you do the first sample ScriptTest.aspx with the alerts, the
                          asp.net function first last (but I assume that has to do with the alert
                          boxes getting in the way).

                          What I am trying to do is

                          1. have the popup come up with a couple of buttons.
                          2. write value to our hidden field that tells me which button was pushed.
                          3. popup window is closed
                          4. Postback is done
                          5. The asp.net function is fired and I handle the buttons. This is
                          important as it also tells me which asp.net button I am dealing with as I
                          could have different buttons on the page with the popup on it.

                          Right now, the function is executed before the user makes a choice.
                          [color=blue]
                          >
                          > Once a user picks a choice from the dialog and the parent form is
                          > submitted, I am assuming you are processing that choice and directing
                          > the user accordingly?[/color]

                          Correct, but as I mentioned above, the function that processes that choice
                          is executed before the Javascript popup is done. In the confirm button, it
                          doesn't get posted until after the confirm button closes - and if the user
                          presses the cancel button, it doesn't post back at all).
                          [color=blue]
                          >
                          > True ASP.NET development is completely different then ASP, PHP, JSP,
                          > etc. I don't know what experience you have with other web development
                          > languages, but I'd suggest picking up a book on ASP.NET to learn the
                          > basics of the architecture, it will save you a lot of hassle.[/color]

                          Quite a bit if experience, actually. Have been working with asp.net for
                          about a year. I also worked with another proprietary program that does much
                          of what asp.net does (but in different ways).

                          I am doing exactly that. I have many books on it (some I have even read).
                          :)

                          The problem is that no how many books you read, you don't get all of it.
                          Many of the books don't get into the ins and outs of the DataGrid, DataList
                          or Repeater and how to deal with finding object on the different
                          datalistitems, for example. They just show you how to use the controls in a
                          basic way. Scott Mitchell's book on the Data Controls is really good and
                          explains these controls real well

                          Here is the page at http://www.payrollworkshop.com/samples/ScriptTest.aspx.

                          *************** *************** *************** *************** *************** *************** *************** ********
                          <%@ Page Language="VB" trace="true" debug="true" AutoEventWireup ="true"
                          ContentType="te xt/html" ResponseEncodin g="iso-8859-1" %>
                          <html>
                          <head>
                          <script language="VB" runat="server">
                          Sub Page_Load( sender as Object,e as EventArgs)
                          trace.warn("sho w = " & show.value)
                          trace.warn("mod alDialogValue = " & modalDialogValu e.value)

                          if not IsPostBack then
                          Button1.Attribu tes.Add("onclic k", "YesNoCancel(); ")
                          end if

                          'Form the script that is to be registered at client side.'
                          Dim scriptString as String = "<script language=JavaSc ript> function
                          DoClick() {"
                          scriptString += "myForm.show.va lue='Welcome to Microsoft .NET'}<"
                          scriptString += "/"
                          scriptString += "script>"

                          If(Not IsClientScriptB lockRegistered( "clientScript") )
                          RegisterClientS criptBlock("cli entScript", scriptString)
                          End If

                          Dim popupScript As String = "<script language='javas cript'>"
                          popupScript += "var ModalDialogWind ow;"
                          popupScript += "var ModalDialogInte rval;"
                          popupScript += "var ModalDialog = new Object;"
                          popupScript += "ModalDialog.va lue = 'The Value';"
                          popupScript += "ModalDialog.ev enthandler = '';"
                          popupScript += "function ModalDialogMain tainFocus()"
                          popupScript += "{"
                          popupScript += " try"
                          popupScript += " {"
                          popupScript += " if (ModalDialogWin dow.closed)"
                          popupScript += " {"
                          popupScript += " window.clearInt erval(ModalDial ogInterval);"
                          popupScript += " eval(ModalDialo g.eventhandler) ;"
                          popupScript += " return;"
                          popupScript += " }"
                          popupScript += " ModalDialogWind ow.focus(); "
                          popupScript += " }"
                          popupScript += " catch (everything) { alert(everythin g); }"
                          popupScript += "}"
                          popupScript += "function ModalDialogRemo veWatch()"
                          popupScript += "{"
                          popupScript += " ModalDialog.val ue = '';"
                          popupScript += " ModalDialog.eve nthandler = '';"
                          popupScript += "}"
                          popupScript += "function ModalDialogShow (Title,Buttons) "
                          popupScript += "{"
                          popupScript += " ModalDialogRemo veWatch();"
                          popupScript += " ModalDialog.eve nthandler =
                          'YesNoCancelRet urnMethod()';"
                          popupScript += " var Title='Test Pop Up Window'; "
                          popupScript += " var
                          args='width=350 ,height=125,lef t=325,top=300,t oolbar=0,';"
                          popupScript += "
                          args+='location =0,status=0,men ubar=0,scrollba rs=1,resizable= 0'; "
                          popupScript += " if (window.opener && !window.opener. closed)"
                          popupScript += " window.opener.d ocument.bgColor = ""beige"";"
                          popupScript += " ModalDialogWind ow = window.open('', '', args);"
                          popupScript += " ModalDialogWind ow.document.wri te('<html>');"
                          popupScript += " ModalDialogWind ow.document.wri te('<head>'); "
                          popupScript += " ModalDialogWind ow.document.wri te('<title>' + Title +
                          '</title>');"
                          popupScript += " ModalDialogWind ow.document.wri te('<script' + '
                          language=JavaSc ript>');"
                          popupScript += " ModalDialogWind ow.document.wri te('function
                          CloseForm(Respo nse) ');"
                          popupScript += " ModalDialogWind ow.document.wri te('{ ');"
                          popupScript += " ModalDialogWind ow.document.wri te('
                          window.opener.d ocument.forms[0].ModalDialogVal ue.value = Response; ');"
                          popupScript += " ModalDialogWind ow.document.wri te('
                          window.opener.d ocument.forms[0].show.value = Response; ');"
                          popupScript += " ModalDialogWind ow.document.wri te('
                          window.opener.d ocument.forms[0].submit();');"
                          popupScript += " ModalDialogWind ow.document.wri te(' window.close();
                          ');"
                          popupScript += " ModalDialogWind ow.document.wri te('} ');"
                          popupScript += " ModalDialogWind ow.document.wri te('</script' + '>');
                          "
                          popupScript += " ModalDialogWind ow.document.wri te('</head>'); "
                          popupScript += " ModalDialogWind ow.document.wri te('<body
                          onblur=""window .focus();"">'); "
                          popupScript += " ModalDialogWind ow.document.wri te(Buttons);"
                          popupScript += " ModalDialogWind ow.document.wri te('</body>');"
                          popupScript += " ModalDialogWind ow.document.wri te('</html>'); "
                          popupScript += " ModalDialogWind ow.document.clo se(); "
                          popupScript += " ModalDialogWind ow.focus(); "
                          popupScript += " ModalDialogInte rval =
                          window.setInter val(""ModalDial ogMaintainFocus ()"",5);"
                          popupScript += "}"
                          popupScript += "function YesNoCancel()"
                          popupScript += "{"
                          popupScript += " var Buttons=''; "
                          popupScript += " Buttons = '<BUTTON
                          onclick=""javas cript:CloseForm (\'Yes\');"" type=""button"" >Yes</BUTTON> ';"
                          popupScript += " ModalDialogShow ('Test Pop Up Window',Buttons );"
                          popupScript += "}"
                          popupScript += "function YesNoCancelRetu rnMethod()"
                          popupScript += "{"
                          popupScript += " document.getEle mentById('theTe xtBox').value = 'Yes';"
                          popupScript += " ModalDialogRemo veWatch();"
                          popupScript += "}"
                          popupScript += "<"
                          popupScript += "/"
                          popupScript += "script>"

                          If(Not IsClientScriptB lockRegistered( "PopupScrip t"))
                          RegisterClientS criptBlock("Pop upScript", popupScript)

                          end if

                          End Sub

                          sub SomeFunction (s as object, e as eventArgs)
                          trace.warn("am in SomeFunction")
                          trace.warn("Mod alDialogValue = " & ModalDialogValu e.value)
                          end sub
                          </script>
                          </head>
                          <body topmargin="20" leftmargin="10" >
                          <form id="myForm" runat="server">
                          <input type="text" id="show" name="show" value="a test"
                          style="width=20 0" runat="server">
                          <input type="button" value="ClickMe" onclick="YesNoC ancel();">
                          <asp:Button ID="Button1" text="submit" onClick="SomeFu nction"
                          runat="server"/>
                          <asp:textbox ID="theTextBox " runat="server"/>
                          <input type="hidden" name="modalDial ogValue" value=""
                          id="ModalDialog Value" runat="Server"/>
                          </form>
                          <input type="text" id="theValue">
                          </body>
                          </html>

                          *************** *************** *************** *************** *************** *************** *************** ***************

                          Thanks,

                          Tom


                          Comment

                          • tshad

                            #14
                            Re: Javascript works fine until an asp.net button pushed

                            "Brian Munroe" <brian.e.munroe @gmail.com> wrote in message
                            news:1111598310 .428886.181230@ l41g2000cwc.goo glegroups.com.. .[color=blue]
                            > Are you getting any Javascript errors? I am trying it in IE 6, Firefox
                            > using ASP.NET and Cassini - it seems to work fine, but I did have to
                            > unwrap some of the JavaScript due to what I am assuming is the fault of
                            > the newsreader.
                            >
                            > I've found that the JavaScript console in Firefox is 100x more verbose
                            > then IEs script error dialog, so if you are getting a javascript error,
                            > try looking there.
                            >
                            > Also, since the popup window doesn't have a status bar, you may be
                            > getting an error and not even know it.[/color]

                            If you run the sample :

                            http://www.payrollworkshop.com/samples/ScriptTest.aspx (With all the
                            alerts)

                            as you go through the alerts you will get the error:

                            TypeError: ModalDialogWind ow has no properties

                            This happens in the ModalDialogMain tainFocus function where it says:

                            popupScript += " alert(""in ModalDialogMain tainFocus"");"
                            popupScript += " if (ModalDialogWin dow.closed)"

                            You get the alert box and then the error - so it is having a problem with
                            ModalDialogWind ow.closed - I assume.

                            This does not happen in IE, but in Mozilla and Netscape it does happen.

                            I don't know about Firefox. I will install it and check it.

                            Thanks,

                            Tom


                            Comment

                            • tshad

                              #15
                              Re: Javascript works fine until an asp.net button pushed

                              "tshad" <tscheiderich@f tsolutions.com> wrote in message
                              news:fl11e.2643 $zl.71@newssvr1 3.news.prodigy. com...[color=blue]
                              > "Brian Munroe" <brian.e.munroe @gmail.com> wrote in message
                              > news:1111598310 .428886.181230@ l41g2000cwc.goo glegroups.com.. .[color=green]
                              >> Are you getting any Javascript errors? I am trying it in IE 6, Firefox
                              >> using ASP.NET and Cassini - it seems to work fine, but I did have to
                              >> unwrap some of the JavaScript due to what I am assuming is the fault of
                              >> the newsreader.
                              >>
                              >> I've found that the JavaScript console in Firefox is 100x more verbose
                              >> then IEs script error dialog, so if you are getting a javascript error,
                              >> try looking there.
                              >>
                              >> Also, since the popup window doesn't have a status bar, you may be
                              >> getting an error and not even know it.[/color]
                              >
                              > If you run the sample :
                              >
                              > http://www.payrollworkshop.com/samples/ScriptTest.aspx (With all the
                              > alerts)
                              >
                              > as you go through the alerts you will get the error:
                              >
                              > TypeError: ModalDialogWind ow has no properties
                              >
                              > This happens in the ModalDialogMain tainFocus function where it says:
                              >
                              > popupScript += " alert(""in ModalDialogMain tainFocus"");"
                              > popupScript += " if (ModalDialogWin dow.closed)"
                              >
                              > You get the alert box and then the error - so it is having a problem with
                              > ModalDialogWind ow.closed - I assume.
                              >
                              > This does not happen in IE, but in Mozilla and Netscape it does happen.
                              >
                              > I don't know about Firefox. I will install it and check it.[/color]

                              I just checked and it works fine as you said. I also checked Mozilla and it
                              works (on the version with no alerts), I noticed Firefox has the same
                              problem with the one with the alerts. IE doesn't have the problem, but it
                              may have something to do with some of the windows that are opening that go
                              behind the actual popup window that I can't get access to once the popup
                              shows up.

                              I still can't figure out how to get the asp.net event to fire after I come
                              back from the popup window event (as it does with the confirm box..

                              It is kind of useless until I figure that one out as I will already have
                              come back from the asp.net event that is doing the processing the javascript
                              request, before the Javascript request is done. You can see it pretty
                              clearly in the http://www.payrollworkshop.com/samples/ScriptTest2.aspx
                              example. The asp.net event has fired when the Javascript popup window first
                              comes up.

                              Tom[color=blue]
                              >
                              > Thanks,
                              >
                              > Tom
                              >[/color]


                              Comment

                              Working...