After enabling by javascript and postback, the selected item in the ListBox is not displaying correctly

I encountered a strange issue with a JavaScript function I have to enable a listbox that is disabled when it loads. The function works perfectly to enable or disable the listbox. However, after the user clicks the save button, it does not capture the newly selected item and instead retains the old selected item from databind. I have checked the isPostback on Page load and if it is not a postback, the listbox is not loaded again. Can anyone provide a hint to help me solve this problem? Thanks in advance.

function enableProject() {           
    var chk = document.getElementById('chkProject');
    if (chk.checked) {
        document.getElementById('listProject').disabled = false;
    }
    else {
        document.getElementById('listProject').disabled = true;
    }
}

Below is my ASP.NET page:

<asp:ListBox id="listProject" runat="server"
    SelectionMode="Multiple" Rows="5" class="w200"  Enabled="false"/>
<asp:checkbox id="chkProject" runat="server"
    Text="Enable" onclick="enableProject()" />

The code snippet below is for checking the ListBox:

For Each projectItem In listProject.Items
    If projectItem.Selected Then
        'create a new project object
        Dim objProj As New Data.Project( ID, projectItem.Value)
        objProjects.Add Project(objProj)
    End If
Next

Answer №1

To begin, ensure that your code is not 'rebinding' on each postback. This could be the reason why you are seeing the same results. Could you please share the code where you are binding the ListBox?

If the control is disabled during postback, it will not capture any new values and will rely on the original data stored in ViewState. Therefore, confirm that the control is enabled before the postback.

For reference, here is a link that demonstrates this behavior:

https://web.archive.org/web/20210420005338/https://aspnet.4guysfromrolla.com/demos/ProblemNotReturningDisabledControls.aspx

If you suspect this is the issue, you can test by adding the following property to your form tag to ensure disabled controls are posted back:

<form id="form1" submitdisabledcontrols="true" runat="server">

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

What is the best way to associate the settings found in a JSON file with an object in C#?

My goal is to map the configuration settings from a JSON file to an instance of HttpApiClientOptions. However, I am facing challenges with binding the properties, particularly the Body property. Even after specifying it as Dictionary<string, object>? ...

How do I use props to enable and conceal elements like lists, buttons, and images?

Check out this unique component: <ReusedHeader H1headerGray="text here... " H2headerRed="text2 here ! " pheader="p1" getStarted="button text1" hrefab="button url 1" whatWeDo="button text ...

What is the return value of the .pipe() method in gulp?

When using the code snippet below, what will be the input to and output of .pipe(gulpIf('*.css', cssnano()))? gulp.task('useref', function(){ return gulp.src('app/*.html') .pipe(useref()) .pipe(gulpIf('*.js&apo ...

Steps to extract a portion of a URL and display it in the "src" attribute

Here is the URL: My Code: (please refer to the JS comments for instructions on how to complete steps 2 and 3) <script> function mdlbox() { //step 1: Show the modal box var y = document.getElementsByClassName('modalDialog'); ...

What is the reason behind Q.js promises becoming asynchronous once they have been resolved?

Upon encountering the following code snippet: var deferred = Q.defer(); deferred.resolve(); var a = deferred.promise.then(function() { console.log(1); }); console.log(2); I am puzzled as to why I am seeing 2, then 1 in the console. Although I ...

Tips for eliminating duplicate entries in ag grid using Angular

Is there a way to eliminate the recurring assetCode entries in ag grid? The PRN and PRN1 values seem to be repeating unnecessarily. Check out the code below: list.component.ts ngOnInit() { this.rowData.push( { 'code': 'Machi ...

Loading external scripts prior to component loading in Vue.js

Within my Vue project, I have the need to fetch a script from a server location (e.g. https://myurl.com/API.js). This script contains a variable that I intend to utilize within my Vue component/view. The issue arises when I attempt to load this script usi ...

What is the solution for repairing a slideshow?

I would like to create a carousel of cards in my index.html file. The current code I have displays all five cards stacked vertically with non-functional clickable arrows. Is there a way to show one card at a time and allow users to click through them? < ...

Tips for applying a class to an element depending on data stored in Local Storage using JQuery

I am currently working on a TODO list project with functions to save and delete records already in place. However, I am facing challenges with the functions for marking items as important and done. One method I use is to retrieve saved items from Local St ...

Obtaining the "match" object within a Custom Filter Selector on jQuery version 1.8

Here's a helpful resource on Creating a Custom Filter Selector with jQuery for your reference. A Quick Overview: If you're unfamiliar with jQuery's Custom Filter Selectors, they allow you to extend jQuery’s selector expressions by addi ...

Enhance your React application by using a personalized hook that allows you to trigger a function

After creating a custom hook to handle uploads to an AWS S3 bucket, I encountered a small issue. Rather than having the hook execute the logic directly, I decided to create an executable function to return instead. However, I am facing a problem where the ...

The loading spinner fails to function when triggered by the ng-click or ng-change events in an AngularJS application

I have implemented a loading spinner to display while the page is loading. App.controller('MailFolderController', ['$scope', '$http',function ($scope, $http){ $scope.loading = true; $scope.allotmentStatus = [ {"value ...

What is the best way to modify an array of objects within component state?

I am currently working on updating a specific property of an object that is stored in an array. Here's a glimpse of my current state: state = { todos: [ { id: '1', title: 'first item, completed: false }, { ...

Creating a personalized context menu in Javascript: The ultimate guide to incorporating copy and paste features

We are developing a unique context menu for our online text editor, complete with copy/paste options. However, we encountered difficulties accessing the system clipboard from within the browser. It once seemed impossible to achieve this, as discussed in th ...

Discover the proper technique to display an error message in cases where no data is detected during the filtering process

I'm currently working on a component that involves search filtering and dropdown filtering. The filtering functionality is working fine, but I'm struggling to determine where to display an error message if no data is found during the filtering pr ...

How come the Qunit counter doesn't seem to ever reach its expected target value during this test?

I'm currently stuck on a Qunit test related to jQuery Mobile's listview.filter extension. I can't figure out how the variable _refreshCornersCount ends up with a value of 3. Below is the section causing confusion. module( "Custom search ...

Utilizing Vue.js for Keyboard Binding

I'm brand new to coding and Vue, and I've hit a roadblock in my project. I'm creating a JavaScript calculator that needs to be usable both with a mouse and keyboard. While I've managed to make it work with the mouse, I just can't ...

Encountered an issue while trying to register a service worker: "[ERROR] Cannot import statement outside

With the utilization of register-service-worker, the boilerplate for registerServiceWorker remained untouched. /* eslint-disable no-console */ import { register } from 'register-service-worker'; if (process.env.NODE_ENV === 'production&ap ...

Switch the jQuery overlay image upon clicking a button

Is there a way to dynamically change the overlay image of a jQuery-ui dialog using a button click from inside the dialog? I attempted to do so in the code snippet below, but unfortunately, the overlay image does not update. It seems that I need to manipu ...

Seeking assistance with coding a beginner-level Google Chrome extension

Currently, I am working on developing a basic Google Chrome extension with 2 or 3 browser actions. I have been using Selenium IDE to capture the necessary steps in Firefox that I need for my project. However, I am unsure of how to translate these recorde ...