YongGang Meng's Weblog


Welcome to Weblog

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.
Posted: Nov 13 2008, 07:51 PM by Ralax | with 6 comment(s)
Filed under: , , , ,

Comments

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

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

# November 13, 2008 7:53 AM

Martin said:

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!

# March 9, 2009 5:59 AM

Ralax said:

yes, it's a lame post!

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

# March 30, 2009 9:17 PM

John Boker said:

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

# May 28, 2009 11:38 AM

ethiopia said:

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

# September 16, 2009 11:09 PM

Gustavo said:

awesome!! you really save me!

# September 23, 2009 1:31 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)