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
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
To use PageMethods you need to follow these steps:
- You need to use ScriptManager and set EnablePageMethods.
- Create a static method in your code behind and use the
[WebMethod]
attribute.
- Call you method in javascript like you do in C# but you have more parameter do fill, such as th
e 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.
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
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.
How to call a web method form a usercontrol like MyUserControl.ascx.
Thanks
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.
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>
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
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,
Telerik