最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Calling C# function through Javascript (without Json) - Stack Overflow

programmeradmin0浏览0评论

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

Something like this:

(in Mypage.aspx)

 function callfunction()

 {

  // i have to call func() function here .....

 }

 </script>

(in Mypage.aspx.cs file)

 public void func()

 {

 // My code goes here

 }

I have researched alot because of this and i ended up so far with 2 conclusions: 1st was to use Json, but my superiors said clearly that they dont want me to do so. 2nd was that i cant do as i wish because of the client, server aspnet protocol

Please give me any kind of tip in how to do this, i am getting desperate

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

Something like this:

(in Mypage.aspx)

 function callfunction()

 {

  // i have to call func() function here .....

 }

 </script>

(in Mypage.aspx.cs file)

 public void func()

 {

 // My code goes here

 }

I have researched alot because of this and i ended up so far with 2 conclusions: 1st was to use Json, but my superiors said clearly that they dont want me to do so. 2nd was that i cant do as i wish because of the client, server aspnet protocol

Please give me any kind of tip in how to do this, i am getting desperate

Share Improve this question asked Jul 1, 2013 at 10:26 Chazz1Chazz1 1331 gold badge4 silver badges18 bronze badges 3
  • You should use Ajax on it.. It is very easy to implement.. – S. S. Rawat Commented Jul 1, 2013 at 10:30
  • 1 no need for json you can call an event handler of any hidden button of aspx using _postback() – Ali Commented Jul 1, 2013 at 10:34
  • Hum maybe using postback would be a solution from now, but i dont know if i need more specific way to do this – Chazz1 Commented Jul 1, 2013 at 11:00
Add a ment  | 

3 Answers 3

Reset to default 6

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

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
  }
}

below are the options available to you

  1. If your using asp 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)

  1. 暂无评论