locked
Custom Web Control: call server-side method from javascript RRS feed

  • Question

  • User-1779948929 posted

    Hi,

    I need to call server-side method from javascript in asp.net custom web control.

    I know how to call server-side method from aspx page using Ajax Extension, but i don't know how to do it in custom control.

    Below is the code i tried in Normal web application..

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <script type="text/javascript" language="javascript">
            function Test() {
                // call server side method
                PageMethods.TestServerSideMethod("test 1", OnGetMessageSuccess, OnGetMessageFailure);
            }
    
            function OnGetMessageSuccess(result, userContext, methodName) {
                alert(result);
            }
    
            function OnGetMessageFailure(error, userContext, methodName) {
                alert(error.get_message());
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
        </asp:ScriptManager>
        <input type='submit' value='Get Message' onclick='Test();return false;' />
        </form>
    </body>
    </html>
    Thursday, April 19, 2012 2:33 AM

Answers

  • User-16411453 posted

    You have to just register the client script using the client script manger, so the control will print out the script tag in the appropiate tag location (head), (form). You do this in OnInit.

    When you register the script tag, the server will output the tag in the html stream sent back to the browser, so the browser will make a reference to the script.

    lookup client script manager

    Ajax has nothing to do with registering client script tags!, it's a toltally different technology, but similar in functionality.

    Perhaps you should sharpen your skills on code behind pages first, before diving into server or user controls.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 22, 2012 2:26 AM

All replies

  • User877341716 posted

    Hi,

    Pls refer below link

    http://forums.asp.net/p/1041069/1449674.aspx

    Thursday, April 19, 2012 2:48 AM
  • User3866881 posted

    but i don't know how to do it in custom control.

    As far as I see,I think you can create an ascx control,where you can do what you usually do to refer the javascript as the codes you've offered us above。And then you can convert the ascx control to a normal Customized control by referring this torturial:

    http://msdn.microsoft.com/en-us/library/aa479318.aspx

    Friday, April 20, 2012 8:42 PM
  • User-16411453 posted

    You have to just register the client script using the client script manger, so the control will print out the script tag in the appropiate tag location (head), (form). You do this in OnInit.

    When you register the script tag, the server will output the tag in the html stream sent back to the browser, so the browser will make a reference to the script.

    lookup client script manager

    Ajax has nothing to do with registering client script tags!, it's a toltally different technology, but similar in functionality.

    Perhaps you should sharpen your skills on code behind pages first, before diving into server or user controls.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 22, 2012 2:26 AM