How to call apsx.cs mthod(C#) from javascript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abdunnabisk
    New Member
    • Jan 2008
    • 17

    How to call apsx.cs mthod(C#) from javascript?

    Any body has any idea on How to call apsx.cs mthod(C#) from javascript?

    Thanks
    Abdun Nabi Sk
  • Mr Gray
    New Member
    • Apr 2008
    • 47

    #2
    Depends what you are trying to achieve?

    Comment

    • abdunnabisk
      New Member
      • Jan 2008
      • 17

      #3
      can u show me some examples?

      thanks..
      Abdun Nabi Sk

      Comment

      • Mr Gray
        New Member
        • Apr 2008
        • 47

        #4
        This is the standard bit of JScript that is in an aspx page but generally you do not send javascript to interact with methods in the cs file:

        Code:
        <script type="text/javascript">
        <!--
        var theForm = document.forms['aspnetForm'];
        if (!theForm) {
            theForm = document.aspnetForm;
        }
        function __doPostBack(eventTarget, eventArgument) {
            if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
                theForm.__EVENTTARGET.value = eventTarget;
                theForm.__EVENTARGUMENT.value = eventArgument;
                theForm.submit();
            }
        }
        // -->
        </script>

        Comment

        • abdunnabisk
          New Member
          • Jan 2008
          • 17

          #5
          actually I am having a method name
          [code=cpp]
          public void Check(int n)
          {
          }
          [/code]
          in aspx.cs file.

          in javascript
          [code=javascript]
          function Test(n)
          {
          <%Check(n)%>;
          }
          [/code]
          its not calling Check() method....
          There are errors like syntax

          and if I dont want to send argument then no error but
          every time i have to refresh the page...

          can u help me

          Thanks
          Abdun Nabi SK
          Last edited by Frinavale; Apr 16 '08, 01:14 PM. Reason: added code tags

          Comment

          • Mr Gray
            New Member
            • Apr 2008
            • 47

            #6
            In your case you should probably have a submit button that runs that code. In order to run cs code you need to post to the server to run that code.

            So in the aspx page you should add an:

            Code:
            <asp:button id="btnSubmit" runat="server" Text="Submit" />
            <asp:TextBox id="txtCheck" runat="server" />
            In the cs file in the Initialise() method you can add the eventhandler for the button like so:

            Code:
            btnSubmit.Click += new EventHandler(Check);
            Then in the Check() method you created you can access the text field like so:

            Code:
            public void Check(object s, EventArgs e)
            {
                string strCheck = txtCheck.Text;
            
                // operate on strCheck here
            }
            Hope this example will help get you started.

            Comment

            • abdunnabisk
              New Member
              • Jan 2008
              • 17

              #7
              I have button that is calling
              javascrip:paren t.Test(2)

              method.....
              anyway I have to call c# method from
              javascript method Test().

              please help me
              thanks

              Abdun Nabi Sk

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Originally posted by abdunnabisk
                I have button that is calling
                javascrip:paren t.Test(2)

                method.....
                anyway I have to call c# method from
                javascript method Test().

                please help me
                thanks

                Abdun Nabi Sk
                You can't really use JavaScript for this.
                You're going to have to use Ajax to call server side code.

                Are you using Ajax in your web project currently?
                If you use Ajax, the page is "partially" posted back to the server, your request is processed and that section of the page is updated.

                You can use pure Ajax to do this if you want to, but .NET now has an Ajax framework that simplifies things.


                Instead of putting in Ajax calls to do validation, you should consider using the client side Validator objects available to you (eg RequiredFieldVa lidator, RegularExpressi onValidator ...etc).

                You should still validate on the server....I would place a call to your Check() method in your button click event and if it return's true, would allow further processing to take place.

                -Frinny

                Comment

                • antonmaju
                  New Member
                  • Apr 2008
                  • 7

                  #9
                  I'm agree with what Frinavale said.
                  But if you really want to do validation via js then you may use PageMethods with ASP.NET Ajax.

                  Comment

                  Working...