Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 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
Updated 26-Aug-11 18:06pm
v2
Comments
Sergey Alexandrovich Kryukov 26-Aug-11 17:06pm    
Do you mean server-side?
--SA
jiji2663 28-Aug-11 8:23am    
yes

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. =)
 
Share this answer
 
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
 
Share this answer
 
i solved it
XML
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:

XML
Response.Write("<script language=javascript>confirm(\"Are you sure to change?\");</script>");
 
Share this answer
 
v2
Comments
Nitij 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.
XML
<script type="text/javascript">
    function showMore() {
        var table = document.getElementById("<%=lblDescriptionDetail.ClientID%>");
        if (table) {
            alert("Success");
        }
    }
</script>


And call it on
More
 
Share this answer
 
I used this way.

JavaScript
JavaScript
<script type="text/javascript">
     function myFunction() {
         //some code here
         alert('Function called successfully!');
     }
</script>

Called it as: C#
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#[^]
 
Share this answer
 
v2
Comments
CHill60 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 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900