I’m developing a web application using .net/c#. I have a user control that I want to call it’s page methhod from client side by using javascript. However, I found the page method could be called if I put it in parent page, but can not if I put the method in user control code file. But for some reasons, I have no control on parent page, but have fullly control on user control. How to overcome this limitation?
I call pagemethod using javascript like this:
jQuery("[id$='tb_UserName']").blur(function() {
var login = jQuery("[id$='tb_UserName']").val();
PageMethods.SearchUserName(login, SearchUserNameCallback);
});
function SearchUserNameCallback(result) {
alert(result);
}
The Page method SearchUserName is defined as following:
[WebMethod]
public static string SearchUserName(string name)
{
...
}
The method has no problem, just it’s location make a big difference. If I put it in user control code file, error comes out as “Pagemethods is undefined.” If I put it in parent code file, it works fine. But I have no control on parent file, I can only do it in my test environment.
It’s called a Page Method because they only exist at the page level. If you need something that can exist in different scopes, you should use a ASMX or WCF webservice and include them as a ServiceReference in your ScriptManager.