简体   繁体   English

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

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

I have a master page named MasterPage_MyMasterPage on whic i have created a static method as 我有一个名为MasterPage_MyMasterPage的母版页,我已经创建了一个静态方法

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


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

and on aspx page i called it as 在aspx页面上我称之为

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

but it doesn't worked, it print as ... 但它不起作用,它打印为...

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

Session["UserName"] is bind when login successfull.Please help me. 成功登录后,Session [“ UserName”]已绑定。请帮助我。

嘿,请尝试对我有用

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

Inline expressions using the <%= ... %> syntax can be used to render to the page. 使用<%= ... %>语法的内联表达式可用于呈现到页面。

But they can't be used to set a server-control property, which is what you're attempting to do (Label.Text property). 但是它们不能用于设置服务器控件属性,而这正是您要尝试执行的操作(Label.Text属性)。

Consider using data-binding syntax instead: 考虑改用数据绑定语法

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

Try this 尝试这个

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

Update: 更新:

Make a protected method in your cs file something like this 在您的cs文件中创建一个受保护的方法,如下所示

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

and in aspx file 并在aspx文件中

<%= getRoleName() %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM