Use a HiddenField control and set its value using javascript fuction and check its value on post back to trigger a C# method. You can use the ClientID property of the control class to find the control in your javascript code..
Use window.location method in you javascript and pass a some parameters and inovke your methods by checking for those in your c# code behind..
Very first thing is if you want to call server side function using server tag inside javascript then it's not possible. You must eigher call Web Service method or page using AJAX call.
brgdotnet
Participant
1202 Points
724 Posts
How to call a C# code behind method from a Java Script function
Apr 24, 2009 03:21 AM|LINK
Is their a way to call a C# code behind method from a Java Script function? Can someone show me how to do it?
Example:
<html>
<head>
<script type="text/javascript">
function displaymessage()
{
// In this function call a C# method named DisplayInformation();
}
</script>
</head>
in C# :
void DisplayInformation(void){
// Do something in this C# method
}
qwe123kids
All-Star
48619 Points
7957 Posts
MVP
Re: How to call a C# code behind method from a Java Script function
Apr 24, 2009 04:28 AM|LINK
Hi,
U can call Session or Control in javscript
in follwing way..
'<%=Session["MyName"]%>'
'<%=Label1.text%>'
just Try
//Inline code
public string Mynamestring()
{
string strName = "";
return strName = "avinash";
}
//Method calle in javascript
'<%=Mynamestring()%>'
Hop it help
Avinash Tiwari
Remember to click “Mark as Answer” on the post, if it helps you.
esayas
Participant
1607 Points
286 Posts
Re: How to call a C# code behind method from a Java Script function
Apr 24, 2009 04:33 AM|LINK
Options...
Ram Reddy Me...
Star
9604 Points
1314 Posts
Re: How to call a C# code behind method from a Java Script function
Apr 24, 2009 04:43 AM|LINK
What actually you want to do in that method.
If the method you are saying is a server side event of a control, you can not call a server side event from client side using java script.
Abhiram Reddy Mekha
khushi_nayak
Member
694 Points
124 Posts
Re: How to call a C# code behind method from a Java Script function
Apr 24, 2009 05:00 AM|LINK
Hi,
Make the method that you want to access client side as public and then get method at client side as:
<%=MethodName()%>
Thanks,
Khushboo Nayak
Please Mark as Answered if this helps you.
Haris Alim
Member
568 Points
131 Posts
Re: How to call a C# code behind method from a Java Script function
Apr 24, 2009 05:04 AM|LINK
You can use page method approach to call C# methods from javascript.
check this link
dpkngm
Member
154 Points
47 Posts
Re: How to call a C# code behind method from a Java Script function
Apr 24, 2009 05:26 AM|LINK
Hi there, you can do it using pagemeathod. Go through the following url for full description.
http://www.dotnetcurry.com/ShowArticle.aspx?ID=109&AspxAutoDetectCookieSupport=1
deepak.nigam@swiftcybernetics.com
http://www.swiftcybernetics.com
please mark as answer if it helps u.
Coool
Contributor
4393 Points
687 Posts
Re: How to call a C# code behind method from a Java Script function
Apr 24, 2009 05:36 AM|LINK
Hi brgdotnet,
Very first thing is if you want to call server side function using server tag inside javascript then it's not possible. You must eigher call Web Service method or page using AJAX call.
Parth Patel
Software Engineer
BryianTan
Contributor
5626 Points
1083 Posts
Re: How to call a C# code behind method from a Java Script function
Apr 24, 2009 06:24 AM|LINK
hello,
you can try something like this
.aspx
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> <script type="text/javascript" language="javascript"> function TestCodeBehind() { document.getElementById('<%= Label1.ClientID %>').innerHTML = '<%= CodeBehind() %>'; return false; } function TestCodeBehind2() { document.getElementById('<%= Button2.ClientID %>').click(); // return false; } </script> </head> <body> <form id="form1" runat="server"> <div> <input id="Button1" type="button" value="Sample One" onclick="javascript:TestCodeBehind()" /> <input id="Button3" type="button" value="Sample two" onclick="javascript:TestCodeBehind2()" /> <asp:Button ID="Button2" runat="server" Text="" OnClick="ClickedME" /> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> </div> </form> </body> </html>MCP, MCAD, MCTS
My blog...
sumitd
Star
12168 Points
2151 Posts
Re: How to call a C# code behind method from a Java Script function
Apr 24, 2009 06:39 AM|LINK
<asp:Button ID="Button1" runat="server" OnClientClick="CallCodeBehind" Text="Upload" />
Add below javascript in aspx page:
<
script type = "text/javascript"> function CallCodeBehind(){
var a = '<%=CallFromJS()%>';}
</
script>Put the below method in codebehind
public string CallFromJS(){
string str = "testing"; return null;}
Visit: www.msblogdirectory.com
www.dotnetspeaks.com