-2

Aspx

<a href="signUp.aspx" class="nav-link w-nav-link"><%=Account%></a>

aspx opening

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="contact.aspx.cs" Inherits="RapidTyper.contact" %>

code behind

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace RapidTyper
    {
        public partial class contact : System.Web.UI.Page
        {
            public string Account = "Sign In";
            protected void Page_Load(object sender, EventArgs e)
            {
                if (Session["user"] != null)
                {
                    Account = "Account";
                }
            }
        }
    }

The problem: the Account in the aspx is not recognized and returns the error : "The name 'Account' does not exist in the current context"

1 Answers1

0

This error goes away when you compile and run your project. But in case you want not to have it without compiling then the fix is to change your CodeBehind attribute to CodeFile attribute (not recommended) in the Page directive in your ASPX page (the first line in aspx). Here is an SO thread explaining the difference between these attributes.

enter image description here

Update
Since, referencing the properties from CodeBehind in aspx page looks easier with <%%> syntax but it has its limitations, such as, you cannot retrieve the values until the page is fully posted back to the server, if you use it inside an UpdatePanel it might throw some exceptions.

It is always better to set such values from CodeBehind like, if you make your anchor element run at server and then set it's InnerText property On Page_Load event, then it will be an efficient approach. i.e:

<a href="signUp.aspx" class="nav-link w-nav-link" runat="server" id="aSignup"></a>

Now in your code behind on Page_Load event do:

public string Account = "Sign In";
protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostback)
    {
        aSignup.InnerText = Account;
    }

}
Jamshaid K.
  • 3,555
  • 1
  • 27
  • 42