Call pagemethod in Master page

13 posts, 0 answers
  1. Dhamodharan
    Dhamodharan avatar
    300 posts
    Member since:
    Sep 2010

    Posted 28 Aug 2013 Link to this post

    Hi,
    can we call page methods in the master page. how can we implement this calling page methods in master page.




    Thanks
  2. David
    David avatar
    90 posts
    Member since:
    Jan 2011

    Posted 28 Aug 2013 Link to this post

    this.Page.Master() will return a reference to the masterpage which hosts this page pointed at by "this".  you could then cast this as necessary and call the exposed values.  Be sure to check for nulls.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Dhamodharan
    Dhamodharan avatar
    300 posts
    Member since:
    Sep 2010

    Posted 29 Aug 2013 Link to this post

    Hi,
    when i call pagemethod from script it returns as "JavaScript runtime error: 'PageMethods' is undefined"
    i called as follows

        PageMethods.ChangeUser(updateUserID(result) {
                                alert(result);
                            } );


    thanks
  5. David
    David avatar
    90 posts
    Member since:
    Jan 2011

    Posted 29 Aug 2013 Link to this post

    My apologies.  I did not realize you were attempting to call this client side.  Sorry, I cannot assist.  My solution was server side. 
  6. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 30 Aug 2013 Link to this post

    Hi Dhamodharan,

    To use PageMethods you need to follow these steps:

    1. You need to use ScriptManager and set EnablePageMethods.
    2. Create a static method in your code behind and use the [WebMethod] attribute.
    3. Call you method in javascript like you do in C# but you have more parameter do fill, such as the Success and Error callbacks.

    Please have a look at the following code I tried which works fine at my end.

    ASPX:

    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
    <div>
        <telerik:RadButton ID="RadButton1" runat="server" Text="Call Method" OnClientClicked="OnClientClicked">
        </telerik:RadButton>
    </div>

    JavaScript:
    <script type="text/javascript">
        function OnClientClicked(sender, args) {
            PageMethods.GetSampleData("Value1", OnSuccess, OnFailed);
        }
        function OnSuccess(response) {
            alert("Success");
        }
        function OnFailed(error) {
            alert("Error");
        }
    </script>

    C#:
    [WebMethod]
    public static void GetSampleData(string value1)
    {
        //Your Code
    }

    Thanks,
    Princy.

  7. Dhamodharan
    Dhamodharan avatar
    300 posts
    Member since:
    Sep 2010

    Posted 30 Aug 2013 Link to this post

    Hi Princy,
    I followed ur steps but still i get the same error as "JavaScript runtime error: 'PageMethods' is undefined".

    I get this when I am using these codes in Master page. for normal aspx page it works perfect.






    Thanks


  8. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 02 Sep 2013 Link to this post

    Hi Dhamodharan,

    Your webmethod code cannot reside in the code behind of your master page. You can include an actual web service or WCF service in your project for methods that need to be called from multiple pages.

    You can define the webmethod code in the code behind of your content page and invoke them from Master page as follows.

    ASPX (Master Page):

    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
        <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
            <script type="text/javascript">
                function CheckAvailability() {
                    PageMethods.CheckAvailability('test', OnComplete);
                    return false;
                }
     
                function OnComplete(result) {
                    alert(result);
                }
     
                CheckAvailability();
            </script>
        </telerik:RadCodeBlock>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    </form>

    C#: (Content-Page)
    [WebMethod]
    [ScriptMethod]
    public static bool CheckAvailability(string uid)
    {
        return true;
    }

    Thanks,
    Princy.


  9. Dhamodharan
    Dhamodharan avatar
    300 posts
    Member since:
    Sep 2010

    Posted 04 Sep 2013 Link to this post

    Hi Princy,
    How to call a web method form a usercontrol like MyUserControl.ascx.







    Thanks
  10. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 05 Sep 2013 Link to this post

    Hi Dhamodharan,

    If you have a [ScriptMethod] defined in your user control, you cannot call that from your .aspx page using PageMethods.functionname(). Only ScriptMethods defined in the same .aspx page can be called. A simple workaround would be to have another page method inside the page which in-turn would call the ScriptMethod on the UserControl. Remember all the methods should be public static to be used as a PageMethod. Please have a look at the following sample code.

    C#: (UserControl.cs)

    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public static string MyUserControlPageMethod()
    {
       return "Hello from MyUserControlPageMethod";
    }

    JavaScript (in aspx):
    <script type="text/javascript" language="javascript">
        function callbackFunction(result) {
            $get("WebUserControl1_Label1").innerText = result;
        }
        function CallUserControlPageMethod() {
            PageMethods.ForwardingToUserControlPageMethod(callbackFunction);
        }
    </script>

    C#: (aspx.cs)
    [WebMethod]
    [ScriptMethod]
    public static string ForwardingToUserControlMethod(string ddlValue)
    {
       return WebUserControl.MyUserControlMethod(ddlValue);
    }

    Hope this helps,
    Princy.

  11. Charl
    Charl avatar
    1 posts
    Member since:
    Nov 2014

    Posted 09 Mar 2015 in reply to Princy Link to this post

    Its not working in my page...Is there any other way?
  12. luc bonenfant
    luc bonenfant avatar
    53 posts
    Member since:
    Sep 2007

    Posted 15 Sep 2015 Link to this post

    I think it's too late for you but... i was in the same case, and i observed that my url was not rewrited.

    So, if your problem comes from url rewrit​ing like me, you need to place the real path before calling PageMethods :

    <script type="text/javascript">
       PageMethods.set_path("/mypages/page.aspx");
    </script>

  13. ashwani
    ashwani avatar
    1 posts
    Member since:
    Aug 2015

    Posted 29 Sep 2015 in reply to Princy Link to this post

    Hi,

    Can I use telerik asp.net control (datagrid) in static web method to bind it to data source? If yes then please let me know snippet or if not then what should be solution for that.

    Your timely response would be highly appreciated.

     Sincerely,

    Ashwani

     

  14. Marin Bratanov
    Admin
    Marin Bratanov avatar
    3602 posts

    Posted 30 Sep 2015 Link to this post

    Hi Ashwani,

    The following article explains how you can bind a grid on the client-side: http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-binding/understanding-data-binding/client-side-binding/client-side-binding.

    You can use the OnSuccess handler for the page method call to data bind the grid by using its set_dataSource() method.

    I advise that you review the entire section in the documentation to see the details and specifics on client-side binding.

    Another similar example is available here: http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-binding/understanding-data-binding/webservice-binding/client-binding. You van also see this in action in the following demo: http://demos.telerik.com/aspnet-ajax/grid/examples/data-binding/client-side/programmatic/defaultcs.aspx.

    Regards,

    Marin Bratanov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017