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.
In my previous article I have shown you Display
Alert Message on Page Load in ASP.NET Application Using Javasscript, Bind
DataGridView In Windows Application Using C#, Digital
Clock in Asp.Net in C#, Asynchronous
Page Loading In Asp.net Using UpdatePanel and Timer Control | Simple
Asynchronous Page Load in ASP.NET.
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.
0 comments:
Please let me know your view