<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.