Click here to Skip to main content
11,477,625 members (75,639 online)
Rate this: bad
good
Please Sign up or sign in to vote.
How can I run function of JavaScript in ASP.Net server side
or how can I run function in side server in JavaScript function
Please F1! F1! F1! F1!
I am working with C#
Posted 26-Aug-11 11:56am
jiji26631.8K
Edited 26-Aug-11 19:06pm
v2
Comments
SAKryukov at 26-Aug-11 17:06pm
   
Do you mean server-side?
--SA
jiji2663 at 28-Aug-11 8:23am
   
yes
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

this post might be too late of a response but this will still be helpful for those future visitors who happen to visit this site looking for solutions.

well i agree with Sergey, however as to what i see and understand, jiji might not really have a clear idea about the client-server model and by which computer languages (native and interpreted/scripts) run on the server side and on the client side. Honestly, the problem posted by jiji was confusing. Now let's make it clear.

Asp.net is a scripting technology and it runs on the server side. It is capable of generating data in text or even in binary format. JavaScript on the other hand is also a scripting technology but which runs on the client side. Commonly Javascript doesn't run on the server side (but in the past there were versions of javascript that run on the server).

The problem i think is 'how to incorporate javascript into asp.net controls'.
Remember, Javascript runs on the client side and it is 'generated as output' by your asp.net script.

Note:
generated as output - when asp.net script generates an output it will be passed to another object that is capable of interpreting it. In the client-server model (in the context of programming), asp.net script generated output is interpreted by the client's browser and it is the browser that understands and capable of running javscript.

Hope this little explanation of mine would help those future visitors seeking for details and explanations or even solutions. =)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There is such thing as Server-side JavaScript.

See:
http://en.wikipedia.org/wiki/Server-side_JavaScript[^],
http://en.wikipedia.org/wiki/Comparison_of_server-side_JavaScript_solutions[^].

I don't think you really need it. On ASP.NET on the server side, you have a full power of .NET where you can use any compiled language and powerful debugging.

Also, I don't think Server-side JavaScript is a common solution these days. I think it's quite rare. On the client side JavaScript is the king on the mountain (or actually the only one platform/browser compatible scripting language), but on the server side it hardly can compete with more powerful ASP.NET, Python, Ruby, etc. I wonder why would you need JavaScript?

—SA
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

i solved it
protected void Page_Load(object sender, EventArgs e)
    {
        String script = @"<script language=""Javascript"">
                        function TestJScript()
                        {
                        alert('Just for testing');
                        }
                        </script>";
        Page.RegisterClientScriptBlock("experiment", script);
        Button3.Attributes.Add("onClick", "TestJScript()");
 
           }

when you click on the Button3 the javascript function is running


and another way:

Button3.Attributes.Add("onclick", "javascript:alert('Testing Macro Functionality');");

and another way:

Response.Write("<script language="javascript">confirm(\"Are you sure to change?\");</script>");
  Permalink  
v2
Comments
Nitij at 13-Mar-15 8:09am
   
Your script tag will be written to the web page and the function will be called from there, and not from the server.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

<script type="text/javascript">
    function showMore() {
        var table = document.getElementById("<%=lblDescriptionDetail.ClientID%>");
        if (table) {
            alert("Success");
        }
    }
</script>

And call it on
More
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

I used this way.

JavaScript
<script type="text/javascript">
     function myFunction() {
         //some code here
         alert('Function called successfully!');
     }
</script>
Called it as: C#
protected void btnServerSide_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this, GetType(), "myFunction", "myFunction();", true);            
} 

Refer: Call JavaScript Function From Code-behind Using C#[^]
  Permalink  
Comments
CHill60 at 13-Mar-15 4:56am
   
Please don't resurrect 3 year old questions just to post a link to your own blog. At best it will attract downvotes, at worst it will be reported as Spam/Abuse.
F-ES Sitecore at 13-Mar-15 5:53am
   
The language you are using is incorrect and only leads to further confusion from those learning to code. Your code is not showing people how to call javascript from code-behind, it is showing how to add javascript to the page from code-behind...the execution still takes place on the client. People ask how to call js from code-behind normally because they don't understand the client\server model and think it is possible to utilise the client while their .net code is executing. Articles like yours that claim to show how to call js from code-behind only adds to their confusion and makes them think it is possible.
CHill60 at 13-Mar-15 7:12am
   
Virtual 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 239
1 Richard Deeming 225
2 Tomas Takac 150
3 Sascha Lefèvre 145
4 _duDE_ 140
0 Sergey Alexandrovich Kryukov 7,675
1 OriginalGriff 7,256
2 Sascha Lefèvre 3,034
3 Maciej Los 2,491
4 Richard Deeming 2,305


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 13 Mar 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100