Sometimes, your User Control needs to get some unique identifier from the page it is hosted in.
Let’s assume you have a side menu that is built server-side, and all the fiddling with friendly URL’s and routes is just not enough of a constant for you to set the active item in the menu.

On the page, declare a variable. In the case below, I call it PageId.
This is because my side menu is built from the database and table.Menu.PageId and table.Page.Id is in a relationship.
I guess you would call this a database first approach because every page in my system is actually listed in the table.Pages and from there you have table.Menu, table.RolePages, table.UserRoles etc, all interrelated.
I therefore set PageId = “5”, because in table.Pages.Id its the primary key. (Of course, if you do not want to ship the DB with your app, you can later use reflection and let the page register itself inside blank-fresh database)

namespace MyAppName.Pages
{
    public partial class MyPageName : System.Web.UI.Page
    {
        public string PageId { get { return "5"; } }

        protected void Page_Load(object sender, EventArgs e)
        {
        }
    }
}

Each participating page in your application now has a unique value for PageId. Over to the ASCX user control.

  1. See the variable at the top called ParentPageId, it’s storing in the ViewState so that I do not lose it over postback etc.
  2. Then in the Page Load event, only executing for !IsPosback, I set the ParentPageId. See SetParentPageId().
  3. Later, in the logic of building the menu, I then compare the PageId coming from the database, with ParentPageId, and can make the list item active.
    if (pageid == ParentPageId) { li.Attributes.Add(“class”, “active”); }
  4. After Note: return ViewState[“ParentPageId”] != null ? (string)ViewState[“ParentPageId”] : “0”;    –> do NOT make this return ViewState[“ParentPageId”] != null ? (string)ViewState[“ParentPageId”] : “”;
    Do not pass back an Empty string, as this will try and enable items that do not have PageIds (some of your pages may not have the PageId set, and in that case, pass some value that you know does not exist, like “0” or “-999”.
using System;
using System.Configuration;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace MyAppName.Controls
{
    public partial class Sidebar : System.Web.UI.UserControl
    {
        private string ParentPageId
        {
            get
            {
                return ViewState["ParentPageId"] != null ? (string)ViewState["ParentPageId"] : "0";
            }
            set
            {
                ViewState["ParentPageId"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetParentPageId();
                BuildSidebarMenu();
            }
        }

        protected void SetParentPageId()
        {
            Type t = Page.GetType();
            System.Reflection.PropertyInfo[] pi = t.GetProperties();
            foreach (System.Reflection.PropertyInfo pinfo in pi)
            {
                if (pinfo.Name == "PageId")
                {
                    ParentPageId = (string)pinfo.GetValue(Page, null);
                    break;
                }
            }
        }

        protected void BuildSidebarMenu()
        {
            // My code to build the sidebar
        }

        protected DataTable GetSidebarMenuTable()
        {
            string sql = @"SELECT ....; ";
            DataSet ds = new DataSet();
            // My code to get the data

            DataTable dt = ds.Tables[0];
            return dt;
        }

        protected HtmlGenericControl ListItem(string id, string menutitle, string menuicon, string page, string pageid, DataTable dt)
        {
            DataRow[] matchingchildrenrows = dt.Select("MenuParentId = " + id);
            HtmlGenericControl li = new HtmlGenericControl("li");
            if (!String.IsNullOrEmpty(pageid))
            {
                if (!String.IsNullOrEmpty(ParentPageId))
                {
                    if (pageid == ParentPageId) { li.Attributes.Add("class", "active"); }
                }
            }
            // The rest of my code here to set classes, create hyperlink-, text-, icon- elements, etc.
            // ...
            // ...
            return li;
        }

    }
}

The same approach can then used form page permissions.

Pages got Ids and your relationship between the logged in UserId, RoleId, PageRoles, etc. then being used to control page access, all pivoting around a simple ID.