简体   繁体   中英

how to call C# class.cs in ASP.NET without Code Behind

I don't know if it is possible, I tried many ways but nothing seems to work in the project I am working on.

Here is the deal : I've got an ASPX page that need to call a Web Service when a button is click. I can't use code behind and Page Method because my page can't inherit from

System.Web.UI.Page

And the convention at my work doesn't let us use code behind.

I've tried calling a ".cs" class with method like Ajax (Asynchronously and synchronously) with the "XMLHttpRequest". In my stand alone it worked fine but when I integrate it in the project it did not work (Iframe inside Iframe inside Iframe inside Master Page).

I can't use things like external Libraries (Jquery, ...). I'd like to know if inside the ASPX page I'm working on I can call with some other way my class.cs with the code I try to call.

I have :

<input type="button" onclick="callSomeJSfunction()" value="testC#" />

My JavaScript do some treatment (getting the text I need without all the DOM component like , , and only keep text inside

) then is supposed to call the server part with the call for the Web Service (I can't call the Web Service from JS because of the cross domain Issue).

And my class.cs calling the Web Service is :

namespace testProlexis

{
    public class prolexisWSCorrector
    {
        public static string callProlexis(string textToCorrect)
        {
            if (string.IsNullOrEmpty(textToCorrect))
            {
                throw new Exception("pas de texte à corriger pour Prolexis");
            }
            prolexisWebServiceImpl.ProLexisService test = new prolexisWebServiceImpl.ProLexisService();
            prolexisWebServiceImpl.AnalyzerInput inPut = new prolexisWebServiceImpl.AnalyzerInput();
            prolexisWebServiceImpl.AnalyzerOutput outPut = new prolexisWebServiceImpl.AnalyzerOutput();

            inPut.text = textToCorrect;
            String info ="try5";
            outPut = test.analyze(inPut, working);
            int tytytyty = outPut.errors.Count();
            return textToCorrect;
}

I have no more idea how to do it, I'm a beginner in ASP.NET (I used to work in JEE). So if anyone has an idea or some tutorial that I can look to help me I'll gladly take it.

Thanks for your time trying to help me.

If you are trying to use C# in your aspx page without a code behind (not recommended) you can just add it in a script tag in your aspx page.

<script language="C#" type="text/C#" runat="server">
    public void DoStuff(object sender, EventArgs e)
    {
        var proxy = new prolexisWSCorrector();
    }
</script>

<asp:Button ID="" runat="server" Click="DoStuff" />

Or

<input type="submit" runat="server" onClick="DoStuff" />

In your Aspx form, javascript function like below,

         function callSomeJSfunction() {
               jQuery.ajax({
                        type: "POST",
                        url: "YourForm.aspx/YourPublicStaticWebMethod",
                        data: "{'ID': '" + txtID.value+ "','Value': '" + txtYear.value '}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (msg) {
                           /*Do Success*/
                              },
                       Error:function(er){
                            /*Do Error*/
                              }
                          });
                                       }

In your Aspx Form, code behind,

[System.Web.Services.WebMethod]
public static Function YourPublicStaticWebMethod(int id,string year)
{
 //Code Here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM