You must Sign In to post a response.
Category: JavaScript
#765959
You need to register or call javascript function from class file, import System.Web.UI and System.Web namespace on your class file, see below snippet
From this method you need to use any one code depends on your condition which I have added in comments.
In this way you can call javascript function from class file asp.net C#
Thanks
Koolprasd2003
Editor, DotNetSpider MVM
Microsoft MVP 2014 [ASP.NET/IIS]
public static void Error(string message)
{
//----- One Way----If you are not using ScriptManager on page-------------
HttpContext.Current.Response.Write("<script type='text/javascript' language='javascript'>alert('" + message + "');</script>");
////----- Second Way ------- If you are not using ScriptManager on page -------
Page page = HttpContext.Current.Handler as Page;
ClientScriptManager cs = page.ClientScript;
cs.RegisterStartupScript(page.GetType(), "WitoutUpdatePanel", "javascript:alert('" + message + "');", true);
////----- Third Way------- If you are using ScriptManager on page -------------
ScriptManager.RegisterStartupScript(page, page.GetType(), "WithUpdatePanel", "javascript:alert('" + message + "')", true);
}
From this method you need to use any one code depends on your condition which I have added in comments.
In this way you can call javascript function from class file asp.net C#
Thanks
Koolprasd2003
Editor, DotNetSpider MVM
Microsoft MVP 2014 [ASP.NET/IIS]
#765961
Created one class file and called created ShowMessage method in that newly created class file. Using console application can access this method
Error : HttpContext.Current.Response.Write("<script type='text/javascript' language='javascript'>alert('" + message + "');</script>");
Object reference not set to an instance of an object.
Class File:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
namespace Home
{
public class Decode
{
public static void ShowMessage(string message)
{
HttpContext.Current.Response.Write("<script type='text/javascript' language='javascript'>alert('" + message + "');</script>");
}
}
}
My Console Application :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Home;
namespace RESTConsole
{
class Program
{
static void Main(string[] args)
{
Home.Decode.ShowMessage("Welcome to Application");
}
}
}
Error : HttpContext.Current.Response.Write("<script type='text/javascript' language='javascript'>alert('" + message + "');</script>");
Object reference not set to an instance of an object.
Class File:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
namespace Home
{
public class Decode
{
public static void ShowMessage(string message)
{
HttpContext.Current.Response.Write("<script type='text/javascript' language='javascript'>alert('" + message + "');</script>");
}
}
}
My Console Application :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Home;
namespace RESTConsole
{
class Program
{
static void Main(string[] args)
{
Home.Decode.ShowMessage("Welcome to Application");
}
}
}
Return to Return to Discussion Forum