www.webdeveloper.com
Results 1 to 15 of 15

Thread: Calling JavaScript function on ASP.NET page load

  1. #1
    Join Date
    Feb 2008
    Posts
    13

    Calling JavaScript function on ASP.NET page load

    I am new to JavaScript and ASP.NET programming, and have what is probably a really simple question.

    I have a JavaScript function defined in the head section of an aspx page.

    How can I fire this function from the asp.net page_load function when the page loads?

  2. #2
    Join Date
    Aug 2007
    Posts
    151
    if you want to fire a javascript function from page_load method in aspx.cs, you can use Page.ClientScript.RegisterScriptBlock method (not remember exactly name, but google can help)

  3. #3
    Join Date
    Oct 2006
    Location
    Ontario Canada
    Posts
    1,160
    you can also add it to the onload event on the body element.

    <body onload="myFunction()">

    or
    window.onload = function() { myFunction() }



    Drew

  4. #4
    Join Date
    Feb 2008
    Posts
    13
    Thanks for the help so far.

    I think I've got to call it from the page_load because I"m filling a hidden field first to then read using my javascript function.

    Google does not return anything with the Page.ClientScript.RegisterScriptBlock function that is very helpful for this though.

    Any other responses (or guidance on the Page.ClientScript.RegisterScriptBlock function) would be appreciated.

    Thanks

    - Kip

  5. #5
    Join Date
    Oct 2006
    Location
    Ontario Canada
    Posts
    1,160
    I believe its like so..
    Page.RegisterClientScriptBlock("MyScript","<SCRIPT Language='JavaScript'> alert('Hello World'); </SCRIPT>");

  6. #6
    Join Date
    Feb 2008
    Posts
    13
    Actually, it's asking for 3 parameters (type, key, script)

    The type is what I'm having problems with. All the examples I'm seeing have this.GetType() in them, but that doesn't seem to be an option for me in VB.NET.

  7. #7
    Join Date
    Oct 2006
    Location
    Ontario Canada
    Posts
    1,160
    try
    this.Page

  8. #8
    Join Date
    Aug 2007
    Posts
    151
    The Page.RegisterClientScriptBlock method has been deprecated;
    in dotnet framework 2005, use Page.ClientScript.RegisterClientScriptBlock method instead.

  9. #9
    Join Date
    Feb 2008
    Posts
    13
    I have put the following at the bottom of my page_load function:

    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "LoadData", sFunction)

    But when the page opens, it is now blank. Doesn't show any of the fields on the page that were being displayed before. It's almost like the page_load is stopping now.

    Any thoughts?

  10. #10
    Join Date
    Aug 2007
    Posts
    151
    assume you have a control named "TextBox1" in aspx file, and its value is "2"
    also, you have a javascript function called "ShowValue" which will alert the value of TextBox1.
    so, your aspx file like this:
    Code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
        <script>
            ShowValue = function()
            {
                alert(document.getElementById("TextBox1").value);
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server" Text="2"></asp:TextBox>        
        </div>
        </form>
    </body>
    </html>
    now, in server-side, at Page_Load method, you set value of "TextBox1" to "9" and call "ShowValue" from server.

    Code:
    TextBox1.Text = "9";
    Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "ShowValue()", true);
    that is.

    The RegisterClientScriptBlock method adds a script block to the top of the rendered page.

    while, the script block added by the RegisterStartupScript method executes when the page finishes loading but before the page's OnLoad event is raised.
    (it means the RegisterStartupScript method adds a script block to the end of the rendered page.)

  11. #11
    Join Date
    Feb 2008
    Posts
    13
    hoangkc -

    Thank you very much. That was exactly how I needed it explained.

    Got it working now and passing the desired information that I need.

    Again - A big thank you.

    - Kip

  12. #12
    Join Date
    Apr 2011
    Posts
    1

    Thumbs up

    Quote Originally Posted by hoangkc View Post
    assume you have a control named "TextBox1" in aspx file, and its value is "2"
    also, you have a javascript function called "ShowValue" which will alert the value of TextBox1.
    so, your aspx file like this:
    Code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
        <script>
            ShowValue = function()
            {
                alert(document.getElementById("TextBox1").value);
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server" Text="2"></asp:TextBox>        
        </div>
        </form>
    </body>
    </html>
    now, in server-side, at Page_Load method, you set value of "TextBox1" to "9" and call "ShowValue" from server.

    Code:
    TextBox1.Text = "9";
    Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "ShowValue()", true);
    that is.

    The RegisterClientScriptBlock method adds a script block to the top of the rendered page.

    while, the script block added by the RegisterStartupScript method executes when the page finishes loading but before the page's OnLoad event is raised.
    (it means the RegisterStartupScript method adds a script block to the end of the rendered page.)
    Yes this is an old post, but my problem was new.

    Thank you so much for the solution! It worked well.

  13. #13
    Join Date
    Nov 2011
    Posts
    1
    actually, you'll want to add a semicolon after ShowValue() in the RegisterStartupScript call. If another component does a similar register call on that page, it's javascript output will run together with yours and cause an error without the semicolon in-between.

    p.s. thanks for the helpful info!

  14. #14
    Join Date
    May 2013
    Posts
    1
    I know this is an old post, but THANK YOU!! I've been fighting trying to figure that out all day :-\ Really appreciate you taking the time to write out the complete example!

  15. #15
    Join Date
    Aug 2013
    Posts
    1
    Helpful tips. Thanks !!!

    I found one more helpful tips from here http://blogfornet.com/2013/08/call-j...age-body-load/

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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



Recent Articles