You must Sign In to post a response.
  • Category: ASP.NET

    Call javascript function from ASP.Net

    I want to call javascript function from asp.Net page load method .
  • #648815
    Hi,

    Please check below methods,

    Method 1 from page onload,


    <script type = "text/javascript" >
    function TestJavaScript()
    {
    //JavaScript code
    }
    </script>



    and call above function on load of your body, like below,


    <body onload="TestJavaScript()">



    Method 2 from your code behind,


    Page.RegisterStartupScript("Confirm", "<script>TestJavaScript();</script>");


    In the above code Confirm() is the JavaScript function name, you may change this according to the name of the function you are using in your code.


    Regards,
    Asheej T K

  • #648819
    Please check the following code:


    protected void Page_Load(object sender, EventArgs e)
    {
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "MyScript", "javascript:YourJavaScriptFunction();", true);
    }


    Thanks & Regards
    Paritosh Mohapatra
    Microsoft MVP (ASP.Net/IIS)
    DotNetSpider MVM

  • #648859
    Try below code:

    protected void Page_Load(object sender, EventArgs e)
    {
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Scriptdata", "javascript:DeleteRecord();", true);
    }

    Regards,
    Sugandha
    Microsoft Certified Technology Specialist
    MY Blog..

  • #648900
    Hello,

    Please check below code:


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title></title>
    <script>
    ShwV = 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 "100" and call "ShwV" from server.


    TextBox1.Text = "100";
    Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "ShwV()", true);

    Regards
    Mr.Rajesh
    Software Engg.


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.