calling JavaScript function from code-behind

A

Alexander Kaplunov

Hello,

I was wondering if there is a way to call a JavaScript function from
code-behind. I know that you can add a "onclick" attribute to a button but
that is not what I'm looking for. What I'm looking for is something similar
to this:

Client side:

function test()
{
alert("Got test!");
}

Server side:

public void foo()
{
call_javascript_function("test");
}

Is there a way to accomplish this?

Any help is appreciated.
Alex.
 
A

Alvin Bruney [MVP]

have a look at registerstartupscript or you can just response.write it out
if push comes to shove
 
S

Scott M.

Or, in Page_Load, you can do this:

control.attributes.add(eventHandlerName, clientFunctionToRun)


Alvin Bruney said:
have a look at registerstartupscript or you can just response.write it out
if push comes to shove

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
Alexander Kaplunov said:
Hello,

I was wondering if there is a way to call a JavaScript function from
code-behind. I know that you can add a "onclick" attribute to a button but
that is not what I'm looking for. What I'm looking for is something
similar
to this:

Client side:

function test()
{
alert("Got test!");
}

Server side:

public void foo()
{
call_javascript_function("test");
}

Is there a way to accomplish this?

Any help is appreciated.
Alex.
 
A

Alexander Kaplunov

Not every control has attributes so this won't work in my case.


Scott M. said:
Or, in Page_Load, you can do this:

control.attributes.add(eventHandlerName, clientFunctionToRun)


Alvin Bruney said:
have a look at registerstartupscript or you can just response.write it out
if push comes to shove

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
Alexander Kaplunov said:
Hello,

I was wondering if there is a way to call a JavaScript function from
code-behind. I know that you can add a "onclick" attribute to a button but
that is not what I'm looking for. What I'm looking for is something
similar
to this:

Client side:

function test()
{
alert("Got test!");
}

Server side:

public void foo()
{
call_javascript_function("test");
}

Is there a way to accomplish this?

Any help is appreciated.
Alex.
 
S

Scott M.

Alex,

Nearly everything does have an eventhandler (onClick, onLoad, onChange,
onBlur, etc.). Your code doesn't tell us what it is you want to do other
than show us a server call to a client function. When do you need your
client function to fire?

Alexander Kaplunov said:
Not every control has attributes so this won't work in my case.


Scott M. said:
Or, in Page_Load, you can do this:

control.attributes.add(eventHandlerName, clientFunctionToRun)


Alvin Bruney said:
have a look at registerstartupscript or you can just response.write it out
if push comes to shove

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
Hello,

I was wondering if there is a way to call a JavaScript function from
code-behind. I know that you can add a "onclick" attribute to a
button
but
that is not what I'm looking for. What I'm looking for is something
similar
to this:

Client side:

function test()
{
alert("Got test!");
}

Server side:

public void foo()
{
call_javascript_function("test");
}

Is there a way to accomplish this?

Any help is appreciated.
Alex.
 
A

Alexander Kaplunov

OK, I am using Microsoft.Web.UI.WebControls.ToolbarButton

This control has a ButtonClick event, which is handeled on the server. What
I really need is when this button is clicked to call a javascript function
on the client side.



Scott M. said:
Alex,

Nearly everything does have an eventhandler (onClick, onLoad, onChange,
onBlur, etc.). Your code doesn't tell us what it is you want to do other
than show us a server call to a client function. When do you need your
client function to fire?

Alexander Kaplunov said:
Not every control has attributes so this won't work in my case.


Scott M. said:
Or, in Page_Load, you can do this:

control.attributes.add(eventHandlerName, clientFunctionToRun)


"Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
have a look at registerstartupscript or you can just response.write
it
out
if push comes to shove

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
Hello,

I was wondering if there is a way to call a JavaScript function from
code-behind. I know that you can add a "onclick" attribute to a button
but
that is not what I'm looking for. What I'm looking for is something
similar
to this:

Client side:

function test()
{
alert("Got test!");
}

Server side:

public void foo()
{
call_javascript_function("test");
}

Is there a way to accomplish this?

Any help is appreciated.
Alex.
 
A

Alvin Bruney [MVP]

you can still use the attributes function and append the javascript to the
particular button. Something like this should work:

//set a pointer to button number 2
ToolbarButton oSchedule = (ToolbarButton)Tools.Items[1];

//append javascript the normal way
oSchedule.Attributes blah blah blah

or you may want to just embed the javascript inside the text of the
particular button on the toolbar. You can do this either thru the above
statement and using

oSchedule.Text = "<onclick = 'somefunction()' span
style="color:brown;Border:eek:utset;">View Rates</span>"

or you can just do it from the text attribute of the toolbar item property
in the designer.

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
Alexander Kaplunov said:
OK, I am using Microsoft.Web.UI.WebControls.ToolbarButton

This control has a ButtonClick event, which is handeled on the server.
What
I really need is when this button is clicked to call a javascript function
on the client side.



Scott M. said:
Alex,

Nearly everything does have an eventhandler (onClick, onLoad, onChange,
onBlur, etc.). Your code doesn't tell us what it is you want to do other
than show us a server call to a client function. When do you need your
client function to fire?

Alexander Kaplunov said:
Not every control has attributes so this won't work in my case.


Or, in Page_Load, you can do this:

control.attributes.add(eventHandlerName, clientFunctionToRun)


"Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
have a look at registerstartupscript or you can just response.write it
out
if push comes to shove

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
Hello,

I was wondering if there is a way to call a JavaScript function from
code-behind. I know that you can add a "onclick" attribute to a button
but
that is not what I'm looking for. What I'm looking for is something
similar
to this:

Client side:

function test()
{
alert("Got test!");
}

Server side:

public void foo()
{
call_javascript_function("test");
}

Is there a way to accomplish this?

Any help is appreciated.
Alex.
 
F

Fritz

Alvin Bruney said:
you can still use the attributes function and append the javascript to the
particular button. Something like this should work:

//set a pointer to button number 2
ToolbarButton oSchedule = (ToolbarButton)Tools.Items[1];

//append javascript the normal way
oSchedule.Attributes blah blah blah

or you may want to just embed the javascript inside the text of the
particular button on the toolbar. You can do this either thru the above
statement and using

oSchedule.Text = "<onclick = 'somefunction()' span
style="color:brown;Border:eek:utset;">View Rates</span>"

or you can just do it from the text attribute of the toolbar item property
in the designer.
Or catch the event at the form level. Add an attribute to the form, then
catch it there. Works properly only for IE.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top