calling JavaScript function from code-behind

Discussion in 'ASP .Net Web Controls' started by Alexander Kaplunov, Jun 17, 2004.

  1. 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.
    Alexander Kaplunov, Jun 17, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:%23deHFg$...
    > 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.
    >
    >
    Alvin Bruney [MVP], Jun 17, 2004
    #2
    1. Advertising

  3. Alexander Kaplunov

    Scott M. Guest

    Or, in Page_Load, you can do this:

    control.attributes.add(eventHandlerName, clientFunctionToRun)


    "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    news:...
    > 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" <> wrote in message
    > news:%23deHFg$...
    > > 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.
    > >
    > >

    >
    >
    Scott M., Jun 17, 2004
    #3
  4. Not every control has attributes so this won't work in my case.


    "Scott M." <> wrote in message
    news:...
    > Or, in Page_Load, you can do this:
    >
    > control.attributes.add(eventHandlerName, clientFunctionToRun)
    >
    >
    > "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    > news:...
    > > 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" <> wrote in message
    > > news:%23deHFg$...
    > > > 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.
    > > >
    > > >

    > >
    > >

    >
    >
    Alexander Kaplunov, Jun 18, 2004
    #4
  5. Alexander Kaplunov

    Scott M. Guest

    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" <> wrote in message
    news:...
    > Not every control has attributes so this won't work in my case.
    >
    >
    > "Scott M." <> wrote in message
    > news:...
    > > Or, in Page_Load, you can do this:
    > >
    > > control.attributes.add(eventHandlerName, clientFunctionToRun)
    > >
    > >
    > > "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    > > news:...
    > > > 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" <> wrote in message
    > > > news:%23deHFg$...
    > > > > 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.
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Scott M., Jun 18, 2004
    #5
  6. 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." <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:...
    > > Not every control has attributes so this won't work in my case.
    > >
    > >
    > > "Scott M." <> wrote in message
    > > news:...
    > > > Or, in Page_Load, you can do this:
    > > >
    > > > control.attributes.add(eventHandlerName, clientFunctionToRun)
    > > >
    > > >
    > > > "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    > > > news:...
    > > > > 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" <> wrote in message
    > > > > news:%23deHFg$...
    > > > > > 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.
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Alexander Kaplunov, Jun 18, 2004
    #6
  7. 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" <> wrote in message
    news:%...
    > 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." <> wrote in message
    > news:...
    >> 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" <> wrote in message
    >> news:...
    >> > Not every control has attributes so this won't work in my case.
    >> >
    >> >
    >> > "Scott M." <> wrote in message
    >> > news:...
    >> > > Or, in Page_Load, you can do this:
    >> > >
    >> > > control.attributes.add(eventHandlerName, clientFunctionToRun)
    >> > >
    >> > >
    >> > > "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    >> > > news:...
    >> > > > 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" <> wrote in message
    >> > > > news:%23deHFg$...
    >> > > > > 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.
    >> > > > >
    >> > > > >
    >> > > >
    >> > > >
    >> > >
    >> > >
    >> >
    >> >

    >>
    >>

    >
    >
    Alvin Bruney [MVP], Jun 18, 2004
    #7
  8. Alexander Kaplunov

    Fritz Guest

    "Alvin Bruney [MVP]" <vapor at steaming post office> wrote:

    > 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.
    Fritz, Jun 19, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    554
    Alphonse Giambrone
    Jun 28, 2003
  2. Fred Armitage
    Replies:
    2
    Views:
    3,355
    Sherif ElMetainy
    Aug 26, 2003
  3. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    Fire Code behind code AND Javascript code associated to a Button Click Event

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 10, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    21,151
    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=
    Feb 11, 2004
  4. Peter Bons

    calling javascript from code-behind

    Peter Bons, Oct 13, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    44,722
    Peter Bons
    Oct 14, 2005
  5. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    451
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
Loading...

Share This Page