I have been struggling with accessing a control in master page from a web method (or any static method for that matter).
I have a bootstrap-styled drop down in master page that list user's favorites. Items in this drop down are populated using a repeater that reads list of favorites for current user from DB and populates the drop down.
On the main page that uses this master page there are a number of reports with an "Add to favorites" button. To do this client side, I added an onclick event to button, it calls a javascript function that calls a web method to store this info (User ID, URL) in database. This part is fine. The problem is that then I need to update the dropdown (rebind the repeater).
Here is what I have and what I have tried:
In master page:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.Master.cs" Inherits="EA.SiteMaster" %>
....
<asp:ScriptManager runat="server" ID="ScriptManager1" EnablePageMethods="true" />
....
<ul class="dropdown-menu" role="menu">
<asp:Repeater runat="server" ID="rptFavorites" OnItemDataBound="rptFavorites_ItemDataBound">
<ItemTemplate>
<li>
<asp:LinkButton runat="server" ID="lbFavLink" /><span class="glyphicon glyphicon-heart"></span>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
in Default.aspx:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="EA._Default" %>
<%@ MasterType virtualpath="Site.master" %>
....
function addFavorite(sURL, sFriendlyName) {
// I added this to get the routing working properly
var path = PageMethods.get_path() + '.aspx';
PageMethods.set_path(path);
PageMethods.AddUserFavorite(sURL, sFriendlyName, onSucceeded, onFailed);
}
function onSucceeded(result, userContext, methodName) {
alert(result);
}
function onFailed(result, userContext, methodName) {
alert(result);
}
....
<button type="button" class="btn btn-primary btn-xs" id="btnMPP" onclick="addFavorite('http://blahblah.com/IV/SomeReport.html','Some Report')" ><i class="fa fa-heart-o" aria-hidden="true"></i></button>
in Default.aspx.cs:
[System.Web.Services.WebMethod]
public static string AddUserFavorite(string sURL, string sFriendlyName)
{
string sMsg = string.Empty;
string sUserID = HttpContext.Current.Session["UserID"].ToString();
Favorites oFavorite = new Favorites();
oFavorite.FavoritesURL = sURL;
oFavorite.FavoritesFriendlyName = sFriendlyName;
oFavorite.UserID = sUserID;
int iRet = Favorites.AddFavrites(oFavorite);
if (-1 == iRet)
sMsg = "Failed to add to favorites list";
else
{
sMsg = "\"" + sFriendlyName + "\" (" + sURL + ") was added to your favorites list";
UpdateMaster();
}
return sMsg;
}
private static void UpdateMaster()
{
Page page = (Page)HttpContext.Current.Handler;
MasterPage mp = page.Master; <--- Always null
Repeater rptFavorites = mp.FindControl("rptFavorites") as Repeater;
if (rptFavorites != null)
{
// rebind repeater here
}
}
Any ideas on how I can achieve this is greatly appreciated. Of course, I can change the "Add to Favorites" to an ASP LinkButton and no issues, but just need to know how this approach can be done; if it can.