簡體   English   中英

在aspx頁面上調用靜態方法不起作用?

[英]Calling Static method on aspx page does not work?

我有一個名為MasterPage_MyMasterPage的母版頁,我已經創建了一個靜態方法

public static string GetRoleName()
    {
        string sRoleName="admin";


        if (HttpContext.Current.Session["UserName"] != null)
        {
            sRoleName = HttpContext.Current.Session["UserName"].ToString();
        }
        return sRoleName;
    }

在aspx頁面上我稱之為

<a >Brayan <asp:Label runat="server" Text='<%= MasterPage_MyMasterPage.GetRoleName() %>' ></asp:Label>

但它不起作用,它打印為...

Brayan <span><%= MasterPage_MyMasterPage.GetRoleName() ;></span>       

成功登錄后,Session [“ UserName”]已綁定。請幫助我。

嘿,請嘗試對我有用

<a >Brayan <asp:Label runat="server" Text='' ><%= MasterPage_MyMasterPage.GetRoleName()%>     </asp:Label>

使用<%= ... %>語法的內聯表達式可用於呈現到頁面。

但是它們不能用於設置服務器控件屬性,而這正是您要嘗試執行的操作(Label.Text屬性)。

考慮改用數據綁定語法

... Text = "<%# MasterPage_MyMasterPage.GetRoleName() %>"

嘗試這個

<%= ((MasterPage_MyMasterPage)Page.Master).GetRoleName() %>

更新:

在您的cs文件中創建一個受保護的方法,如下所示

protected string getRoleName()
{
    return ((MasterPage_MyMasterPage)Page.Master).GetRoleName();
}

並在aspx文件中

<%= getRoleName() %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM