DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: How can I call js function from code behind?

  1. #1
    Join Date
    Dec 2004
    Posts
    717

    How can I call js function from code behind?

    How can I call js function from code behind?
    Thanks in advances,
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    You can't. Code-behind runs on the Web server; JavaScript runs in the user's browser. What are you trying to do?
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    May 2005
    Location
    UK
    Posts
    278

    you can run js scripts on code behind with limitaitons ...

    Hi

    You can call Js functions as a script registered on the codebehind itself , but not sure about ur scenario
    Page.RegisterStartupScript("key","value")
    key is the name you want to give the script eg. "PageClose"
    value is the
    stringBuilder str = new StringBuilder()
    str.Append("<script = language='javascript'>");
    str.Append("window.close();");
    str.Append("</script>")
    here instead of using the window.close you coud append your js function as a string, ideally i put this string builder class and build the script in the constructor if i need it always in the page

    Then use this in the event handler you want to execute the script

    Page.RegisterStartUp("PageClose",str.ToString());
    This would place the javascript before the closing </form> tag of the page thats rendered

    Page.ClientScriptBlock("PageClose",str.ToString());
    This would place the JS function after the opening <form> tag of the page thats rendered

    Hope this helps

  4. #4
    Join Date
    Dec 2004
    Posts
    717
    Thanks. Phil & srinivas_s .
    Actually, I have one function in C# and another function in JavaScript.
    If I click one button, I wanna check the function in C# firstly.And then, If it's pass, I would like to call the function in Javascript. :-) It's not possible.
    What do you say?
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  5. #5
    Join Date
    May 2005
    Location
    UK
    Posts
    278
    It is possible, eg. Lets say the button's id is button1

    In the button_Click event on the code behind in C# do this

    Use your C# function, to return a condition pass/fail value
    if (pass) {user Page.RegisterStartupScript("JSFunc","<js script string>")}
    else
    {do something else}

    So you have called both your server side code in C# and your JS script as per your requirement

    This is precisely why you can use RegisterStartupScript and RegisterClientScriptBlock methods on the page class.

  6. #6
    Join Date
    Dec 2004
    Posts
    717
    Thanks.srinivas_s.
    I'll try it.
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  7. #7
    Join Date
    Jun 2005
    Posts
    1

    Calling Javascript from client side

    You can do this in a following way.

    On the server side event you need to write like this.

    Protected strScript

    Sub Event ()...

    strScript = "<script language="javascript">"


    End Sub

    Write the entier java script into a string variable

    On the client side code like the following

    <html>
    <form>
    ....
    ....
    ....
    ....
    </table>
    <%= strScript %>



    One of the drawback in this approch is if you click refresh button the same script will be fired. You should hadling this very carefully

  8. #8
    Join Date
    Jul 2005
    Posts
    1

    JS function first...

    I was wondering if it would be possible to first call the JS function and then the code behind processing. How to do this?
    Thanks,
    -sb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center