Dotnetplace.com

.NET Articles, Web Devploment, ASP.NET, HTML, Sharepoint 2010, Aamir Hasan
Advertise Here
Advertise Here
Advertise Here
Advertise Here
Advertise Here
Advertise Here
Advertise Here
Advertise Here
Advertise Here

Advertise

Loading...


Posted by Mairaj   on Sunday, June 10, 2012 Total Views:  

Sometimes you want to call javascript finctions from code behind.In this case you would use ClientScriptManager to do this.Let’s see an example.Here we would call a javascript function which shows an alert.Create a function in javascript ShowAlert().On Page_Load we will call this function.

Desgin code

<html>

<head runat="server">

    <title></title>

    <script type="text/javascript">

        function ShowAlert() {

            alert('This is being called from code behind');

        }

    </script>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    </div>

    </form>

    <p><a href="http://www.dotnetplace.com">ASP.NET controls with jquery</a></p>

</body>

</html>

 

Code behind

Now we will call the above javscript method from server side by using RegisterStartupScript

 

   protected void Page_Load(object sender, EventArgs e)

    {

        ClientScriptManager cs = Page.ClientScript;

        cs.RegisterStartupScript(this.GetType(), "script", "ShowAlert();", true);

    }

 

Here you would have noted that the RegisterStartupScript method of ClientScriptManager class accepts four parameters.

  1. Control that registers the Script. Here page is registerig the script.
  2. This could be any key you want to write but make sure another script is not registered with this key.
  3. The script to be executed.Here you can write name of method to be executed or you can write script directly.
  4. Script tags to be added.true for yes false for not.


Microsoft Reference

ClientScriptManager.RegisterStartupScript Method


Ouput

Download

Call-javascript-function-from-server.zip (980.00 bytes)

Demo

See live demo

Protected by Copyscape Online Plagiarism Tool

Comments (2) -

mairajahmad
mairajahmad United States
7/5/2012 8:04:58 PM #

fdfdfdfdfdf

mac makeup store
mac makeup store United States
10/14/2014 9:51:25 AM #

Can I simply say what a reduction to seek out somebody who actually is aware of what theyre speaking about on the internet. You definitely know tips on how to deliver a problem to light and make it important. More individuals have to learn this and understand this facet of the story. I cant consider youre no more common because you positively have the gift.

mac cosmetics discount
mac cosmetics discount United States
11/1/2014 9:48:14 AM #

An interesting dialogue is value comment. I think that you should write more on this subject, it may not be a taboo topic however usually individuals are not sufficient to speak on such topics. To the next. Cheers

mac cosmetics outlet
mac cosmetics outlet United States
11/3/2014 5:09:35 AM #

The subsequent time I read a blog, I hope that it doesnt disappoint me as a lot as this one. I imply, I know it was my choice to read, however I really thought youd have something interesting to say. All I hear is a bunch of whining about one thing that you could possibly repair when you werent too busy looking for attention.

mac cosmetics wholesale
mac cosmetics wholesale United States
11/3/2014 2:00:15 PM #

The subsequent time I learn a weblog, I hope that it doesnt disappoint me as much as this one. I imply, I do know it was my choice to learn, however I really thought youd have something interesting to say. All I hear is a bunch of whining about one thing that you might repair when you werent too busy on the lookout for attention.

mac makeup kit
mac makeup kit United States
11/5/2014 2:13:58 PM #

After examine just a few of the weblog posts in your website now, and I truly like your approach of blogging. I bookmarked it to my bookmark website record and will be checking again soon. Pls take a look at my web site as properly and let me know what you think.

wholesale mac makeup
wholesale mac makeup United States
11/9/2014 3:22:30 AM #

I'am impressed, I have to say. Really rarely do I encounter a weblog thats both educative and entertaining, and let me tell you, you have got hit the nail on the head. Your thought is outstanding; the issue is one thing that not sufficient people are speaking intelligently about. I am very completely satisfied that I stumbled across this in my search for something relating to this.

mac cosmetics wholesale
mac cosmetics wholesale United States
11/19/2014 6:34:36 AM #

Good post. I be taught one thing more difficult on completely different blogs everyday. It'll all the time be stimulating to read content material from other writers and follow a bit one thing from their store. I'am choose to use some with the content material on my blog whether or not you don't mind. Natually I�ll provide you with a hyperlink in your internet blog. Thanks for sharing.

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Advertizement 1
Advertizement 2
Advertizement 3
Advertizement 4
Advertizement 5