Manipulating the "is-invalid" class in CodeBehind with ASP.NET

I'm in the process of developing an ASP site that requires users to fill out a form. I have incorporated textboxes, dropdown lists, and utilized Bootstrap 4 for styling purposes. The entire content is enclosed within an update panel to ensure a responsive user experience without necessitating a full postback.

Upon submission of the form, I conduct a check to ascertain if any of the fields, be it textboxes or dropdown lists, are left empty. In such instances, I refrain from inserting data into the database and mark the empty fields by adding "is-invalid" to their CssClass property.

Control.CssClass += " is-invalid";

Initially, everything functions as intended with the empty fields displaying in red.

However, when the user proceeds to fill in the formerly empty fields and clicks submit again, the fields persist in appearing red despite my attempts to remove the "is-invalid" class by replacing it with an empty string.

Control.CssClass.Replace(" is-invalid", "");

My query revolves around why the red highlighting fails to vanish. If altering the CssClass via the CodeBehind works seamlessly during append operations, then why does the removal operation fall short?

PS.: I am aware that employing JavaScript could offer a solution, but I strongly prefer maintaining the entirety of the validation process within the CodeBehind rather than splitting it between C# (.cs) and JavaScript functions. Nevertheless, if resolving this issue becomes unattainable solely using CodeBehind, I may resort to leveraging JavaScript, though I remain hopeful for a resolution utilizing C#.

Answer №1

Control.ModifyCssClass(" is-invalid", "");

This method extracts the string from the CssClass property, removes the specified is-invalid value, and then discards the modified string without updating the property itself.

If you intend to update the property with the modified value, you should assign it back like this:

Control.CssClass = Control.ModifyCssClass(" is-invalid", "");

Answer №2

When during the page lifecycle did you incorporate this? It seems like a promising initial point of reference.

Additionally, it would be beneficial to have a glimpse of your code.

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

Incorporating a URL into an HTML marquee

As a beginner in coding, I'm currently exploring how to incorporate a link into my marquee. My goal is to eliminate any color change or underline animation as well. Do you have any recommendations? .Marquee { color: #da6fe2; background-color: t ...

Tips for disabling default browser input validation in React

https://i.stack.imgur.com/834LB.png Is there a way to remove the message "Please fill out this field" while still keeping the "required" attribute intact? I have my own validation system in place, so I need to use the "required" attribute to determine whe ...

Attach a floating div to multiple elements throughout a webpage

Looking for the optimal method to place a div in various locations on a webpage while anchoring it from the bottom left. The div should be able to be attached to any element on the page for relative positioning, specifically intended for a tooltip. Each ...

Looking for a way to include spaces and special characters in a datatable column in .NET?

When trying to retrieve the datatable columns for Authorization Area and Test Fees [AED], I am currently getting the following column names: dt.Columns["AUTH_PRIOD_AREA"].ColumnName = "Authorization Area"; dt.Columns["Test_FEES"].ColumnName = "Test Fe ...

Beyond the footer area on the right side

I have a two-column layout, but I'm having issues with the footer area. When I add a border around it, the right side extends beyond what I expected. Check out the issue here. I tested it in Firefox and used some CSS: footer { border-style: solid ...

Building Dynamic Forms in ASP.NET: A Step-By-Step Guide

I need to dynamically create a form using the following HTML structure: <form action='http://www.example.com' method='POST' name=''> <input type='HIDDEN' name='' value=''> <input t ...

Understanding how to access an Internal ConfigurationSection from Web.Config within an ASP.NET environment

Is there a way for me to access a Microsoft defined ConfigurationSection called DataCacheClientSection that has an internal type? Microsoft.ApplicationServer.Caching.DataCacheClientSection Unfortunately, I cannot access it in the conventional way like ...

Tips for adjusting the side layout in Bootstrap

Is there a way to keep the side navigation fixed when scrolling down? See it in action here <div class="page-container"> <!-- top navbar --> <div class="navbar navbar-default navbar-fixed-top" role="navigation"> <div class="conta ...

Utilizing local storage for the functionality of my Save Button

My innovative program features a drag-and-drop functionality for assigning players to different teams. If 'Player A' is dragged into the 'Team D' column, I am looking for ways to manipulate localStorage or any other function to save the ...

Instantiating an interface

Can someone help answer this query: If IEnumerable is an interface, why does .Net allow us to instantiate an instance of IEnumerable? IEnumerable<string> Errors = new IEnumerable<string>() Why can't we create instances of our own interfa ...

Adjust the height of the page to fit the available screen space on mobile devices

Seeking a solution to ensure my web app fills the entire height of the device. Currently utilizing 100vh on the body tag, however, Safari tab on iOS adds an additional height which causes users to have to scroll down to view the full page. Any suggestions ...

How can I prevent Hover effects on active LI elements in a menu?

When a Navigation menu item (LI) is marked as active in CSS, how can I prevent it from being hover-able? Check out the CSS code below: .page-header .menu li { float:left; display: inline; margin-right: 2px; line-height: 34px; } .page-he ...

Strategies for Increasing Index Values in JavaScript

I attempted to modify the data attribute in my code snippet below. After clicking on the square, the data attribute should be incremented. However, it appears that the incrementing is not happening as expected. How can I resolve this issue? Additionall ...

What is the optimal method for configuring software preferences?

I am currently working with C# .NET. Within my software, I have implemented a settings dialog that allows users to configure application settings that need to be saved to a file. Key requirements include: All classes I have created should have access t ...

Decrease the dimensions of the image while maintaining its width at 100%

I've got a dilemma with the images on my website that are linked from image hosting sites. They all need to be displayed at 100% width, but some of them have huge pixel dimensions which cause slow loading times. Is there a way to resize the images wi ...

Troubleshooting the Problem of Uneven Heights in Bootstrap Columns

I am facing an issue with a dropdown inside a Bootstrap grid. When I click on the dropdown, the height of the row is not increasing as expected. Can anyone guide me on how to achieve this? HTML: <div class="container"> <div class=& ...

Set Bootstrap column size to match a particular column

I am currently using BootStrap 4 and I am attempting to make a column's height equal to another column. Below is the code snippet I am working with: <div class="container" style="overflow:hidden"> <div class="row" style="overflow:hidden" ...

Tips for utilizing a variable within a variable containing HTML code

Is it possible to incorporate variables in a JavaScript function that includes HTML code? Let's consider the following example: function SetCFonts() { var Color = $('#CColor').val(); var Font = $('#CFont').val(); var S ...

What is the best way to achieve a consistent style for two tables?

I would like to achieve uniform styling for each row. The first row, which contains 'het regent vandaag', has the following CSS rule: .attachments-table td { margin: 0 5px; padding: 10px 8px; line-height: 1.4; white-space: pre-wr ...

Stylish Bootstrap sidebar featuring a vibrant background hue

I am facing an issue with my bootstrap layout where I have two columns - one on the LEFT and one on the RIGHT. The left column should be 8 units wide using the Bootstrap's 12 column grid format, while the right column should be 4 units wide as shown b ...