Calling client function from server..

  • Thread starter Thread starter Roger
  • Start date Start date
R

Roger

Is this possible??

client side:

<script language="jscript">
function hello() {
window.alert("HELLO!");
}
</script>

server side:

Private Sub cmButton_Click()
hello()
End Sub
 
Hi Michael,

No, you need to put this in your Page_Load event:

cmButton.Attributes.Add("onclick", "hello(); return false;");

Good luck! Ken.
 
No, server-side code is executed before the HTML is generated and sent to
the browser, including any javascript it contains. There is no bridge
between the two.

What you can do, however, is output some javascript to the page based on
whether or not the button was pressed.
Put a literal in your page somewhere...

<asp:Literal ID="executeHello" runat="Server"></asp:Literal>

And in your button handler, do something like this...

Private Sub cmButton_Click()
executeHello.Text = "<script language=""javascript"">hello();</script>";
End Sub


When the page loads, it will contain that piece of javascript that is
calling hello () if and only if the button was pressed.
 
Yeah, ignore my method. Mine would post back the page and say hello
afterwards. Ken's method will immediately execute the javascript when the
button is pressed, which is probably what you're looking for :)
 
Thanks guys..


The unProfessional said:
Yeah, ignore my method. Mine would post back the page and say hello
afterwards. Ken's method will immediately execute the javascript when the
button is pressed, which is probably what you're looking for :)
 
cmButton.Attributes.Add("onclick", "hello(); return false;");

No need for the "return false;" - you only need that if you're asking your
JavaScript function to return something to the caller.
 
Hi,

By default an <asp:Button> posts back to the server. Without the "return
false;" this page would do a post back. Alternatively you could use an
<input type='button'> and then you won't have to return false. Ken.
 
By default an <asp:Button> posts back to the server.

It certainly does.
Without the "return false;" this page would do a post back. Alternatively
you could use an
<input type='button'> and then you won't have to return false.

Sorry - my mistake - I didn't see the reference to <asp:Button> in the OP.
 
Back
Top