Asp.net programming we consider all important topic for knowledge about controls,web Services, JSON, Ajax, database, and interview related Question. In this Asp.net and sql server tutorial blog we give many examples of jquery as like How Play YouTube Video in Your Asp.net Website by URL, How to Play YouTube Videos in Asp.net by using Jquery , Jquery Change div background on mouseover in asp.net , JQuery UI Datepicker (Calendar) etc.

Saturday, November 8, 2014

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:







9 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