Member Avatar for tuckerblue
tuckerblue

I have a few C# questions too, but this is mainly an ASP problem I think. All the code I mention below takes place in the code-behind of my Master Page. I worry a lack of knowledge on master pages may be the problem too. Excuse the database name; my dad wants to make a site called 'Dumbassery.com' which is what I'm working on :P...

I've been working on an ASP.NET site with C# code-behind. I wanted to add some cookies for login stuff but the cookies weren't sticking around. I decided, "Fix it later, try to work around it for now." I commented out the code, rebuilt, and the code continued to run. I know this because I have 2 panels that switch visibility on correctly logging in. I've closed Visual Studio, I've taken out the code completely and rebuilt, and the code continues to run!!

Master Page C# code-behind (with the commented out sections):

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //sets banner and background according to current site properties
                CurrentSiteProperty cs = (from c in new DumbasseryDatabaseDataContext().CurrentSiteProperties where c.id == 1 select c).First();
                BannerImage.ImageUrl = cs.Banner.fileLocation;
                bodyTag.Style.Add(HtmlTextWriterStyle.BackgroundImage, cs.BackgroundImage.fileLocation);
            }
            /*if (Request.Cookies["loggedIn"] != null && Request.Cookies["loggedIn"].Value == "true")
            {
                LoginPanel.Visible = false;
                LoggedInPanel.Visible = true;
                LoggedInHomeLink.Text = Request.Cookies["loggedIn"].Values.Get("userName") + " Home";// Request.Cookies["loggedIn"]["userName"] + " Home";
                Response.Cookies["loggedIn"].Expires = DateTime.Now.AddDays(1);
            }*/
        }

        protected void Login(Object o, EventArgs e)
        {
            /*User loginUser = (from u in new DumbasseryDatabaseDataContext().Users where u._password == PasswordTextBox.Text && u.loginName == LoginNameTextBox.Text select u).FirstOrDefault();
            if (loginUser != null)
            {
                LoginPanel.Visible = false;
                LoggedInPanel.Visible = true;
                LoggedInHomeLink.Text = loginUser.displayName + " Home";
                HttpCookie loggedIn = new HttpCookie("loggedIn");
                loggedIn.Values.Add("userName", loginUser.displayName);
                loggedIn.Value = "true";
                loggedIn.Expires = DateTime.Now.AddDays(1);
                Response.Cookies.Add(loggedIn);
            }*/
        }

        protected void Logout(Object o, EventArgs e)
        {
            /*Response.Cookies["loggedIn"].Expires = DateTime.Now;
            LoginPanel.Visible = true;
            LoggedInPanel.Visible = false;*/
        }

A couple other issues I've had that may or may not be tied to the problem that need fixing regardless:
I mention two controls in my Page_Load above (BannerImage and bodyTag) that give the error "The name 'controlName' does not exist in the current context." Even though they're showing as errors after building, the site still runs. It's not a problem as much as it is extremely confusing why this is happening (not being able to intellisense the controls doesn't help either).

The other issue is the cookies I mentioned before. The cookie code I was playing with is the C# code commented out above. Any help on why they aren't sticking around would be extremely helpful. I could tell something was wrong with my cookies because the controls they alter (the previously mentioned panels) appear at the top of the master page as follows:

Before Logging In:
Note that the LoginPanel is visible, LoggedInPanel is not, and the HyperLink has no text.

<asp:Panel runat="server" ID="LoginPanel">
    more controls
</asp:Panel>
<asp:Panel runat="server" ID="LoggedInPanel" Visible="false" >
    <asp:HyperLink ID="LoggedInHomeLink" runat="server" NavigateUrl="~/Register.aspx" />
    more controls
</asp:Panel>

After Logging in:
Note that the LoginPanel and LoggedInPanel successfullly change visibility and the Hyperlink text is now "username Home".

<asp:Panel runat="server" ID="LoginPanel" Visible="false">
    more controls
</asp:Panel>
<asp:Panel runat="server" ID="LoggedInPanel" Visible="true" >
    <asp:HyperLink ID="LoggedInHomeLink" runat="server" NavigateUrl="~/Register.aspx" Text="username Home" />
    more controls
</asp:Panel>

The very next page visited after the initial login redirection:
Note that the visibilities remained the same, but the Hyperlink text has lost the username.

<asp:Panel runat="server" ID="LoginPanel" Visible="false">
    more controls
</asp:Panel>
<asp:Panel runat="server" ID="LoggedInPanel" Visible="true" >
    <asp:HyperLink ID="LoggedInHomeLink" runat="server" NavigateUrl="~/Register.aspx" Text=" Home" />
    more controls
</asp:Panel>

The 2nd page visited after the initial login redirection:
The page returns to how it was before login.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.