Instructions on how to dynamically apply the "active" CSS class to a linkbutton in the code behind

I'm encountering an issue with link buttons on my master page. I want to dynamically add the CSS class "Active" after clicking each link button and postback URLs.

<asp:LinkButton ID="Linkbutton1" runat="server" PostBackUrl="/News.aspx?lang=1"
         Text="News" OnClick="Linkbutton1_Click">
</asp:LinkButton>

Linkbutton 1

Linkbutton 2 - class "active"

Linkbutton 3

I attempted to add the class using the link button's onclick event, but it gets removed after the postback occurs.

Answer №1

Make sure to include the following code snippet in your Linkbutton1_Click method:

Linkbutton1.CssClass = "active";

For more information, check out this helpful article on MSDN: MSDN Article.

Answer №2

To implement functionality in the code behind, cookies can be utilized:

Inside the Linkbutton1_Click method:

Response.Cookies["Linkbutton1-cssClass"].Value = "active";

Within the Page_Load method:

if(Request.Cookies["Linkbutton1-cssClass"] != null)
   Linkbutton1.CssClass = Server.HtmlEncode(Request.Cookies["Linkbutton1-cssClass"].Value);

Finally, a foreach loop can be used to handle all LinkButtons.

Answer №3

Call a JavaScript function changestyle(SenderID) when clicking on a link button

<script type="javascript">
var previousStyle = "";
function changestyle(SenderID)
{
   var LinkButtonActive = document.getElementById(senderID);
    LinkButtonActive.className = "subTabActive";

    if (previousStyle != "" && previousStyle != id)
    {
        var previousElement = document.getElementById(previousStyle);
        previousElement.className = "subTabInactive";
    }
    previousStyle = SenderID;
}
</script>

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

Is it achievable to animate dynamic height using CSS? Open to JS alternatives as well

I am currently utilizing AngularJS, which enables me to utilize ng-show and ng-hide in order to display or hide elements based on a logical condition. My goal is to animate the size changes of the container when the child objects are shown or hidden, creat ...

The default routing in MVC does not cooperate with the RoutePrefix attribute routing functionality

Although I have a workaround, I am curious as to why this particular configuration does not seem to function properly in MVC (.Net 4.6.1). I have a controller where I intend to utilize a RoutePrefix: [RoutePrefix("entry")] public class DefaultCo ...

Issue with duplicate selectors not being merged in SASS partials

Is there a way for SASS to combine duplicate CSS selectors from multiple files into a single output file? In my current setup, I have the same CSS selector present in separate SASS partials files that are all included in a master file. For example: File1 ...

Tips for utilizing numerous tables with multiple selection tags

I am struggling with a jQuery issue involving multiple container elements with the class "product-sizes". Each container contains a select option for choosing between inches and centimeters, which triggers the corresponding table display. The problem arise ...

Filter SQL server table records using multiple filter conditions

I have a Student table with multiple foreign keys used for filtering student records. The structure of the table looks like this: StudentId GenderId DegreeId NatioanlityId 1 2 2 3 .... Each student can sp ...

Accelerate blinking timer by utilizing CSS3 animations

I'm currently developing a quiz application that includes a timer counting down from 10 seconds to 0 seconds. As the timer reaches closer to 0 seconds, I am looking to implement a feature where my text blinks faster and faster. Additionally, I would l ...

Clear Localization

Incorporating explicit localization into my pages, such as: <asp:Literal ID="appTitle" runat="server" Text="<%$ Resources:TranslationResource, AppTitle %>"></asp:Literal></div> If the AppTitle resource is missing, is there a metho ...

Is it possible to split the Entity Framework ModelBuilder into separate files?

Can ModelBuilders for Entities be stored in separate files? We want to segregate automatic scaffolding from the database and include some manual customization as well. Here is what I am trying, but encountering an error: File 1: modelBuilder.Entity<P ...

TailwindCSS in React.Js does not seem to accept randomly generated color codes

Check out my Messages component below: import randomColor from "random-color"; import React from "react"; import "../Chat.css"; function DisplayMessage({ username, message }) { const changeTextColor = randomColor(0.99, 0.99 ...

Adjust the transparency of a selected item in a dropdown menu

Within my code, I have several select elements structured as follows: <li> <select> <option>choose something</option> <option value="1">something</option> <option value="2">anything< ...

Display only the static placeholder in Angular 2 multi-select feature

My experience with angular 4 is fairly new and I've recently delved into using the angular multiselect feature with the npm package available here. I've managed to successfully configure it, as well as capture selected and deselected items/event ...

Creating a mesmerizing parallax effect for your image: A step-by-step guide

Looking for help to create a parallax effect on an image when hovered over. Does anyone have any advice or resources? Feeling frustrated at the moment. Here is an example link for reference: http://codecanyon.net/item/jquery-moving-perspective/full_scre ...

Scrolling horizontally within SVG graphics

Check out this JSFiddle body { background-color: #111111; color: #ffffff; /*max-width: 100%;*/ overflow-x: hidden; } .row { max-width: 100rem; } .svgrow { min-width: 70rem; } .svgrow svg { overflow-x: auto; } I am trying to ma ...

Numerous Divs Expanding to Fit Window Size

I successfully managed to make one div expand to the size of the window, filling the screen completely. However, I am facing an issue where the other divs are overlapping each other and only displaying thing 3. How can I ensure that they do not overlap and ...

Creating a fluid grid layout using CSS that consists of just two cells that adjust

Is it possible to design a flexible CSS grid layout with two cells that can switch between stacking vertically or horizontally to maximize the area of the second cell? (or similar heuristic) Vertical Layout Horizontal Layout Ideally, the menu cell shoul ...

Encountering yet another frustrating issue with z-index not functioning properly in versions of IE above 7, despite extensive research yielding no solution

I have scoured numerous resources and read countless articles on how to resolve z-index issues in IE 7, 8, and 9. However, none of the suggested solutions seem to work for my particular situation. The issue at hand is that I have interactive content posit ...

Fix the issue of the screen bouncing around when the scroll bar is visible

When my modal popup appears on screen, I am using the CSS property overflow: hidden to eliminate the scrollbar. To ensure that the screen does not shift when the scrollbar disappears, I am calculating the scrollbar width using JavaScript and adding paddin ...

When the cursor hovers, a drop-down menu appears simultaneously

Currently utilizing the Bigcommerce stencil theme, I have encountered an issue where both dropdown menus appear when I hover over a link. My goal is to have only one dropdown menu pop up when hovering over a specific link. https://i.sstatic.net/paVoY.png ...

Can you suggest a method for randomly arranging the display of submitted input?

I'm still learning javascript and primarily focus on front end development. I want to display user submissions in a randomized manner on the screen, even if it means that everything gets wiped away upon refreshing the page. Currently, this is my code ...

Styling buttons using CSS for proper alignment

Struggling to center align two buttons on my website, none of the common methods seem to work. It's becoming a headache as nothing else is overriding it. Perhaps I'm missing something. This is my CSS button code: .btn { border-radius: 10px ! ...