Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
<script type="text/javascript">
             var myVar = setInterval(function () { myTimer() }, 1)
 function myTimer() {

 if (document.getElementById("Player").PlayState.toString() == "2")

{   <%testc();%>  }

else

{  <%testd();%> }

}
</script>


CODE BEHIND

public void testc()
 {
Label38.Text = "work"
}

 public void testd()
 {
Label38.Text = "not work";
 }
Posted
Updated 14-Oct-14 0:55am
v3
Comments
VC.J 14-Oct-14 6:56am    
why do you need to call testc() from code behind there are better way to do that
VC.J 14-Oct-14 6:57am    
Like this document.getElementById(Label38).innerText= 'work';
alienqueen 14-Oct-14 18:17pm    
i can write Label38.text value. this is not important for me.

important thing is calling c# function from javascript. this function not working.

and my answer: because i use Label38 for testing. this c# function work or not. but i see it is not working. i could not solve it


You can't call it that way because the C# executes on the server and then disconnects with the client and the JavaScript then runs on the client.

Just do as the comment suggest, use JavaScript or the jQuery library to change the textbox.

In jQuery it is very easy:
JavaScript
$("#Label38").val("whatever you want");


And if your real code is as simple as you have shown here then use one function and pass in a variable instead of 2 functions that do almost the same thing.
 
Share this answer
 
in code behind use this
C#
[WebMethod]
       public static void testc()
       {
           var abc = 1212;
       }


add script manager
XML
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>


Set EbablePageMethods to true

add an Anchor tag
<a href="javascript:testWebMethods();">More >>></a> 


function testWebMethods() {
       PageMethods.testc(onSucess);
             function onSucess(result) {
                 alert('hi' );
             }

}

this is an example to call webmethod
 
Share this answer
 
v2
my visual studio doesn't see when i write "PageMetods"
 
Share this answer
 
Hi ,

I am not sure what is your requirement , try the below options.

Add reference to System.Web.Service assembly and define the code behind methods as Public methods , then attached Web Methods attribute to expose it.

C#
using System.Web.Services;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {


        [WebMethod]
        public void testc()
        {
            Label38.Text = "work";
        }

        [WebMethod]
        public void testd()
        {
            Label38.Text = "not work";
        }
    }
}



HTML code

XML
<head runat="server">
    <title></title>

    <script type="text/javascript">
        var myVar = setInterval(function () { myTimer() }, 1)
        function myTimer() {

           <%testc();%>

}
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="Label38" runat="server" >Apple</asp:Label>
    </div>
    </form>
</body>
</html>



If you are satisfied with the reply please accept and close the question.
 
Share this answer
 
v2
Comments
VC.J 14-Oct-14 8:09am    
Mathew in web methods how can we access page controls as you have use Label inside webmethod.

please see the link
http://forums.asp.net/t/1531113.aspx?How+to+get+page+controls+in+WebMethod+ASP+Net+2+0
Mathew Soji 14-Oct-14 9:55am    
Please try the above code , It works .
Don't know your system environment , I am using VS2013 targeting .Net 4.5.
alienqueen 14-Oct-14 18:31pm    
[WebMethod] not work. I use ASP.net 4.0. i use VS2013

If someone give me webmethod sample with files, i will try this. because web methods not work
VC.J 14-Oct-14 10:10am    
ok, I have VS2010, but what my concern is webmethod has to be static and in static function how we are going to use controls(label button).
alienqueen 14-Oct-14 18:43pm    
thanks a lot for all answers but not working. any idea?

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