C# 在aspx页面上调用静态方法不起作用?

C# 在aspx页面上调用静态方法不起作用?,c#,asp.net,C#,Asp.net,我有一个名为MasterPage\u MyMasterPage的母版页,我在上面创建了一个静态方法 public static string GetRoleName() { string sRoleName="admin"; if (HttpContext.Current.Session["UserName"] != null) { sRoleName = HttpContext.Current.Session[

我有一个名为MasterPage\u 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
但它不起作用,它打印成

Brayan <span><%= MasterPage_MyMasterPage.GetRoleName() ;></span>       
Brayan
会话[“用户名”]在登录成功时绑定。请帮助我。

使用
语法可用于呈现到页面

但它们不能用于设置服务器控件属性,而这正是您尝试执行的操作(Label.Text属性)

考虑改用:

。。。Text=“”
试试这个

<%= ((MasterPage_MyMasterPage)Page.Master).GetRoleName() %>
在aspx文件中

<%= getRoleName() %>

嘿,请试试这个对我有用

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

它会和这个一样吗


发生错误:错误114:无法使用实例引用访问成员“MasterPage\u MyMasterPage.GetRoleName()”;改为使用类型名称限定它
<%= getRoleName() %>
<a >Brayan <asp:Label runat="server" Text='' ><%= MasterPage_MyMasterPage.GetRoleName()%>     </asp:Label>