return javascript value from asp.net button

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi there,
Is there a way to call a javascript function and return its value, from
inside an asp.net button ??
 
In your Page_Load event you can add a line such as the following:

[C#]
button.Attributes["onclick"] = "alert(myFunc());";
or
button.Attributes.Add("onclick", "alert(MyFunc());");

This will manipulate the HTML output to include the client side event handler.

HTH
 
thanks for the reply...
I'm trying to get the user's time (hours and minutes).. and my code is in
the page load, not a button (sorry),... how can i call this function , not to
show it in a messagebox, but to just store it... like:
dim x as string = javafunction()
i have 2 seperate function , 1 for hour and 1 for minute...

Dave Fancher said:
In your Page_Load event you can add a line such as the following:

[C#]
button.Attributes["onclick"] = "alert(myFunc());";
or
button.Attributes.Add("onclick", "alert(MyFunc());");

This will manipulate the HTML output to include the client side event handler.

HTH
----
Dave Fancher
http://davefancher.blogspot.com

ACaunter said:
Hi there,
Is there a way to call a javascript function and return its value, from
inside an asp.net button ??
 
What you want to do is not directly possible. You'll have to develop a
"proxy" page or something that executes the client side java script to
retrieve the values that you want and stores the results in a hidden field
and automatically posts (see form.submit()) the form back to the server
which, in turn stores the values of the hidden fields in (most likely) a
session object then redirects the user to the page with which s/he will
interact.

If you are trying to just place a timestamp when the user posts the form,
you wouldn't need a proxy page, you could just add a client side submit
event handler that executes the code you want and saves it in a hidden
field. Never underestimate the power of DHTML.

--
Dave Fancher
http://davefancher.blogspot.com


ACaunter said:
thanks for the reply...
I'm trying to get the user's time (hours and minutes).. and my code is in
the page load, not a button (sorry),... how can i call this function , not
to
show it in a messagebox, but to just store it... like:
dim x as string = javafunction()
i have 2 seperate function , 1 for hour and 1 for minute...

Dave Fancher said:
In your Page_Load event you can add a line such as the following:

[C#]
button.Attributes["onclick"] = "alert(myFunc());";
or
button.Attributes.Add("onclick", "alert(MyFunc());");

This will manipulate the HTML output to include the client side event
handler.

HTH
----
Dave Fancher
http://davefancher.blogspot.com

ACaunter said:
Hi there,
Is there a way to call a javascript function and return its value, from
inside an asp.net button ??
 
Add a hiden field with the runat=server attribute.

the script would do something like:
document.forms[0].hiddenFieldElement.value='Hello World";
document.forms[0].submit();

now the problem you are going to have is that you can't really respond to an
event for this...so in your page load you'll have to have....

(VB.NET)
If hiddenFieldElement.value<>String.Empty then
''...do whatever you need to and then set the hidden field value to
String.Empty
hiddenFieldElement.value=String.Empty
End if

(C#)
if(hiddenFieldElement.value!=string.Empty){
//Process Evenything
hiddenFieldElement.value=string.Empty
}
 
Back
Top