Search code examples
c#asp.netajaxmodalpopupextenderpagemethods

PageMethods or Alternative in Master Page?


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 ?


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 :)