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

Take away the dropdown selection once the form has been submitted

Every day, a user fills out a form ten times. They choose an option from the dropdown menu, fill out the form, and submit it. I am looking for a solution to either remove the selected option once it's been submitted or mark it as complete by highlight ...

Can you please tell me the name of this specific graph? And could you explain how I can recreate it on a

I am fully aware that this information is just a Google search away. However, I am unsure of the specific name of the chart I am seeking. Therefore, I am unable to conduct an effective search for suitable plugins or APIs. Your understanding is greatly appr ...

CSS modified after opening a modal dialog that has loaded external HTML content

Within my ASP.NET MVC project, I am utilizing a tab wizard. On one of the tabs, I trigger a modal dialog by loading HTML content from an external API. However, once I close the wizard and navigate to the next tab, the table style (specifically border color ...

IE Browser Exposes Flawed Design in JSP Coding

I have a pair of JSP files. The first one features a table, while the second one contains rows (<tr>) that are meant to be added to the table. Within the second JSP file, I initialize the same action, followed by the <tr> element that should b ...

verifying the tags entered by the user

Although this question might come across as a bit cliché, it's actually my first time incorporating a "tagging" feature into one of my project websites. I want to ensure that I handle it correctly. Currently, I am using the same tagging system as se ...

The background image on mobile devices is excessively zoomed in

My RWD page is experiencing a strange issue with the background image. The image has the following styles: #background-image { width: 100%; height: 100%; opacity: 0.5; position: absolute; z-index: -1; background-image: url('../landing.jpe ...

Toggle the visibility of an element by clicking a button

I have a table structured as follows: <tr> <td class="title">Title</td> <td class="body">Body</td> <td class="any">Any text</td> </tr> <tr> <td class="title">Title</td> ...

Manipulate DIV elements with jQuery by selecting them based on their class names and then

Is there a way to use jQuery to eliminate all DIVs on a webpage while preserving their content that have the following specific pattern? <div class="ExternalClass85AC900AB26A481DBDC8ADAE7A02F039">....</div> Please note that these DIVs adhere ...

Unable to locate the sibling element using CSS Selector with Selenium

When using a CSS Selector to click on the next sibling element in Selenium WebDriver, an InvalidSelectorException is thrown. If we consider my DOM structure: <div class="checkbox-group"> <div> <span class="checkbox">::aft ...

Confirm a chosen dropdown option by its value in Selenium utilizing C#

I need to confirm the selected dropdown option by its value using the code below: SelectElement selectElement = new SelectElement(Base.driver.FindElement(by)); selectElement.SelectByValue(value); However, I am facing a challenge in verifying if the corre ...

Angular controller utilizing the `focusin` and `focusout` events from jQuery

Can anyone help me figure out why this piece of code is generating syntax errors in my AngularJS controller? $(".editRecur").focusin(function() { $(.recurBox).addClass("focus"); }).focusout(function() { $(.recurBox).removeClass("focus"); }); ...

Implementing pagination in Webgrid using AJAX post method

I've developed this JavaScript code: function PartialViewLoad() { $.ajaxSetup({ cache: false }); $.ajax({ url: "/ControllerAlpha/MethodBeta", type: "GET", dataType: "html", data: { s ...

What is the best way to alter the background of a div when hovering over an image?

I am looking to achieve a unique effect with my thumbnail image and background. Currently, I have the thumbnail image inside a div with another image serving as the background to frame it. My goal is to change the position of the background image from bott ...

Can you tell me what is creating the space between the elements in this form?

For an example, take a look at this link: I am puzzled by the gap between the form-group and the button, as well as between the different form-groups. When using Bootstrap 4 with flexbox activated, all my elements (inputs and buttons) collapse without any ...

What is the best way to dynamically render classes based on conditions in a table using React Bootstrap?

I am looking for a way to dynamically change the color of a class based on the transaction data in a table. import React from "react"; import Table from "react-bootstrap/Table"; import "./TableComponent.css"; const TableComponent = ({ Movement }) =&g ...

Modifying the font style of text within an HTML string using SQL Server

I need to standardize font sizes in my database where strings are stored as html and users can modify the font size. However, for generating reports, I require all font sizes to be consistent. If I have the following html code, how can I change the font si ...

Utilize the :not() and :hover selectors in Less when styling with CSS

Some of my elements have a specific class called .option, while others also have an additional class named .selected. I want to add some style definition for the :hover state on the .option elements, but only for those without the .selected class. I' ...

Angularjs - Repeatedly reloading identical images

Whenever I use ng-src in the <img> tag and change the ng-src by clicking next or previous buttons, the browser reloads the image even though it's already loaded. This results in a not-so-smooth experience as the image is downloaded again before ...

Issue with the appearance of the footer in my Wordpress template

After creating a WordPress template, I noticed that the footer is not extending to 100% width as expected. Check out the page here Any suggestions on why this might be happening? Could it be due to a missing closing div tag somewhere in the code? Thank ...

Ways to modify the appearance of the button within ion-calendar

Looking to customize the styling of ion-calendar classes Attempting to add styles to the ion-calendar-month class, but not seeing any changes take effect. ...