Click here to Skip to main content
15,917,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Good day,

How can I call java script from C#? I tried Response.Write() and Page.ClientScript... but still I can't make it work. The javascript is a pop up messagebox

<script>
$.msg({
bgPath: '../Pictures/Images/',
content: '

Server Error

',
klass: 'white',
fadeIn: '200',
fadeOut: '100',
timeOut: '10000'
});
</script>


I want to call this in C# button click event

Thank you
Posted

I Suggest U to put this code in a javascript function, like this :

JavaScript
<script>

function myFunction() {
$.msg({
bgPath: '../Pictures/Images/',
content: '
Server Error

',
klass: 'white',
fadeIn: '200',
fadeOut: '100',
timeOut: '10000'
});

}
</script>


call this function in c# like this :

C#
// if u don't have ScriptManager on your page then like this :

ClientScript.RegisterStartupScript(GetType(), "js", "myFunction();", true);

// if u have ScriptManager on your page then like this :

ScriptManager.RegisterStartupScript(Page, Page.GetType(), "js", "myFunction();", true);
 
Share this answer
 
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Jul-13 0:38am    
This page you referenced is misleading, so is your answer. Even though it should work, it has nothing to do with "calling" of a Javascript function from code behind. Javascript works on the client side, and C# — on the server side; so one cannot call another. The idea is: JavaScript code is generated in HTTP response.
—SA

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