[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 789
  • Last Modified:

How to call a JavaScript function from within _Click function

I'm not a Javascript Guru and in fact try to stay away from it other than the Atlas framework.

So if I have this
<!--
      function reload()
      {
            window.opener.top.MainFrame.MessageSubjectFrame.location.reload();
            window.opener.top.MainFrame.document.getElementById("MessageFrame").style.display ='None';
      }
//-->
      </script>

how do I call the reload function in my button's onclick function
    Protected Sub btnAccept_Click(ByVal sender As Object, ByVal e As EventArgs)
        call the reload function in here
    End Sub

0
dba123
Asked:
dba123
  • 25
  • 9
  • 7
2 Solutions
 
jef06Commented:
If you are using Atlas, then you are using .Net 2.0

<asp:Button ID="cmdOne" Text="ASP.NET 2.0 Button" runat="server" OnClientClick="alert('Client side javaScript alert() function called')" />
0
 
TimCotteeCommented:
You cannot do it server-side as this is a client-side script. What you can do is add it to the client-side click event:

For asp.net 1.1:  in page_load use:

btnAccept.Attributes.Add("onclick","reload();")

For asp.net 2.0: set the .onclientclick property to "reload();"
0
 
dba123Author Commented:
ok, but I still want to call my OnClick function...so then yo'ure saying just add the OnClientClick in addition to Onclick or is there a way to d something like this
OnClick="btnAccept_Click; return reload();")"

let me try my hack and see if it works.

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
jef06Commented:
<asp:Button ID="cmdOne" Text="ASP.NET 2.0 Button" runat="server" OnClick="btnAccept_Click" OnClientClick="alert('Client side javaScript alert() function called')" />
0
 
TimCotteeCommented:
The two events are independent. If you call an onclientclick, it will execute and then the onclick event handler will take over after that. Generally this is not a good idea if you are refreshing the same page, however it looks like you are refreshing a parent so that is perfectly ok.
0
 
dba123Author Commented:
Do I need to do it this way or is this just another way

strScript = "reload();"
RegisterStartupScript(strScript)
0
 
dba123Author Commented:
by the way, this call will be made from a pop-up page I'm working on (I hate pop-ups, but not my decision at the momen).  So when the user clicks accept, I want the window to close and then to refresh the parent page is what I'm trying to get working
0
 
TimCotteeCommented:
Does the popup need to close after the postback or do you not need the postback? If you want it to close after the postback then you need to add a bit of script in your btnAccept_Click() event handler:

Response.Clear()
Response.Write("<script language=javascript>self.close();</script>")
Response.End()

This will effectively write a single line of script to the window when it is rendered after the postback which will cause it to close.
0
 
dba123Author Commented:
Ok I guess let me tell you what I tried now
I changed the funciton first off

      function reload()
      {
            window.opener.top.MessageSubjectFrame.location.reload();
            window.close();
      }

ok, now I have this

    Protected Sub btnAccept_Click(ByVal sender As Object, ByVal e As EventArgs)
        vcUser.UpdateEBookPolicyDate()
        btnAccept.Attributes.Add("onclick", "reload();")
    End Sub

So, when I click on the accept button, I think I'm refreshing the pop-up page and not the parent (the one I navigated from)

How can you tell if the reload worked or was called other than debugging. I assume look at the source view in IE.
0
 
jef06Commented:
carefull if you do a reload of the parent it might open your popup again, you might want to use location instead of reload on the window.parent.location.href= window.parent.location.href;
0
 
jef06Commented:
this btnAccept.Attributes.Add("onclick", "reload();") need to be done on the page_load
0
 
dba123Author Commented:
I don't really need the postback
0
 
dba123Author Commented:
why on the page load if I only want this to happen when invoked by the clicking of the Accept button?
0
 
jef06Commented:
<script language="C#" runat="server">
  protected  void Page_Load(object sender, EventArgs e){
    //Set the button's client-side onmouseover event
    btnClick.Attributes.Add("onClick", "alert('Ouch, you clicked me!');");
  }
</script>

<form runat="server">
  <asp:button runat="server" Text="Click Me!" id="btnClick" />
</form>
0
 
TimCotteeCommented:
After the postback you mean. Ok, then that means that you should use the same technique as for the close() I just showed you to emit the reload on the btnAccept_Click.

You cannot use the attributes.add method as this just adds the event handler to the button click, it doesn't actually execute it until that button is clicked. You need to use response.write or page.clientscript.registerstartupscript to emit the relevant javascript. Now it is likely in fact that you won't even have this function if you use response.clear() so you may be better off emitting the whole bit of script as a one-off:

    Protected Sub btnAccept_Click(ByVal sender As Object, ByVal e As EventArgs)
        vcUser.UpdateEBookPolicyDate()
        Response.Clear()
        Response.Write("<script language=javascript>window.opener.top.MessageSubjectFrame.location.reload(); window.close();</script>")
        Response.End()
    End Sub
0
 
dba123Author Commented:
I guess I'm not sure if I need or don't need the postback....I'm not doing anything with session state, cookies, etc. so I assume I don't need to worry about it.
0
 
jef06Commented:
You should never use Response.Write to send client side script.
0
 
dba123Author Commented:
>>>You should never use Response.Write to send client side script.

reason?
0
 
TimCotteeCommented:
Probably not jef06 though it is effective in this kind of situation. Of course you can use the registerstartupscript/registerclientscriptblock and these will have the same effect.
0
 
dba123Author Commented:
Ok, again, here's what I have but it's not working.  I want this pop-up to close then refresh the parent

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        panAgreement.Visible = True
        btnAccept.Attributes.Add("onclick", "reload();")
    End Sub

    Protected Sub btnAccept_Click(ByVal sender As Object, ByVal e As EventArgs)
        vcUser.UpdateEBookPolicyDate()
    End Sub

Now, the respnse.write does close it, but Jeff's saying it's not good to use.  However otherwise, the script above without the response.write does nothing as far as my human eye can tell
0
 
dba123Author Commented:
I guess what's the best way as in coding standards (safety, efficiency, yada, yada) in terms of doing that in ASP.NET on the 2 things I want to happen here.  I don't just want a hack but I'm not sure the best code practice either.
0
 
dba123Author Commented:
also, my pop-up is an aspx page with a control inside it.  The control contains the Accept button.  Not sure if they makes any difference on our conversations.
0
 
dba123Author Commented:
what is MessageSubjectFrame, the name of the frame you want to apply this on correct?
0
 
jef06Commented:
you have no control of the response object like in asp, it means that you migth send client side script which are either not in the html tag or any where in the page, also using end will stop the response will the page is not complete. use register or attribute but not response
0
 
dba123Author Commented:
>>carefull if you do a reload of the parent it might open your popup again

shouldn't.  Cause here's the sequence of what I'm doing

1) I click on this link in an aspx page of ours.  This aspx page is in an iframe or something

<asp:HyperLink ID="HyperLink1" ForeColor="black" NavigateUrl="JavaScript: void(0);"  OnClick="javascript:var v=window.open('/controls/EBookPolicy.aspx', 'eBookPolicy', 'top=5,left=5,width=780,height=300,resizable=1,channelmode=0,directories=0,fullscreen=0,location=0,menubar=0,scrollbars=0,status=0,titlebar=0,toolbar=0');" runat="server"><b>HERE</b></asp:HyperLink>

2) Pop-Up comes up. The Pop-Up page is another aspx and it contains a registered control that contains the acept button
0
 
TimCotteeCommented:
No, it doesn't make a difference. The thing with attributes.add is that you are adding client-side attributes of an element from the server-side. So you can tailor client-side events such as the onclick, onmouseover etc based on server-side held values. This allows you to make dynamic tooltips for onmouseover for example. However this method does not actually cause these javascript snippets to run. It simply allows them to be run when the appropriate trigger occurs, be that a click, roll-over or keypress.

Using response.write to emit the relevant script may not be perfect but does do the job. You can achieve the same effect using the page.clientscript.registerstartupscript() method. Though you would probably be as well to use response.clear() first.
0
 
dba123Author Commented:
Ok, but I don't get why this isn't wroking also below.  Seems to me that this adds it to the button and then when clicked, the reload is called. Maybe it is working but just not refreshing the right page.  I think it's refreshing my pop-up page instead as I do see it flash.  Also my current reload function is not closing the window so I'll try to add the self.close into my actual javascript reload function right after the refresh call

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        panAgreement.Visible = True
        btnAccept.Attributes.Add("onclick", "reload();")
    End Sub

    Protected Sub btnAccept_Click(ByVal sender As Object, ByVal e As EventArgs)
        vcUser.UpdateEBookPolicyDate()
    End Sub
0
 
jef06Commented:
If you open the popup with a registerstartupscript the page might only reload the page from the cache and the re popup your page, also using Reload might not post pack your page on the server. add at the end of location a random number
0
 
dba123Author Commented:
So here is what  "seems"t o be working, just that I need some delay window.setTimeout  or something before it refreshes the parent page but not sure what I should put in for the first param of selfTimeout

      function reload()
      {
      window.opener.location.reload();
      self.close();
      }

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        panAgreement.Visible = True
        btnAccept.Attributes.Add("onclick", "reload();")
    End Sub

so that works but

1) How do I know I"m targeting the parent aspx (the one I came from before the pop-up)
2) How do I add that delay so that I bring the user back to that page after the DB transaction is done

0
 
dba123Author Commented:
so I took out some stuff from before which was

    window.opener.top.MainFrame.MessageSubjectFrame.location.reload

took out the MinFrame.MessageSubjectFrame part and it seems to work however I feel I should be targeting the right frame so I need to figure that out.
0
 
TimCotteeCommented:
You are targetting the correct parent window, if that parent has move to a different page, it will be reloaded. This is because the window.opener refers to the window not the document object.

To add the delay you can use:

setTimeout('window.opener.location.reload();window.close();',1000);

Which will give a one second delay before those two script statements are run.
0
 
dba123Author Commented:
I tried this, but now it doesn't do a thing
      function reload()
      {
                         setTimeout('window.opener.location.reload();window.close();',1000
      }
Thi s is what I had that worked

      function reload()
      {
            window.opener.location.reload();
            self.close();
      }
0
 
dba123Author Commented:
0
 
dba123Author Commented:
My entire argument against Javascript scripting inside n aspx is that now .NET controls can create the client-side JavaScript.  When I see an aspx page with a ton of JavaScript functions in it, it makes me think that the people who coded the .NET are still thinking in classic asp terms am I wrong?
0
 
jef06Commented:
I did an entire site using asp.Net custom made Ajax framework, and the feel for the user is so much better :)
0
 
dba123Author Commented:
I mean should it be this way?  I tried this but I assume that I don't need the additional lines after Set Timeout

      function reload()
      {
          setTimeout('window.opener.location.reload();self.close();',1000)
            window.opener.location.reload();
            self.close();
      }
0
 
dba123Author Commented:
>>I did an entire site using asp.Net custom made Ajax framework, and the feel for the user is so much better :)

No, I love Ajax, dont' get me wrong.  I guess then I jus tdon't like to see a ton of Javascript code ni an aspx.  Not having used Atlas yet, I assume though that you're using .NET client-side type controls so you don't see a mess of JavaScript defined in your aspx?
0
 
dba123Author Commented:
the whole concept behind Javascript  driving Ajax is fine, I just don't like to see a ton of JavaScript in an aspx and thinking the framework for Atlas is probably much cleaner in that you ar eusing more organized controls and standards
0
 
dba123Author Commented:
well, I'm working on figuring out why this is not working

      function reload()
      {
          setTimeout('window.opener.location.reload();self.close();',1000)
            window.opener.location.reload();
            self.close();
      }

it's closing the window but now I don't get a refresh and it's closing it immediately, the delay isn't working.
0
 
dba123Author Commented:
also tried this, but then nothing happens period

        btnAccept.Attributes.Add("onclick", "setTimeout('reloadParent()';,2000)")
0
 
dba123Author Commented:
btnAccept.Attributes.Add("onclick", "setTimeout('reloadParent()';,2000

function reloadParent()
      {
            window.opener.location.reload();
            self.close();
      }
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 25
  • 9
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now