Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Call javascript function from codebehind in asp.net

Aug 9, 2010
Here i will explain how to call javascript function from codebehind without assigning it to any control in asp.net.

We can call javascript functions in two ways

first way is


 System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "document.getElementById('" + button2.ClientID + "').style.display='none';", true);
Seconda way you can call javascript function also


System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "MyJavascriptFunction();", true); 
Like this we can call the javascript functions or buttons in codebehind without assigning it to any control

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

17 comments :

Anonymous said...

Hi..
Your site is really nice.i learned many things from this site.Please try to write the above article with some examples.This could be useful for the beginners like me.

1. RegisterClientScriptBlock

2.RegisterClientScriptInclude

3.RegisterStartupScript

4. RegisterOnSubmitStatement

Anonymous said...

I had a problem with typeof(Page) so I resorted to the following with success:

System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "Script", "function();", true);

Manoj said...

worked for me...
Thank You a lot...

Gireesh said...

Its Not Working...........

Ramesh said...

yes its not working .....

Ramesh said...

we can call the alert from this method but call a function is not working ...!

Unknown said...

what is htphandler and http module please help where used in exactly in projects

Anonymous said...

i Want the coding for telerik grid

Mayank said...

Hi,

I have created one javascript file on button click i lost my effect .

How can i call my Javascript file in code behind on button click .

Thanks
Mayank

Unknown said...

hi sureshji

me also suresh can u pls tel me the vise versa of the above concept ...............
thanks in advance

Anonymous said...

ScriptManager.RegisterStartupScript(this, this.GetType(), "Script", "showmydiv()", true);

try this

Anonymous said...

Hi
I have three text boxes, the sum of the first and second textbox display in the third text box while leaving text box2 using java script

Thanks in advance

Ayesha said...

Not working

Anonymous said...

I have a problem that
I have 2 popup divs,
on that divs one div is opened from by gridview row command, than the 2nd popup should be open from 1st popup.
i wrote a javascript method, so from that methop can i call 2nd popup too.. ans my question plz..

my code is as follows..

in .aspx page..



in this I m getting 1st popup .. from 1st popup i want to open 2nd one plz ans to my problem..

warmasree@gmail.com

Anonymous said...

hello Even both codes are not working for me.. should we import any lib files. ???
thanks in advance.

Spencer said...
This comment has been removed by the author.
Anonymous said...

How can I get the return value of javascript function???

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.