Calling a JavaScript function from c# code behind file

Calling JavaScript function from code behind ASP.NET:



In this post we describe how to call JavaScript function from code behind file. When we are working in web application then this required many a times.

JavaScript function from C# code behind ASP.NET:

Java Script Function:

Use this function in head tag on page.

function MyFunction() {
            alert('this is javascript function run by C# code.');
        }

Write this code in C# code page:

            Page.ClientScript.RegisterStartupScript(this.GetType(),
                "alert", "MyFunction();", true);

How to execute JavaScript Function From ASP.NET Code:

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

<!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>Call JavaScript in asp.net page by C# on page load </title>
    <script type="text/javascript">
        function MyFunction() {
            alert('this is javascript function run by C# code.');
        }

    </script>
</head>

<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>
  

c# - execute JavaScript from code behind in asp.net:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class How_calljavascript_aspx_page : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!ClientScript.IsStartupScriptRegistered("alert"))
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(),
                "alert", "MyFunction();", true);
        }
    }
}

Calling a JavaScript function from c# code behind file by parijat




We can also use Script Manager for execute JavaScript code behind the asp.net page.





Asp.net Related Other Post:







Comments

  1. I agree with you.This post is truly inspiring.
    I like your post and everything you share with us is current and very informative,
    I want to bookmark the page so I can return here from you that you have done a fantastic job

    T H A N K S
    F O R
    S H A R I N G

    WinAVI Video Converter 11.5 Keygen
    Trend Micro Antivirus
    Movie Converter 7 Keygen
    Neuratron Audio Score Ultimate Keygen

    ReplyDelete
  2. Admiring the hard work you put into your
    site and detailed information you provide. It’s awesome to come across a blog every once in a
    while that isn’t the same out of date rehashed material.
    Wonderful read! I’ve bookmarked your site and I’m adding your RSS feeds to my Google account....

    T h a n k s F o r

    S h a r i n g

    Internet Business Promoter

    ReplyDelete
  3. Admiring the hard work you put into your
    site and detailed information you provide. It’s awesome to come across a blog every once in a
    while that isn’t the same out of date rehashed material.
    Wonderful read! I’ve bookmarked your site and I’m adding your RSS feeds to my Google account....

    T h a n k s F o r

    S h a r i n g

    Internet Business Promoter

    ReplyDelete
  4. Hello admin, I always used to read paragraph in news papers but

    now as I am a user of net Therefore from now I am using net for content, thanks tonet
    web. it's my first visit at this site, and articleweb
    is really fruitful in support of me ..

    Baidu Antivirus 2016 Offline Installer
    Mixcraft 7.7 Crack
    Solsuite 2016 keygen

    ReplyDelete
  5. I am truly glad to read this webpage posts which includes plenty of helpful data, thanks for providing these kinds of information.
    Are you finding Software Crack, Serial Number, Keys, Free Game, Android APPS ?, We are help you to get any crack software, game, apps. please visit our website: Download Crack | Serial Number | Serial Keys | Camtasia Studio 8 Crack | Camtasia Studio 8 Key | Camtasia Studio 8 Crack | IDM Serial Number | Microsoft Office 2007 Product Key
    More:
    inDesk | Bangla Live TV | SSC Result | HSC Result | JSC Result | Bangla eBook | Bangla Boi | Serial Number IDM

    ReplyDelete
  6. I am truly glad to read this webpage posts which includes plenty of helpful data, thanks for providing these kinds of information.
    Are you finding Software Crack, Serial Number, Keys, Free Game, Android APPS ?, We are help you to get any crack software, game, apps. please visit our website: Download Crack | Serial Number | Serial Keys | Camtasia Studio 8 Crack | Camtasia Studio 8 Key | Camtasia Studio 8 Crack | IDM Serial Number | Microsoft Office 2007 Product Key | Microsoft Office 2010 Product Key
    More:
    LimonCox | inDesk | Bangla eBook | Bangla Boi | Serial Number IDM | Nulled Scripts

    ReplyDelete
  7. I just stumbled upon your informative article and wanted to say that I have really enjoyed reading your very well written posts. See more nice videos of instagram from site picbear

    ReplyDelete

Post a Comment

Popular posts from this blog