Thursday, November 13, 2008 7:51 PM Ralax

How to invoke PageMethods in MasterPage

I have a web project, in the project, there is a MasterPage and some WebContentPages.
In the MasterPage, there is a function, when you click a button, show or hide the left content.When you refresh or navigate other WebContentPage, the left content keeps its display style.
Here is my first solution:
MasterPage.Master:

<div id="left" style='<%=string.Format("display:{0}",GetToolbarDisplayFlag())%>'>

</div>

<div id="buttons_left">
     <img alt="Move Left" onclick="switchView('left',this);" src='<%=GetImgSrc()%>' />

</div>

Remember to set the EnablePageMethods = "True"


MasterPage.Master.CS:

public string GetToolbarDisplayFlag()
{

       return HttpContext.Current.Session["ToolbarDisplay"] == null ? "block" : HttpContext.Current.Session["ToolbarDisplay"].ToString();

}
public string GetImgSrc()
{
   if(GetToolbarDisplayFlag() == string.Empty || GetToolbarDisplayFlag() == "block")
   {
       return "../App_Themes/images/s_left.gif";
   }
   return "../App_Themes/images/s_right.gif";

}

[WebMethod]public static void SetToolbarDisplayFlag(string flag)
{
    HttpContext.Current.Session["ToolbarDisplay"= flag;

}


JS:


function switchView(objname,current)
{
    var obj = $get(objname);
    if(obj.style.display=="block" || obj.style.display=="")
    {
        obj.style.display="none";
        current.src ='../App_Themes/images/s_right.gif';
        PageMethods.SetToolbarDisplayFlag("none",OnSucceeded,OnFailed);
    }
    else
    {
        obj.style.display="block";
        current.src ='../App_Themes/images/s_left.gif';
        PageMethods.SetToolbarDisplayFlag("block",OnSucceeded,OnFailed);
    }
}
function OnSucceeded(result, userContext, methodName)
{   
    return;
} 
function OnFailed(error, userContext, methodName)
{
    return;
}           

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();


When you click the button, it will cause exception: PageMethods not defined.

This is because the method "SetToolbarDisplayFlag" Js invoke is in MasterPage, it seems PageMethods donot contain MasterPage methods.

 

So, my solution is move the method "SetToolbarDisplayFlag" from MasterPage to a base page called "PageBase" which inherited from System.Web.UI.Page, and each WebContentPage derived from PageBase.

PageBase.cs:


public class PageBase : Page

{

   [WebMethod]
   public static void SetToolbarDisplayFlag(string flag)
   {
       HttpContext.Current.Session["ToolbarDisplay"] = flag;

   }

}


Now, you can invoke methods in MasterPage.
Filed under: , , , ,

Comments

# How to invoke PageMethods in MasterPage - YongGang Meng's Weblog

Pingback from  How to invoke PageMethods in MasterPage - YongGang Meng's Weblog

# re: How to invoke PageMethods in MasterPage

Monday, March 09, 2009 5:59 AM by Martin

Lol.. that was really intelligent of you. as if no one knew that it must be placed on the base page. I think people langing on this page would find a solution on how to place Methods on the MasterPage not on the BasePage..

Lame post!

# re: How to invoke PageMethods in MasterPage

Monday, March 30, 2009 9:17 PM by Ralax

yes, it's a lame post!

You must have a good solution, would you please comment here?

# re: How to invoke PageMethods in MasterPage

Thursday, May 28, 2009 11:38 AM by John Boker

This is a good post that explains things very well.  Thank you.

# re: How to invoke PageMethods in MasterPage

Wednesday, September 16, 2009 11:09 PM by ethiopia

thanks for the info..im nood so this one was very helpful! dont mind martin..his lamer than his words..lols! ^_^;

# re: How to invoke PageMethods in MasterPage

Wednesday, September 23, 2009 1:31 PM by Gustavo

awesome!! you really save me!

Leave a Comment

(required) 
(required) 
(optional)
(required)