Click here to Skip to main content
15,914,447 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi


I am using the asp.net and C#.net and Javascript, I had written a function like "callmessage()" in javascript and now iam trying to call that function from C#.net
, i had tried in lot of ways , here i will write the sample code

case1:

<script type="text/javascript">
function callmessage1() {
alert('hello');
} 
</script>


in serverside code in backend from C#.net

C#
 protected void Button1_Click(object sender, EventArgs e)
    {        HttpContext.Current.Response.Write("<script>callmessage1();</script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "btn", "<script type = 'text/javascript'>callmessage1();</script>");//siva
 ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", "callmessage1();", true);
}



this is not working

but when i do like this it was working
case2:



C#
 protected void Button1_Click(object sender, EventArgs e)
    {
HttpContext.Current.Response.Write("<script>alert('its working');</script>");

ClientScript.RegisterClientScriptBlock(this.GetType(), "btn", "<script type = 'text/javascript'>alert('its working');</script>");//siva
 ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", "alert('its working');", true);
} 


can anyone please specify me the Mistake where i am doing , just i need to call the function,moreover in another project the same code is working , and i am getting the best result, but why in this project

anything i am missing...........


is there any chance of inline javascrift code in C#.net

Please help out from here
thank Q..!
Posted
Updated 26-Oct-12 1:39am
v2

You can call javascript function from c# code.
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "showNotify", "YourJavascriptMethod", true);

You don't need to include script tags there as the last parameter 'true' will automatically add them.
 
Share this answer
 
Okay .. explainantion is simple ..

How a code from your ASPX.CS file will come to know about your callmessage1() function which is written in your ASPX file.

If you want to run a JS from code behind in that case
y

you have write entire script in .CS file

OR

Add this function in HTML tag of your button like

ASP.NET
<asp:button id="Button1" usesubmitbehavior="false" onclientclick="javascript:callmessage1(); /> </pre></xml>" xmlns:asp="#unknown"></asp:button>
 
Share this answer
 
Comments
[no name] 26-Oct-12 7:18am    
Can u please write a sample function in .cs file ,so that i can understand much better
Thanks For ur Replay

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