How To Call C# Function From JavaScript
-
Hi, This is from Chandrakanth. I would like to call a function which is in C#, From JavaScript. How can i go for that? Can any one give me repply for this. Thanks And Regards chandrakanth
Chandrakanth
Unfortunately, this isn't very straightforward - js runs in the browser on the client, the C# code on the server. To call anything on the server you'd need to use AJAX to make a call back to the server. I tend to do this with a callbacks page - straight apsx page, no markup, and a big ol switch statement in page load, which looks for a querystring param which indicates the method in the callbacks file I wanna call...
C# has already designed away most of the tedium of C++.
-
Unfortunately, this isn't very straightforward - js runs in the browser on the client, the C# code on the server. To call anything on the server you'd need to use AJAX to make a call back to the server. I tend to do this with a callbacks page - straight apsx page, no markup, and a big ol switch statement in page load, which looks for a querystring param which indicates the method in the callbacks file I wanna call...
C# has already designed away most of the tedium of C++.
-
No, Ko Ye Htut. The question that he was asking is about calling the server-side function from client-side. It's true that
document.getElementbyID("IDName").Click()
can invote the server-side click event. but your code won't work if he wants to call the normal server-side function (e.g. public void MyMethod(){ })YeHtut wrote:
FireEvent
You can't just fire the event to invote the server-side method from client-side script.. but it is possible to do with Ajax.
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)
-
Hi, This is from Chandrakanth. I would like to call a function which is in C#, From JavaScript. How can i go for that? Can any one give me repply for this. Thanks And Regards chandrakanth
Chandrakanth
Hi, you can try this. Make Javascript function like this.
function GetClientScript() { var str= "My test"; var test = '<%= GetFunction()%>'; alert(test); }
Here GetFunction() is C# function. This function must return something else this cant work. Now call this function onClientClick of any asp.net linkbutton or button. And here you are..Thanks, Sun Rays To get something you must have to try once. My Articles
-
Hi, This is from Chandrakanth. I would like to call a function which is in C#, From JavaScript. How can i go for that? Can any one give me repply for this. Thanks And Regards chandrakanth
Chandrakanth
-
U try to do like this. This is written in code behind file. public void ServerSideFunction() { ... } This javascript is I was written in Aspx file. <script> <%ServerSideFunction;%> </script> I already test and work it.
-
U try to do like this. This is written in code behind file. public void ServerSideFunction() { ... } This javascript is I was written in Aspx file. <script> <%ServerSideFunction;%> </script> I already test and work it.
Seem like you are confusing. The server-side script can be written in both class file (e.g. .cs or .vb) and aspx file. But if we want to write the server-side script in aspx file, you have to use <% %>. So, the way that you showed is just writing the serverside code in aspx file. And it doesn't mean that you are calling the serverside function from client-side ( Javascript or vbscript or etc). still confused? Okay. Let's say.. I want to call this function ( ServerSideFunction ) from Javascript when I click the button ( input button. not server-control). So,
ServerSideFunction()
should be invoked only when I click the button. (not on pageload). How to do that? :)Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)
-
Seem like you are confusing. The server-side script can be written in both class file (e.g. .cs or .vb) and aspx file. But if we want to write the server-side script in aspx file, you have to use <% %>. So, the way that you showed is just writing the serverside code in aspx file. And it doesn't mean that you are calling the serverside function from client-side ( Javascript or vbscript or etc). still confused? Okay. Let's say.. I want to call this function ( ServerSideFunction ) from Javascript when I click the button ( input button. not server-control). So,
ServerSideFunction()
should be invoked only when I click the button. (not on pageload). How to do that? :)Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)
I think so, anyway should be postback and call the Server Side function. Otherwise can not be called the server side function. AJAX is called partial postback and call the server function. So Main page is never refresh. I know like this. If you know that ow to do with other way, please share it to me. Thanks for best suggest. :)
-
I think so, anyway should be postback and call the Server Side function. Otherwise can not be called the server side function. AJAX is called partial postback and call the server function. So Main page is never refresh. I know like this. If you know that ow to do with other way, please share it to me. Thanks for best suggest. :)
As I said in this post[^], Ajax is the only solution for this problem. I was pointing out the mistake that you wrote in this post[^]. Thank you. :)
Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)