Saturday, 7 June 2014

How to call javascript function from code behind Using C# in Asp.Net

6/07/2014 - By Pranav Singh 0

As we all know that ASP.NET  stands for model view and controller. Using JavaScript is one of the most challenging part. In my  current article I have used Asp.net, C#.Net and JavaScript.


So in this article I will show you how you can call JavaScript  in your Asp.Net application from code behind . In this article I will show How to call javascript function from code-behind.

So first we will create a new ASP.NET   application and add a .aspx page.

Now have a look of your .aspx page code.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AlertmessageInCsharp.aspx.cs" Inherits="ProjectDemo_Asp.et.AlertmessageInCsharp" %>

<!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 to javascript Function from Code Behind using C#.Net</title>
    <script language="javascript">
        function CallCodeBehind() {
            alert("Call to javascript Function from Code Behind using C#.Net");
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h3>Call to javascript Function from<br /> Code Behind using C#.Net</h3>
    </div>
    </form>
</body>
</html>

In above code we have declared a declared a javascript function. This function we will call in our codebehind.

Have a look of the below code.

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

namespace ProjectDemo_Asp.et
{
    public partial class AlertmessageInCsharp : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Call My Function In C#", "CallCodeBehind()", true);
        }
    }
}

Here we have used Page.ClientScript.RegisterStartupScript function to register our JavaScript. Now view your page in browser you will get output as shown below.



About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top