2017-05-26 86 views
0

我一直在努力從web方法(或任何靜態方法)訪問主頁面中的控件。從WebMethod訪問主頁面控件

我有一個引導樣式的下拉母版頁列出用戶的收藏夾。此下拉列表中的項目使用中繼器填​​充,該中繼器從DB讀取當前用戶的收藏夾列表並填充下拉列表。

在使用此母版頁的主頁上,有許多報告帶有「添加到收藏夾」按鈕。爲了做到這一點客戶端,我添加了一個onclick事件按鈕,它調用一個JavaScript函數,調用一個Web方法來存儲這個信息(用戶ID,URL)在數據庫中。這部分沒有問題。問題是,那麼我需要更新下拉列表(重新綁定中繼器)。

以下是我有什麼,我曾嘗試:

在母版頁:

Default.aspx中
<%@ 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> 

<%@ 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> 
在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 
    } 
} 

任何想法可以實現這一點非常感謝。當然,我可以將「添加到收藏夾」更改爲ASP LinkBut​​ton並且沒有問題,但只需要知道這種方法可以如何完成;如果可以的話。

回答

0

我不認爲你需要這段代碼來訪問你的中繼器。

Page page = (Page)HttpContext.Current.Handler; 
MasterPage mp = page.Master; <--- Always null 

你只需要告訴它在MasterPage中尋找它。 試試這種方式。

(Repeater) rpt = (Repeater) Master.FindControl("rptFavorites"); 
+0

不起作用。那是我第一次嘗試。無法從靜態方法訪問控件。如果您正在調用在用戶類中定義的方法,則可以從Web方法創建該類的實例,然後調用該函數;但我不使用班級。 – NoBullMan

+0

對此做了更多的研究,我發現你不能。正如這個討論一樣。 https://stackoverflow.com/questions/31514188/how-to-access-page-controls-inside-a-static-web-method我只是希望我可以更有幫助。 – jmag

+0

好的,謝謝你的幫助。 – NoBullMan

相關問題