Question

I having a problem that seems i can'f find a solution to which is the following:

I have a master page that have a menu, each link in the menu is a LinkButton.

I am required that whenever a user click on a certain link to show a login popup so i used Ajax ModalPopupExtender and i successfully showed the popup.

Now i want to validate the user which means that i will need to input username and password and press login but since i am in a popup it will close because of postback so i suppressed the postback and now i have to do the checking from the Client side so i need to call a server method from a javascript function, i tried using the PageMethods but i keep getting PageMethdos not defined, then i read that it won't work inside master pages or user controls.

Any solution to my problem ?

Was it helpful?

Solution

PageMethods are to be used in aspx pages , not in MasterPage Methods. The only workaround is to create a seperate .asmx WebService and add your Logic in a static function . To Do so , right click on your solution on VS and click on Add New Item, Select WebService.asmx. in the code behind of the WebService write a static webMethod

[WebMethod]// press alt+shift+f10 after selecting the WebMethod wording to include the library
public static bool CheckLogin (string username, string password){
//Type your method here
return result;//Result should be Boolean
}

Now on your masterPage.master on client side script click event of the link, Post an Ajax Request to the webservice

 $.ajax({
        type: "POST",
        url: "YourWebServiceName.asmx/CheckLogin",
        data: '{"Username":"' + $('#username').val() + '","password":"' +
                $('#password').val() + '"}',
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function(message) {
          alert(message);//will alert 'true'
           //DO what you want to do on client side
        },
        error: function() {
          alert(message);//will alert 'false'
 //DO what you want to do on client side
        }
    });

Please let me know if you need any further clarification Have a nice day :)

OTHER TIPS

One solution is to create a base class for all your pages and put the page method there. For example:

public class CustomBasePage : System.Web.UI.Page
{
    [System.Web.Services.WebMethod]
    public static bool ValidateUser(...)
    {
        bool isValid = false;

        ...

        return isValid;
    }
}

Now all you content page should deliver from CustomBasePage instead of Page:

//public partial class Index : System.Web.UI.Page
public partial class Index : CustomBasePage
{
    ...
}

This way you write the method only once and it is accessible always, so you can depend on it in your master page.

Did you try adding the <base target="_self"> tag in the head of the page. It will make sure that the postback happens in the original page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top