Thursday, November 13, 2008 7:51 PM Yonggang Meng

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 Yonggang Meng

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!

# re: How to invoke PageMethods in MasterPage

Monday, December 28, 2009 8:57 AM by najmulansari

Thanks man,

I was stuck from last 2 days and was not able to figure out the exact problem.... but your solution was perfect.... it worked for me....

Thanks

# re: How to invoke PageMethods in MasterPage

Thursday, May 27, 2010 2:09 AM by Pragatheeswaran

Cool.. It solved my puzzle :)

# re: How to invoke PageMethods in MasterPage

Wednesday, September 22, 2010 6:05 AM by Pradip

hey YongGang,

I am too lazy to drop appreciation replies, but I must appreciate and salute your knowledge !

Thanks !

# re: How to invoke PageMethods in MasterPage

Friday, October 01, 2010 7:03 AM by dogan

HI - Try to add [System.Web.Script.Services.ScriptMethod] under the [WebMethod] before moving to a basepage.

# re: How to invoke PageMethods in MasterPage

Tuesday, December 14, 2010 12:41 PM by Jon

Smart and easy solution. Thank you.

# re: How to invoke PageMethods in MasterPage

Friday, February 18, 2011 6:07 PM by cocogorilla

This is fantastic.  Structurally sound too... not really a workaround... just the right solution and elegant.

WOW

# re: How to invoke PageMethods in MasterPage

Saturday, May 21, 2011 9:46 PM by Betsey

Walking in the presence of gaitns here. Cool thinking all around!

# re: How to invoke PageMethods in MasterPage

Sunday, May 22, 2011 9:33 AM by qzwrxdkda

RMGcu3  <a href="uzpduqxrjnpx.com/.../a>

# re: How to invoke PageMethods in MasterPage

Friday, May 27, 2011 11:41 AM by yltvfjhvyl

hu5EtL  <a href="ntywdpfwnqct.com/.../a>

# re: How to invoke PageMethods in MasterPage

Monday, May 30, 2011 11:26 PM by cheap designer handbags

This articles helps me more.Thanks for your sharing,I will pay more attentions to your blog. Looking forward to your better and better articles.See you next time.

# re: How to invoke PageMethods in MasterPage

Saturday, June 04, 2011 4:25 AM by muthyala

How can i add Page base class because class cannot inherit page class can u pls help me out as soon as possible

wil ge thankful for ur help

# re: How to invoke PageMethods in MasterPage

Saturday, June 04, 2011 5:10 AM by Muthyala

Hey Thanks Alot Dude

It really worked & saved me

from past 2days am struggling

THank Alot

THank u

# re: How to invoke PageMethods in MasterPage

Monday, January 16, 2012 5:07 AM by master_still

Thanks a lot mike...it really helped

Leave a Comment

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