Calling C# function through Javascript (without Json)

前端 未结 3 1851
走了就别回头了
走了就别回头了 2021-01-14 06:19

I have a function named \"callfunction()\" in JavaScript(Mypage.aspx) .This function should call another function \"func()\" in C# (Mypage.aspx.cs )

Something like

相关标签:
3条回答
  • 2021-01-14 06:44

    If it's a webforms project (not MVC) and you don't want to use AJAX, you can use __doPostBack.

    <script type="text/javascript">
     function callfunction(parameter)
     {
         __doPostBack('func', parameter)
     }
    </script>
    

    C#:

    public void Page_Load(object sender, EventArgs e)
    {
      string parameter = Request["__EVENTARGUMENT"]; // parameter
      var senderObject = Request["__EVENTTARGET"]; // func
      if(senderObject == "func")
      {
         //call your function here, or write the implementation
      }
    }
    
    0 讨论(0)
  • 2021-01-14 06:58

    below are the options available to you

    1. If your using asp.net then use Ajax tools to create this

    2. if you don’t want to user Ajax toolkit use JavaScript __doPostBack

    3. or other option write server side function in the web service and call web method using JavaScript

    0 讨论(0)
  • 2021-01-14 07:04

    Ok....Try using page methods

    First add a script manager on your aspx page

      <asp:ScriptManager ID="scpt" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
    

    Then go to your aspx.cs page and declare a function something like

    [System.Web.Services.WebMethod]
        public static string ValidateUser(string emailId, string password)
        {
            //Your logic code
            return returnString;
        }
    

    Then from your javascript call the c# method like

     PageMethods.ValidateUser(email, password, CallSuccess_Login, CallFailed_Login);
    

    And also in ur javascript create 2 call back functions CallSuccess_Login and CallFailed_Login

    Hope it helps

    0 讨论(0)
提交回复
热议问题