There is not a direct method for calling code behind functions ( but there is for calling code behind events).
If you want to call code behind function then you will have to tweek the code a bit. Pls see the below sample.
HTML
-------
<head runat="server">
<title></title>
<script type="text/javascript" language="javascript">
function CallServer() {
document.getElementById('hdnHidden').value = "1";
form1.submit();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" id="btnSubmit" value="Submit" onclick="CallServer()" />
<input type="hidden" id="hdnHidden" runat="server" value="0" />
</div>
</form>
</body>
</html>
C#
----
protected void Page_Load(object sender, EventArgs e)
{
if (hdnHidden.Value == "1")
{
//call your function here
MyFunction();
}
}
private void MyFunction()
{
//place your code here
}
Here I am using a hidden variable and checking every time on page load so as to verify whether you want to call the page behind function from js or not.
Please let me know if you have any issues.
Thanks
Please mark as answer if you find the post useful
My Blog: Akshay's Notion
the problem is i'm using javascript to process the input file, now after processing i want to upload the output to server using server code. I'm trying to figure out how to handle the output and pass it to the server.
so basically this gives a output file, now i want to upload this output file directly to server using
SubmitButton_Click function. Pls help(i'm new to javascript asp and C#)
ajeet.7
0 Points
6 Posts
use javascript to call serverside function
Apr 03, 2012 12:47 PM|LINK
I'm new to javascript and C#, trying to implement my project on these platform.
I have a query on how to execute a server side code using javascript
Code :
Task :
to encrypt the file on local machine and upload to server
can anyone please help me ?
akshay22
Participant
914 Points
184 Posts
Re: use javascript to call serverside function
Apr 03, 2012 01:22 PM|LINK
Hi Ajeet,
There is not a direct method for calling code behind functions ( but there is for calling code behind events).
If you want to call code behind function then you will have to tweek the code a bit. Pls see the below sample.
HTML ------- <head runat="server"> <title></title> <script type="text/javascript" language="javascript"> function CallServer() { document.getElementById('hdnHidden').value = "1"; form1.submit(); } </script> </head> <body> <form id="form1" runat="server"> <div> <input type="button" id="btnSubmit" value="Submit" onclick="CallServer()" /> <input type="hidden" id="hdnHidden" runat="server" value="0" /> </div> </form> </body> </html> C# ---- protected void Page_Load(object sender, EventArgs e) { if (hdnHidden.Value == "1") { //call your function here MyFunction(); } } private void MyFunction() { //place your code here }Here I am using a hidden variable and checking every time on page load so as to verify whether you want to call the page behind function from js or not.
Please let me know if you have any issues.
Thanks
My Blog: Akshay's Notion
ajeet.7
0 Points
6 Posts
Re: use javascript to call serverside function
Apr 03, 2012 02:27 PM|LINK
the problem is i'm using javascript to process the input file, now after processing i want to upload the output to server using server code. I'm trying to figure out how to handle the output and pass it to the server.
the output function(javascript)
function Save(data) { alert("Save ??"); uriContent = "data:application/octet-stream;base64," + data; document.location.href = uriContent; }so basically this gives a output file, now i want to upload this output file directly to server using SubmitButton_Click function. Pls help(i'm new to javascript asp and C#)
mudassarkhan
All-Star
78956 Points
13402 Posts
MVP
Re: use javascript to call serverside function
Apr 03, 2012 02:51 PM|LINK
To upload file using JavaScript you need to make use of Uploadify plugin
http://www.aspsnippets.com/Articles/Implement-Uploadify-jQuery-Plugin-in-ASPNet.aspx
Contact me