This is a migrated thread and some comments may be shown as answers.

Call pagemethod in Master page

12 Answers 1208 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Dhamodharan
Top achievements
Rank 1
Dhamodharan asked on 28 Aug 2013, 07:37 PM
Hi,
can we call page methods in the master page. how can we implement this calling page methods in master page.




Thanks

12 Answers, 1 is accepted

Sort by
0
David
Top achievements
Rank 1
Iron
Veteran
Iron
answered on 28 Aug 2013, 07:44 PM
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.
0
Dhamodharan
Top achievements
Rank 1
answered on 29 Aug 2013, 09:29 AM
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
0
David
Top achievements
Rank 1
Iron
Veteran
Iron
answered on 29 Aug 2013, 01:14 PM
My apologies.  I did not realize you were attempting to call this client side.  Sorry, I cannot assist.  My solution was server side. 
0
Princy
Top achievements
Rank 2
answered on 30 Aug 2013, 07:08 AM
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.

0
Dhamodharan
Top achievements
Rank 1
answered on 30 Aug 2013, 10:24 AM
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


0
Princy
Top achievements
Rank 2
answered on 02 Sep 2013, 05:00 AM
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.


0
Dhamodharan
Top achievements
Rank 1
answered on 04 Sep 2013, 05:10 PM
Hi Princy,
How to call a web method form a usercontrol like MyUserControl.ascx.







Thanks
0
Princy
Top achievements
Rank 2
answered on 05 Sep 2013, 10:53 AM
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.

0
Charl
Top achievements
Rank 1
answered on 09 Mar 2015, 11:26 AM
Its not working in my page...Is there any other way?
0
luc bonenfant
Top achievements
Rank 1
answered on 15 Sep 2015, 11:02 PM

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>

0
ashwani
Top achievements
Rank 1
answered on 29 Sep 2015, 05:50 PM

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

 

0
Marin Bratanov
Telerik team
answered on 30 Sep 2015, 05:17 AM

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
Tags
General Discussions
Asked by
Dhamodharan
Top achievements
Rank 1
Answers by
David
Top achievements
Rank 1
Iron
Veteran
Iron
Dhamodharan
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Charl
Top achievements
Rank 1
luc bonenfant
Top achievements
Rank 1
ashwani
Top achievements
Rank 1
Marin Bratanov
Telerik team
Share this question
or