modify the appearance of HTML controls in real time with C#

Is there a way to dynamically add an additional navigation item to my website's menu list when the admin logs in through admin.aspx? The menu list is created using HTML controls such as <ul>...<li>, and I would like the new navigation item to be added seamlessly. Alternatively, I could initially include the admin menu item in the list with style { visibility:hidden}, and then change it to {visibility:visible } once the login is successful.

Here is an example of my master page code:

<ul id="ul_myLst" runat="server">
    <li><a href="Testimonials.aspx">Testimonial</a>
    </li>
    <li><a href="#fakelink">Contact Us</a>
    </li>
    <li><a href="#fakelink">About Us</a>
    </li>
    <li><a href="Registration.aspx">Registartion</a>
    </li>
    <li><a href="OurFaculty.aspx">Our Faculty</a>
    </li>
    <li id="abc" runat="server" style="visibility:hidden">
        <a href="OurFaculty.aspx">Admin</a>
    </li>
</ul>

And here is the Default.aspx code snippet:

if (f.pass.Equals(txtpass.Value)) {

    HtmlGenericControl ul = (HtmlGenericControl)(this.Master.FindControl("abc"));
    //ul.Attributes["class"] = "admin-p";
    ul.Style.Remove("visibility");
    ul.Style["visibility"] = "visible";

    Response.Redirect("Index.aspx");

}

The current code works well, but the issue arises when revisiting index.aspx as the admin menu automatically hides again.

Answer №1

It seems like your code is checking if the password entered matches a stored value during the login process.

If you have some sort of hidden input field or another method storing this information, you can set up a function to handle setting the Admin menu whenever the page loads. Make sure to adjust the "if" statement based on how the login value is being remembered on the site (such as Session, viewstate, or cookie).

Here's an example:

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

private void SetAdminMenu()
{
    if(f.pass.Equals(txtpass.Value))
    {
        abc.Visibility = visible;
    }
}

Answer №2

Saba, your approach needs some adjustments.

Consider these 2 straightforward methods to achieve your goal:

1. Modifying the visibility of a web control

In the code-behind file of the master page (MasterPage.master.cs), insert this line into either the OnLoad or Page_Load method:

var control = (HtmlGenericControl)Page.FindControl("abc");
control.Visible = User.IsInRole("Admin");

Remember to include runat="server" in the <li> tag within the MasterPage.master file.

2. Dynamically adding a new <li>

In the MasterPage.master file, ensure you add runat="server" to the <ul> tag.

Then, in the code-behind file of the master page, implement the following logic within the OnLoad or Page_Load method:

if(User.IsInRole("Admin"))
{
    var menuList = (HtmlGenericControl)Page.FindControl("ul_myLst");
    var adminItem = new HtmlGenericControl("li");
    var anchor = new HtmlAnchor();
    anchor.HRef = "OurFaculty.aspx";
    anchor.InnerText = "Admin";
    adminItem.Controls.Add(anchor);
    menuList.Controls.Add(adminItem);
}

Answer №3

In order to display the admin menu, you need to utilize session and verify the existence of the session value at the beginning of your master page. When returning to the master page, it will once again confirm the presence of the session value before allowing you to implement CSS code to make the admin menu visible.

Here is a breakdown of the technical steps:

  1. Establish a session on the second page (default.aspx)
  2. Check for the session value at the top of the master page: Use an if statement to determine if the session value exists, then include code to display the admin menu accordingly
  3. Create a log-out page to invalidate the session

To learn more about ASP sessions, visit: http://www.w3schools.com/asp/asp_sessions.asp

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Rzslider not functioning properly due to CSS issues

I am facing an issue where rzslider is not appearing in my app. However, when I copy the code to an online editor, it works perfectly fine. Below is the code snippet: var app = angular.module('rzSliderDemo', ['rzModule', 'ui.boo ...

ASP.NET - Severely struggling with connectivity issues involving AJAX/JQUERY

I have developed a real-time update script that refreshes certain div elements with server-side information every second. The issue I am facing is that the script seems to be causing heavy usage, as even my mouse animation in Google Chrome keeps loading. ...

Tips for changing the JSON result of a method in a WCF service

I'm currently exploring how to customize the name of the JSON object returned from a WCF service to a web client via an AJAX call, rather than using the default wrapper. Despite my efforts, I haven't been able to find any relevant articles on thi ...

Ensure the main page occupies the entire screen without the need for scrolling

Can someone help me with my Vue app? I need to figure out how to make the start page full screen without any scroll bars in the main window at any scale. The scrollbar should be located within "div class="bottom-center". <script setup> import Comp fr ...

Issues with the display property

After setting the display of a div tag as none in the style property, I am trying to show it based on an on-click function. However, the issue I am facing is that when I click on the function, the div displays for a brief moment then disappears again. Ca ...

Is there a way to access other HTML pages in a separate folder from the index file when running npm start?

At the moment, I have a simple index.html file and would like to access the app.html file located in a subfolder from the index.html. You can see the folder structure here: My app.html file is inside the 'app' subfolder. How can I reference that ...

Issues with Rails not successfully sending AJAX data to the controller

I am new to AJAX, so please bear with me while I try to figure this out. My goal is to send data from a chat message box to two places – the chat box itself and to a method in my Rails backend. This way, I can display the message in real-time and also st ...

The email submission function in my PHP form seems to be malfunctioning. I must be overlooking something

Having an issue with my PHP form, it's only sending field names instead of data to the email address. Here is a simplified version of the code: <?php $ToEmail = 'example@example.com'; $EmailSubject = 'Site contact form'; $ma ...

Turn off automatic resizing of iframes

I'm currently dealing with a webpage that contains an iframe. The iframe contains quite a bit of data, and every time it loads, its height adjusts to match the content within. Unfortunately, this is causing my page layout to be disrupted. Is there a w ...

Utilizing Bootstrap datepicker to set maximum and minimum dates based on another date input

I have been attempting to create a restriction on the date selection, where datepicker1 cannot exceed datepicker2, and vice versa. Initially, I tried implementing this feature using (Linked Pickers). Unfortunately, this did not achieve the desired date in ...

The image does not automatically adjust size when the window is resized

I'm having an issue with my HTML and CSS code that is supposed to display two images side by side and resize when the browser window shrinks, but it's not working as expected. Even after removing all classes on the parent divs, the images still ...

Creating a customized display on a webpage using XML and XSL to generate unique

Looking to utilize my xml file for generating various xsl pages. <movies> <movie> <name>Shark tank</name> <movie> <movie> <name>Tank Shark</name> <movie> ...

React Icons and Browser Tab Alerts

Is there a way to incorporate notifications into my website's favicon when the browser tab is not in focus? My tech stack includes React, Material UI, and TypeScript. I assume this is a frequent requirement, yet I haven't come across a simple so ...

Creating a dynamic table in HTML with Angular by programmatically inserting rows using components

There's a peculiar issue that I'm facing and I just can't seem to figure out why it's happening. My goal is to display an HTML table using Angular 4. When I use the following syntax to output the table, everything works perfectly: < ...

Utilizing Kendo MVVM to Bind to an Self-Executing Anonymous Module Function

Currently, I am experimenting with the Kendo UI MVVM framework and attempting to bind it to a self-executing anonymous modular function. The situation is a bit complex, as the functionality is only somewhat working. Although the module is being updated whe ...

developing a stand-alone job listings feature

Our website features a job postings page that our clients are interested in incorporating into their own websites. This would allow applicants to easily apply for jobs directly on the client's site, with the information being saved in our system. One ...

How can I transform an HTML element into a textarea using CSS or JavaScript?

While browsing a webpage, I discovered a <div> element with the class .plainMail and I want to find a way to easily select all its text by simply pressing Ctrl+A. Currently using Firefox 22, I am considering converting the div.plainMail into a texta ...

The value of the variable is failing to be included in the Ajax request being sent to the server via jQuery

I'm encountering an issue with my contact form where I can't seem to retrieve the $_POST values via ajax for saving to a text file. It seems like there might be a problem with my javascript code. Check out the jQuery code snippet below: functio ...

Choosing a sibling element before another using CSS

Is there a way to make both of my icons display some text when hovered over? I managed to make it work, but the leftmost icon doesn't trigger the span element when hovered. The text needs to appear on the left side of the Yelp icon for design reasons. ...

Can you explain how one applies selenium's ExpectedConditions within a page object model framework?

Hopefully I'm not the only one facing this issue. In my endeavor to write selenium tests in C# while implementing a page object model design, I've encountered a dilemma. The problem arises when I need to incorporate explicit waits using the Expe ...