繁体   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