Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an application which is in .Net1.1 and upgraded to 3.5 using Visual studio 2008.

There is page which is having a checkbox and a list box which is enabled and disabled using Javascript if enabled we can select the items in List and upon button click the selected items will be processed.

The Selected items is working fine in .Net 1.1 but not in 3.5.I am giving sample code where I am facing the issue.The same code works fine in .Net1.1

Below are the aspx lines

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListBox.aspx.cs" Inherits="WebApplication1.ListBox" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>UsageReport</title>
        <script language="javascript">
            function isAllChecked()
            {
                var ctrlList = document.getElementById("lstUsers");
                var allChecked = document.getElementById("chkAll");
                var checked = allChecked.checked;

                if (checked) {

                    ctrlList.disabled = true;
                    unselectItems();
                }
                else {

                    ctrlList.disabled = false;

                }
            }

            function unselectItems()
            {
                var lstUsers = document.getElementById("lstUsers");
                var list = lstUsers;  

                for (var i = 0; i < list.length; i ++) 
                {
                    list[i].selected = false;
                }
                list.selectedIndex = -1;
            }
        </script>
    </head>

<body>
    <form id="form1" runat="server">
    <div>
    </div>
    <table>
        <tr>
            <td>
                &nbsp;</td>
            <td>
        <asp:CheckBox ID="chkAll" Checked="True"  runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
        <asp:ListBox ID="lstUsers" runat="server" SelectionMode="Multiple" Enabled="false"></asp:ListBox>
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

Below are the lines for CodeBehind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class ListBox : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.chkAll.Attributes["onclick"] = "isAllChecked();";
                lstUsers.DataSource = new string[] { "one", "two", "three" };
                lstUsers.DataBind();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (ListItem li in lstUsers.Items)
            {
                if(li.Selected)
                Response.Write(li.Value);
            }
        }
    }
}
share|improve this question

1 Answer

up vote 0 down vote accepted

After doing some research, The problem is with the ListBox is disabled by default, As ASP.Net 3.5 does not allow to submit the changes to disabled control is the root cause.

It is worked with a workaround by removing the enabled="false" from the ListBox

<asp:ListBox ID="lstUsers" runat="server" SelectionMode="Multiple"> </asp:ListBox>

and acheived the same functionality by calling the javascript onload of the body

<body onload="isAllChecked()" >

The hack i did here is as ASP.Net allow any changes to the disable controls so made that control as enabled at the server but from the client script made it disable

I also tried using submitdisabledcontrols="true" in the form tag but of no use for me

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.