获取服务器端值到客户端

时间:2014-04-09 15:15:21

标签: c# asp.net

这里我将把服务器端值发送到客户端。

ASPX

<a href="#" class="alert-link">Welcome <%=GetUserName();%></a> <-- problem in here

aspx.cs

public string GetUserName()
{
    string name;
    MembershipUser usr = Membership.GetUser();
    name = usr.UserName; //<-- Correct names come to here
    return name;
}

1 个答案:

答案 0 :(得分:2)

aspx标记无效。你不应该在ERB标签内加一个分号。

<a href="#" class="alert-link">
    Welcome <%= GetUserName() %>
</a>

另外考虑检查null

public string GetUserName()
{
    var user = Membership.GetUser();
    if(user == null)
    {
        return "Anonymous";
    }

    return user.UserName;
}