0

I am trying to access a C# from my ASPX page that is already called from a web part for sharepoint foundation 2010. I am following instructions from the post here

My breakpoint in the function isn't getting hit when I debug.

.aspx

<input type="submit" id="SEND" value="Valider" class="btn btn-success" onclick="ValiderDemande()" runat="server"/>
<script type="text/javascript">
        function ValiderDemande() {
            $.ajax({
                type: "POST",
                url: "AjouterDemande.aspx/ValiderDemande",
                success: function (msg) {
                window.frameElement.commitPopup();

                },
                error: function (e) {
                }
            });
        }
</script>

.aspx.cs

 [WebMethod]
    public static void ValiderDemande() 
    {
        string sssss = string.Empty;
        Thread.Sleep(2000);

    }
4
  • Can you post a code sample here ? Sep 14, 2015 at 14:28
  • i updated the post Sep 14, 2015 at 15:02
  • Where is this page stored "AjouterDemande.aspx"? Sep 14, 2015 at 15:34
  • it's the same page Sep 14, 2015 at 15:40

3 Answers 3

0

Have you put console log messages your javascript and checked which parts of the javascript are actually executing?

7
  • it give nothing Sep 14, 2015 at 15:50
  • In that case, the problem is not on the server side at all. The problem is in your javascript not getting called, so you need to fix your code to actually call the javascript.
    – user46971
    Sep 14, 2015 at 15:56
  • Oh, I see your problem, I think. It appears that you have specified OnClick for a server-side control; that is to call code on the server side. You have code on the client-side, so you want OnClientClick.
    – user46971
    Sep 14, 2015 at 15:57
  • it work but without ajax call Sep 14, 2015 at 16:10
  • You would want to put some sort of console log message into that error message handler, right?
    – user46971
    Sep 14, 2015 at 16:21
0

The example you're referring to is for ASP webforms. This won't work for a SharePoint webpart. An option would be to create a mapped layouts folder in your solution where you'd create an application page. Then you'd refer to it like so:
url: "../_layouts/15/Project/AjouterDemande.aspx/ValiderDemande" where the URL would be typically according to the hierarchy in your Layouts in your solution / 15 hive.

This is how I had my code behind and JS:

JS:

$('#btn').click(function () {
             $.ajax({
                 type: "POST",
                 url: "../_layouts/15/Project/ApplicationPage1.aspx/GetMessage",
                 data: "{}",
                 contentType: "application/json; charset=utf-8",
                 dataType: "json",
                 async: true,
                 cache: false,
                 success: function (msg) {
                     console.log(msg);
                                 }
             })
             return false;
         });

CodeBehind:

In ASPX page:

    [WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public static string GetMessage()
    {
        return WebPart.WebPart1UserControl.GetMessage(); // WebPart is under Project
    }

In WebPart:

    public static string GetMessage()
    {
        return "Dummy Message";
    }

This output "Dummy Message" to the console.

3
  • sorry but i did not understand your solution (create mapped folder, my sharepoint solutio has already a layout folder that i put my aspx file in it ) Sep 15, 2015 at 7:50
  • Then no need to create that. Just make sure that the reference to your aspx page is with reference to layouts as shown in my answer. Create a method in your ASPX file which calls a method in your webpart. In your webpart method, return the string. What part did you not understand?
    – Akhoy
    Sep 15, 2015 at 8:24
  • social.msdn.microsoft.com/Forums/sqlserver/en-US/… i have the same issue it call Page_Load() instead of my webmethode function Sep 15, 2015 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.