简体   繁体   中英

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

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

<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.

嘿,请尝试对我有用

<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).

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

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

and in aspx file

<%= getRoleName() %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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