locked
Call C# function from Javascript RRS feed

  • Question

  • I have a [OK] button for attachment after a file is selected. When I click the [OK] button, it will call the "OkAttach()" JavaScript function to upload the file. Then user can click the [Save] button which all C# function to save the item.

    Now, the client think this is too much clicks. They want to combine the [OK] button and [Save] button to one button. When users click the [OK] button, it should upload the file and save the item. The upload function is in JavaScript (the default SharePoint function) while the [Save] function is in C#. I have research on the web and found two solutions:

    (1) Use pure JavaScrip. In side the "OkAttach()" JavaScript function, I added

    var OKbutton = document.getElementById('<%=btSave5%>');

    OKButton.click();

    I do not see it does any thing (C# function btSave is not perform).

    (2) Use PageMethods.btSave_Click(); It also seems does do anything other than upload the file. I think PageMethods is not available. It does not allow me to put another ScriptManager in the front-end form "Only one ScriptManager is allowed". However, I cannot find where it declaim ScriptManager.

    Please provide me a method (will be better in code) that will allowed me to call C# function from JavaScript function. Many thanks.

    Tuesday, March 27, 2018 8:47 PM

Answers

  • Hi,

    If the asp:button code like this

    <asp:Button id="btSave" Text="Save" OnClick="btSave_Click" runat="server"/>

    we can use the jQuery code below to click button.

    $("input[id$='_btSave']").click();

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by eg10013 Thursday, March 29, 2018 1:36 PM
    Thursday, March 29, 2018 9:51 AM

All replies

  • Hi eg10013,

    you can implement WebMethod ASMX /OR/ Custom WCF and Call it using AJAX Call

    Consume WCF REST Service using jQuery

    Call ASMX Web Service from jQuery Ajax

    Consuming WCF REST Services Using jQuery AJAX Calls

    $(function() {
        $('#btnCallService').click(function() {
            $.ajax({
                type: 'POST',
                url: 'HelloService.asmx/GetEmployeeDetail',
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                success: function(response) {
                    $('#lblData').html(JSON.stringify(response));
                },
                error: function(error) {
                    console.log(error);
                }
            });
        });
    });

    Hope this will help you


    Best Regrads, Ahmed Madany MCTS @twitter http://twitter.com/ahmed_madany @Blog http://ahmedmadany.wordpress.com @LinkedIn http://eg.linkedin.com/pub/ahmed-madany/35/80/2b6

    Tuesday, March 27, 2018 9:27 PM
  • Hi,

    Add the code below into a script editor web part in the form page to achieve it.

    <script src="//code.jquery.com/jquery-1.12.4.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(function(){
    	$("#attachOKbutton").click(function(){
    		$("input[value='Save']").click();
    	});
    });
    </script>

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Proposed as answer by LinyusMVP Wednesday, March 28, 2018 7:54 AM
    Wednesday, March 28, 2018 6:53 AM
  • Thank you for your response. However, my button is an "asp" button, "input" is "html" button. I have tried both. It does not do any thing.

    $("input[value='Save']").click();

    Wednesday, March 28, 2018 2:57 PM
  • Thank you for your response. I had issue with your code.

    (1) Do I need to enable Ajax?

    (2) My form is extention is ascx (a custom control form with VS2015 them embedded to the page). I tried to change the url line with my page and function WorkOrder.ascx.cs/btSave_Click. It does not work.

    Please advise. Thank you.

      

    Wednesday, March 28, 2018 3:48 PM
  • Here is my script function which seems not do anything.

    This is the c# function I want to call. WorkOrder.ascx and WorkOrder.ascx.cs are embedded to EditForm_FW.aspx

    Wednesday, March 28, 2018 4:27 PM
  • Hi,

    If the asp:button code like this

    <asp:Button id="btSave" Text="Save" OnClick="btSave_Click" runat="server"/>

    we can use the jQuery code below to click button.

    $("input[id$='_btSave']").click();

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    • Marked as answer by eg10013 Thursday, March 29, 2018 1:36 PM
    Thursday, March 29, 2018 9:51 AM
  • Thank you. It works!
    Thursday, March 29, 2018 1:37 PM