I want to call javascript function from aspx.cs. In Button click I will check for some conditions, corresponding to that conditions I have to call javascript function. Is it possible.
No, you can't call a javascript function from your code-behind. At the point that your code-behind is being processed, the page, and therefore the javascript has not yet been created, so it doesn't exist. Once the response is sent to the browser, and the
javascript exists, your code-behind no longer exists in the context of that particular page. Create a client-side button click handler, and use javascript to test the condition and call the other javascript function if necessary.
You can call an alert statement from ur code behind.
Strictly speaking, your example doesn't call an alert from the code behind. The javascript code is created in the code-behind as a string of text to be written to the page and sent to the browser with the rest of the page, where the javascript executes.
This question - "calling" client-side functions from server-side code - comes up all the time, and the answer is always the same. You can't.
9 times out of 10 the question is asked because the questioner doesn't really understand the flow of execution between server and client. They imagine that there is some sort of umbilical cord between their aspx.cs or vb file and the page that has been
rendered on some user's computer somewhere. There isn't. You can use javascript to execute other javascript on the client. You can use javascript to execute server-side code and utilise the response from that server-side execution (AJAX). But server-side
code will only ever execute in response to a request from the client. It cannot call, or initiate communication with anything on a page that has been rendered.
As your example shows, you can even emit javascript from the code behind so that when the page renders, it is executed on the client as soon as the body loads. You can use Page.ClientScript.RegisterStartUpScript to do the same thing, but you still can't
call javascript from server-side code.
9 times out of 10, we also discover that the questioner could and should do what they need purely through client-side javascript.
atayfun:
All that code does is show or hide a div element client-side upon a button press.
Here is what was asked for: I want to call javascript function from aspx.cs. In Button click I will check for some conditions, corresponding to that conditions I have to call javascript function. Is it possible.
As Mikesdotnetting pointed out, you can't really do that, as one is server-side, before the page is rendered, and the other is client-side, after the page is rendered.
The best that you can do is something like this: private void Button1_Click(object sender, System.EventArgs e)
{
string javaScript =
"<script language=JavaScript>\n" +
"alert('Button1_Click client-side');\n" +
"</script>";
sanitha
0 Points
3 Posts
call javascript function from C#
Jun 01, 2007 08:27 AM|LINK
Hi,
I want to call javascript function from aspx.cs. In Button click I will check for some conditions, corresponding to that conditions I have to call javascript function. Is it possible.
Pls help me.......
Thanks in advance.
Mikesdotnett...
All-Star
155597 Points
19981 Posts
Moderator
MVP
Re: call javascript function from C#
Jun 01, 2007 12:03 PM|LINK
Web Pages CMS | My Site | Twitter
Nitinkcv
Member
710 Points
264 Posts
Re: call javascript function from C#
Jun 01, 2007 12:20 PM|LINK
Hi,
You can call an alert statement from ur code behind. But i am not sure if other things are possible.
Please check this link
http://www.developer.com/net/asp/article.php/1594521
Mikesdotnett...
All-Star
155597 Points
19981 Posts
Moderator
MVP
Re: call javascript function from C#
Jun 01, 2007 12:54 PM|LINK
This question - "calling" client-side functions from server-side code - comes up all the time, and the answer is always the same. You can't.
9 times out of 10 the question is asked because the questioner doesn't really understand the flow of execution between server and client. They imagine that there is some sort of umbilical cord between their aspx.cs or vb file and the page that has been rendered on some user's computer somewhere. There isn't. You can use javascript to execute other javascript on the client. You can use javascript to execute server-side code and utilise the response from that server-side execution (AJAX). But server-side code will only ever execute in response to a request from the client. It cannot call, or initiate communication with anything on a page that has been rendered.
As your example shows, you can even emit javascript from the code behind so that when the page renders, it is executed on the client as soon as the body loads. You can use Page.ClientScript.RegisterStartUpScript to do the same thing, but you still can't call javascript from server-side code.
9 times out of 10, we also discover that the questioner could and should do what they need purely through client-side javascript.
Web Pages CMS | My Site | Twitter
atayfun
Member
2 Points
2 Posts
Re: call javascript function from C#
Jun 08, 2007 03:40 PM|LINK
---
atayfun
Member
2 Points
2 Posts
Re: call javascript function from C#
Jun 08, 2007 04:02 PM|LINK
---------------------------
<
asp:Button ID="btnHide" Runat="server" Text="Hide" /> <asp:Button ID="btnShow" Runat="server" Text="Show" /><
script language="javascript" type="text/javascript"> function hideButton() {document.getElementById(
"divAbc").style.visibility = "hidden"; return false;}
function showButton() {document.getElementById(
"divAbc").style.visibility = ""; return false;}
</script><
div id="divAbc"> <table><tr><td style="width: 200px; height: 100px; background-color: red"></td></tr></table></
div>------------------------------------------------------------------------------------------------ .cs codebehind page_load
---------------------------
protected void Page_Load(object sender, EventArgs e)
{
btnHide.Attributes.Add(
"onclick", "return hideButton()"); btnShow.Attributes.Add("onclick", "return ShowButton()");}
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: call javascript function from C#
Jun 08, 2007 05:23 PM|LINK
atayfun:
All that code does is show or hide a div element client-side upon a button press.
Here is what was asked for:
I want to call javascript function from aspx.cs. In Button click I will check for some conditions, corresponding to that conditions I have to call javascript function. Is it possible.
As Mikesdotnetting pointed out, you can't really do that, as one is server-side, before the page is rendered, and the other is client-side, after the page is rendered.
The best that you can do is something like this:
private void Button1_Click(object sender, System.EventArgs e)
{
string javaScript =
"<script language=JavaScript>\n" +
"alert('Button1_Click client-side');\n" +
"</script>";
RegisterStartupScript("Button1_ClickScript", javaScript);
}
But the client-side script is ONLY going to execute AFTER all server-side processing is completed.
NC...
iyerrohit
Member
4 Points
2 Posts
Re: call javascript function from C#
Jul 25, 2008 04:52 AM|LINK
Excellent answer @ above person...........................
It worked in my app........................
tcatu
Member
37 Points
44 Posts
Re: call javascript function from C#
Sep 16, 2008 11:35 AM|LINK
hi I wanna call my js function called "check" from aspx.cs in a button_click1..
so I tried the upper massaeges codes. but I can not succesed.
So anyone can write again according my codes ?
thnx
ASP.NET 2.0 asp.net javascript button click asp.net Webservice atlas ASP.NET Javascript onclick asp.NET 2.0 textbox clearing asp.net authentication forms javascript modal asp net javascript save changes prompt asp.net 2.0 javaScript "web Service" ASP.Net 2.0
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: call javascript function from C#
Sep 16, 2008 02:32 PM|LINK
What does mistake is related RegisterStartupScript mean?
NC...