简体   繁体   中英

Get Server side values to Client side

Here i'm going to get Server side values to client side.

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;
}

The aspx markup is invalid. You should not have a semicolon inside the ERB tag.

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

Additionally consider checking for null .

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

    return user.UserName;
}

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