• A2ZMenu.Com
Loading
  • Home
  • Blogs
    • AJAX
    • ASP.NET
    • BI
    • CSharp
    • JavaScript
    • LINQ
    • Miscellaneous
    • SQL
    • SharePoint
    • Silverlight
    • WCF
  • Tutorials
    • HTML5
    • ASP.NET 4.0
    • CSharp
    • SharePoint
    • SQL Function
  • Online Exam
    • ASP.NET
    • LINQ
    • Silverlight
    • SQL
    • JavaScript
    • SharePoint
  • Forum
  • Utility
  • Fun @ Work
    • Appraisal
    • Funny Images
    • Funny Puzzle
    • Miscellaneous
    • Motivating Stories
  • Wiki
  • Contact Us
  • AJAX
  • ASP.NET
  • BI
  • CSharp
  • JavaScript
  • LINQ
  • Miscellaneous
  • SQL
  • SharePoint
  • Silverlight
  • WCF

Test Your Skill

Show/Hide
 Q. Out of Boxing and Unboxing which one is implicit?
View all questions...
Calling JavaScript function from C#
Tweet
mail-image

Hello Folks,

Today I will discuss about calling JavaScript function from ASP.NET code behind. The idea is sometimes we need to pass the server side data to the JavaScript methods. In this case we require to call the JavaScript from the code behind and pass the required parameter. 

Here is the code that will register the script block on the page and call the JavaScript message with the parameters passed from the code.

private void MethodRegisterJavaScript()

    {

        int a = 5; int b = 6;

        StringBuilder script = new StringBuilder();

        script.Append("<script type=\"text/javascript\">");

        script.Append("DisplayMessage('" + a + "', '" + b + "' )");

        script.Append("</script>");

        Page.ClientScript.RegisterClientScriptBlock(typeof(object), "JavaScriptBlock", script.ToString());

    } 

In the above method I am passing parameters a, b to JavaScript function DisplayMessage.

Now as per the requirement we can call MethodRegisterJavaScript, which will register the Script on page and call the JavaScript Method. 

Important point to note is that ClientScriptManager class has two methods RegisterClientScriptBlock and RegisterStartupScrip both can be used to register the required script on the page but there is little difference with respect to location on page where they get registered.

RegisterStartupScript method registers the Script block at the bottom of page just before the closing form tag. Advantage of this method is that if one is using any of the control’s ID in the JavaScript method, it will work fine as script get introduced on the page after all the controls are loaded. 

RegisterClientScriptBlock will be registered on the page before any of the control loads. So use this method only if you are sure that JavaScript method you are calling does not refer to any of the control’s ID.

Happy Coding J 


Related Posts:

Modify webconfig file at runtime
How to Read XML Data into DataSet
The path format is not supported
C# Null-Coalescing Operator (??)
Relative Path not working after URL Rewrite
MIME TYPE: Fetch MIME type from Registry
Convert Integer to Enum instance


Add Comment

Comment
Name
 (Required)
E-mail (Privacy assured)
 (Required)

Poll of the Day

  Who is best CEO so far?
 
 
 

Related Contents

Online Test Papers

  • ASP.NET
  • JavaScript
  • LINQ
  • SharePoint
  • Silverlight
  • SQL

Related Tutorials

  • HTML 5 Tutorial
  • SharePoint Best Practice
  • ASP.NET 4.0 Tutorials

Connect with Facebook

Top Blogs

  • SharePoint 2010 Model Dialog
  • Validation of viewstate MAC failed. If this application is hosted...
  • Silverlight: ListBox with CheckBox
  • Creating a Windows Service in C#
  • Using InstallUtil to install/uninstall service
  • How to call webservice using Ajax or JavaScript
  • LINQ to SQL Best Practice
  • The contract name could not be found in the list of contracts implemented by the service
  • Calling JavaScript function from C#
Blogs
  • AJAX
  • ASP.NET
  • BI
  • CSharp
  • HTML
  • JavaScript
  • LINQ
  • SQL Server
  • SharePoint
  • Silverlight
Tutorials
  • HTML5
  • ASP.NET 4.0
  • CSharp
  • SharePoint
  • SQL Server
Online Examinations
  • ASP.NET
  • LINQ
  • SQL Server
  • JavaScript
  • SharePoint
Online Utilities
  • Age Calculator
  • Happy Birthday Wisher
  • Independence Day Wisher
  • URL Encoder Decoder
  •  
Fun @ Work
  • Appraisal
  • Funny Images
  • Funny Puzzle
  • Miscellaneous
  • Motivating Stories
A2ZMenu © 2012 Home | Contact Us | Privacy Policy | Connect with Facebook | Follow us on Twitter