Skip to content
Fix Code Error

Error Stack Overflow when trying to hide buttons in navbar after user login

June 27, 2021 by Code Error
Posted By: Anonymous

What Im trying to do is to hide the login button in masterpage header when the user logged in.
Below are the codes.

SiteMaster.Master.aspx

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="SiteMaster.Master.cs" Inherits="UniSystem.SiteMaster" %>

<!DOCTYPE html>

<html lang="en">
<head runat="server">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
    <title><%: Page.Title %> - UniSystem Database - </title>

    <asp:PlaceHolder runat="server">
        <%: Scripts.Render("~/bundles/modernizr") %>
    </asp:PlaceHolder>


</head>
<body>

    <form runat="server">
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-gtEjrD/SeCtmISkJkNUaaKMoLD0//ElJ19smozuHV6z3Iehds+3Ulb9Bn9Plx0x4" crossorigin="anonymous"></script>
        <asp:ScriptManager runat="server">
            <Scripts>
                <%--To learn more about bundling scripts in ScriptManager see https://go.microsoft.com/fwlink/?LinkID=301884 --%>
                <%--Framework Scripts--%>
                <asp:ScriptReference Name="MsAjaxBundle" />
                <asp:ScriptReference Name="jquery" />
                <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
                <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
                <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
                <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
                <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
                <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
                <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
                <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
                <asp:ScriptReference Name="WebFormsBundle" />
                <%--Site Scripts--%>
            </Scripts>
        </asp:ScriptManager>

        <nav class="navbar navbar-expand-lg navbar-dark bg-dark navbar-fixed-top">
            <div class="container-fluid">
                    <a class="navbar-brand ps-5" runat="server" href="~/">Home</a>
                    <button type="button" class="navbar-toggler" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                        <span class="navbar-toggler-icon"></span>>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                <div class="collapse navbar-collapse" id="navbarSupportedContent">
                    <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                        <li class="nav-item"><a runat="server" class="nav-link fs-5 gap-2" href="~/AdminModule/RegisterLecturer/Lecturer.aspx">Register Lecturer</a></li>
                        <li class="nav-item"><a runat="server" class="nav-link fs-5 gap-2" href="~/LecturerModule/LecturerWorkload/WorkloadLec.aspx">Lecturer Page</a></li>
                        <li class="nav-item"><a runat="server" class="nav-link fs-5 gap-2" href="~/Login/LoginPage.aspx">Login</a></li>
                        <li class="nav-item"><a runat="server" class="nav-link fs-5 gap-2" href="~/Contact">Contact</a></li>
                    </ul>
                    <div class="d-grid gap-2 d-md-flex justify-content-md-end pe-5">

                    <asp:LinkButton ID="btnlogin" class="btn btn-outline-danger me-md-2 fs-5"  runat="server" PostBackUrl="~/Login/LoginPage.aspx">Login</asp:LinkButton>
                    </div>    
                    
                </div> 
            </div>
        </nav>

        <div class="container body-content pt-3">
            <asp:ContentPlaceHolder ID="MainContent" runat="server">
            </asp:ContentPlaceHolder>
            <hr />
            <footer>
                <p>&copy; <%: DateTime.Now.Year %> - Designed by MAMK-CI190019</p>
            </footer>
        </div>

    </form>
</body>
</html>

SiteMaster.Master.aspx.cs

namespace UniSystem
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public LinkButton btnlogin
        {
            get
            {
                return this.btnlogin;
            }

        }    

LoginPage.aspx

<%@ Page Title="Login" Language="C#" MasterPageFile="~/SiteMaster.Master" AutoEventWireup="true" CodeBehind="LoginPage.aspx.cs" Inherits="UniSystem.LoginPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

    <!DOCTYPE html>
<link rel="stylesheet" href="/Login/css/Login.css"/>    
<div style="height: 150px; width: 980px;">
   
    <h2 position: absolute; left: 100px; top: 150px;>
        Login Page  
    </h2>
   
        <asp:Label ID="lblLoginID" Runat="server" Text='ID' style="font-size: medium"></asp:Label>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="txtLoginID" runat="server" Width="240px"></asp:TextBox>

        <br />
        <br />

        <asp:Label ID="lblLoginPass" Runat="server" Text='Password' style="font-size: medium"></asp:Label>
        &nbsp;&nbsp;
        <asp:TextBox ID="txtLoginPass" runat="server" Width="240px" ></asp:TextBox>
    </div>       

<div class ="radiopad">
    <asp:RadioButtonList ID="rblDBSelect" runat="server">
        <asp:ListItem Text="Student" Value="Student" />
        <asp:ListItem Text="Lecturer" Value="Lecturer" />
        <asp:ListItem Text="Admin" Value="Admin" />
    </asp:RadioButtonList>
</div>

<div class ="radiopad">
    <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />

    <br />

    <asp:Label ID="lblAttention" runat="server" CssClass="Note"></asp:Label>
</div>
    </asp:Content>

LoginPage.aspx.cs

namespace UniSystem
{
    public partial class LoginPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            LoginPageC std = new LoginPageC();
            string txtloginid = txtLoginID.Text;
            string txtloginpass = txtLoginPass.Text;
            string selectTbl = rblDBSelect.SelectedItem.Value.ToString();

            {
                if (txtloginid.Trim() == "" && txtloginpass.Trim() == "")
                {
                    lblAttention.Text = "Please Insert Data Completely!";
                }
                else
                {
                    if (std.LoginValidation(txtloginid, txtloginpass, selectTbl) == false)
                    {
                        lblAttention.Text = " *Wrong Credentials";
                    }
                    else
                    {
                        

                        string selTbl = std.GetSelectedTbl();
                        if (selTbl == "Lecturer")
                        {
                            Session["loginid"] = std.GetLoginID();
                            Response.Redirect("/LecturerModule/LecturerWorkload/WorkloadLec.aspx");
                        }
                        else if (selTbl == "Admin")
                        {
                            Session["loginid"] = std.GetLoginID();
                            Response.Redirect("/AdminModule/RegisterLecturer/Lecturer.aspx");
                        }
                        else if (selTbl == "Student")
                        {
                            Response.Redirect("Student.aspx");
                        }
                        else
                        {
                            lblAttention.Text = " *Please Select A Role";
                        }
                    }
                }       
            }
        }
    }
}

WorkloadLec.aspx / aka lecturer dashboard

<%@ Page Title="WorkloadLec" Language="C#" MasterPageFile="~/SiteMaster.Master" AutoEventWireup="true" CodeBehind="WorkloadLec.aspx.cs" Inherits="UniSystem.WorkloadLec" %>
<%@ MasterType VirtualPath="~/SiteMaster.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

<!DOCTYPE html>
    

    <div style="height: 800px; width: 980px;">
    <h2 position: absolute; left: 100px; top: 150px;>
        Register Lecturer  
    </h2>
        <br />
              <asp:datagrid id="dgLecturerWorkload" runat="server" BackColor="White" BorderStyle="Solid" 
                BorderColor="Transparent" AutoGenerateColumns="False" Width="980px" ShowFooter="True"
                OnItemDataBound="dgLecturerWorkload_ItemDataBound" OnItemCommand="dgLecturerWorkload_ItemCommand"
                AllowPaging="True" OnPageIndexChanged="dgLecturerWorkload_PageIndexChanged" Height="300px">
                
                  <AlternatingItemStyle CssClass="Even_Row_Admin"></AlternatingItemStyle>
                
                  <HeaderStyle CssClass="Table_Header_Admin"></HeaderStyle>
                
                <Columns>

                        <asp:TemplateColumn HeaderText="No">
                            <HeaderStyle Font-Bold="True"/>
                            <ItemTemplate>
                                <%# (dgLecturerWorkload.PageSize*dgLecturerWorkload.CurrentPageIndex)+ Container.ItemIndex+1%>
                            </ItemTemplate>
                            <ItemStyle Width="5%" />
                        </asp:TemplateColumn>


                        <asp:TemplateColumn HeaderText="Subject">
                            <HeaderStyle Font-Bold="True"/>
                            <ItemTemplate>
                                <asp:Label ID="lblSubjectName" Runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "SUBJECT_NAME") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle Width="50%" />
                        </asp:TemplateColumn>
                        
                    
                        <asp:TemplateColumn HeaderText="Create Assignment/Tutorial">
                            <HeaderStyle Font-Bold="True"/>
                            <ItemTemplate>
                                <asp:Button ID="btnCreateAsgnmt" Runat="server" CommandName="CreateAsgnmt" Text="Create" />
                            </ItemTemplate>
                            <ItemStyle Width="15%" />
                        </asp:TemplateColumn>
  
 
                        <asp:TemplateColumn HeaderText="Quiz True/False">
                            <HeaderStyle Font-Bold="True"/>
                            <ItemTemplate>
                                <asp:Button ID="btnResultQuizTF" Runat="server" CommandName="ResultQuizTF" Text="Result" />
                                <asp:Button ID="btnCreateQuizTF" Runat="server" CommandName="CreateQuizTF" Text="Create/View Quiz" />
                            </ItemTemplate>
                            <ItemStyle Width="15%" />
                        </asp:TemplateColumn>


                        <asp:TemplateColumn HeaderText="Quiz Objective">
                            <HeaderStyle Font-Bold="True"/>
                            <ItemTemplate>
                                <asp:Button ID="btnResultQuizObj" Runat="server" CommandName="ResultQuizObj" Text="Result" />
                                <asp:Button ID="btnCreateQuizObj" Runat="server" CommandName="CreateQuizObj" Text="Create/View Quiz" />
                            </ItemTemplate>
                            <ItemStyle Width="15%" />
                        </asp:TemplateColumn>

                </Columns>

            <PagerStyle Mode="NumericPages" />
 
    </asp:datagrid>
        

        <asp:DropDownList ID="ddlLecturerWorkload" runat="server" ></asp:DropDownList>

        <br />
        <br />
        <br />
        <asp:Label ID="lblAttention" runat="server" CssClass="Note"></asp:Label>
             
                
</div>



        <div class="center" style="width: 980px; height: 59px;">
        
            Centered
        </div>


</asp:Content>

WorkloadLec.aspx.cs

namespace UniSystem
{
    public partial class WorkloadLec : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e) 
        {

            if (Session["loginid"] != null)
            {  
                {
                    Master.btnlogin.Visible = false;
                }         
            }

            if (!IsPostBack)
            {
                SetupSubject();
            }

        }

        public void SetupSubject()
        {
            dgLecturerWorkload.ShowFooter = true;

            WorkloadLecC std = new WorkloadLecC();
            DataSet ds = std.GetDisplaySubj();
            dgLecturerWorkload.DataSource = ds.Tables["SUBJECT"];
            dgLecturerWorkload.DataBind();


            ArrayList arAnswerEyeList = std.SelecSubj("", "");
            ddlLecturerWorkload.DataSource = arAnswerEyeList;
            ddlLecturerWorkload.DataTextField = "Value";
            ddlLecturerWorkload.DataValueField = "Key";
            ddlLecturerWorkload.DataBind();
        }


        protected void dgLecturerWorkload_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
        {
            dgLecturerWorkload.CurrentPageIndex = e.NewPageIndex;
            SetupSubject();
        }



        protected void dgLecturerWorkload_ItemDataBound(object source, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {

            if (e.Item.FindControl("DeleteUser") != null)
            {
                ((Button)e.Item.FindControl("btnDeleteUser")).Attributes.Add("onClick", "return confirm('Are you sure wish to delete the data?');");
            }


        }
protected void dgEdit_Student(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            dgLecturerWorkload.EditItemIndex = e.Item.ItemIndex;
            SetupSubject();
        }

        protected void dgCancel_Student(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            dgLecturerWorkload.EditItemIndex = -1;
            SetupSubject();
        }
}
}
}

So when a user clicks the login button in the masterpage, it will redirect to loginpage.aspx, then from the login page, it redirects to WorkloadLec.aspx like a dashboard. As the dashboard also displays the same navbar from the masterpage, i want to hide the login button after user logged in. However, StackOverflowException is being thrown at the return.this.btnlogin.

I follow all the steps in https://www.youtube.com/watch?v=GrKZ2YFsr8s but still cant get it to work. Please help me.

Solution

Is there another protected (probably auto generated) variable you meant to reference? The getter is referencing itself, which is causing your StackOverflowExecption.

public LinkButton btnlogin
{
    get {
      return this.btnlogin; <-- this should be another variable
    }
} 
Answered By: Anonymous

Related Articles

  • How to properly do JSON API GET requests and assign output…
  • How to parse JSON with XE2 dbxJSON
  • The 'compilation' argument must be an instance of…
  • Azure Availability Zone ARM Config
  • Avoid creating new session on each axios request laravel
  • Event Snippet for Google only shows one event while testing…
  • Search match multiple values in single field in…
  • Why does this Azure Resource Manager Template fail…
  • NullpointerException error while working with choiceBox and…
  • loop and eliminate unwanted lines with beautiful soup

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How can I pass the local value of a foreach of a function in the argument of another function(no events)?

Next Post:

How can I add new rows in Datatables under the specific columns?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error