locked
Disable items in ListBox rather then Checkbox RRS feed

  • Question

  • User679911585 posted

    Hi all,

     

    The following link http://www.codeproject.com/KB/webforms/disableListItems.aspx disables items 1 and 2 from the checkboxlist if the user selects checkbox item 3.

    Can someone PLEASE tell me how i could change the code in this link to work for ListBox.

    So if I had 5 items in the lisr box (below)

    AA

    BB

    CC--disable

    DD--disable

    EE

     

    It will disable items CC and DD. But this will be disabled when the form loads...

     

    please help me? please...

    Sunday, October 4, 2009 4:21 PM

Answers

  • User1564875471 posted

    In that case, you will need to handle the DataBound event of the ListBox control and set the disabled attribute through the attributes ListItem property:

    This is a full example which bind the ListBox programatically:

    <asp:ListBox ID="ListBox1" runat="server" OnDataBound="ListBox1_DataBound">
    </asp:ListBox>

       protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                // bind the ListBox.
                ListBox1.DataSource = new string[] { "AA", "BB", "CC", "DD", "EE" };
                ListBox1.DataBind();
            }
        }
        protected void ListBox1_DataBound(object sender, EventArgs e)
        {
            // find the CC item.
            ListItem ccItem = ListBox1.Items.FindByText("CC");
            if (ccItem != null)
                ccItem.Attributes.Add("disabled", "");
    
            // find DD item
            ListItem ddItem = ListBox1.Items.FindByText("DD");
            if (ddItem != null)
                ddItem.Attributes.Add("disabled", "");
    
        }


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 4, 2009 5:32 PM

All replies

  • User-2119480821 posted
    Html option tag (after ListBox rendered) does not support enable/disable functionality. You can add/remove or checked/unchecked items in ListBox. Use checkboxlist to achive your task, follow the link you posted.
    Sunday, October 4, 2009 5:03 PM
  • User1564875471 posted

    Adding "disabled" attribute to the ListItem should do the trick:

    <asp:ListBox ID="ListBox1" runat="server" AutoPostBack="true">
        <asp:ListItem Text="AA"></asp:ListItem>
        <asp:ListItem Text="BB"></asp:ListItem>
        <asp:ListItem Text="CC" disabled></asp:ListItem>
        <asp:ListItem Text="DD" disabled></asp:ListItem>
        <asp:ListItem Text="EE"></asp:ListItem>
    </asp:ListBox>


    Sunday, October 4, 2009 5:12 PM
  • User679911585 posted

     Hi, this works if the list is static...but my data is from a database...how can i modify the above...

    ...please help

    Sunday, October 4, 2009 5:20 PM
  • User1564875471 posted

    In that case, you will need to handle the DataBound event of the ListBox control and set the disabled attribute through the attributes ListItem property:

    This is a full example which bind the ListBox programatically:

    <asp:ListBox ID="ListBox1" runat="server" OnDataBound="ListBox1_DataBound">
    </asp:ListBox>

       protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
                // bind the ListBox.
                ListBox1.DataSource = new string[] { "AA", "BB", "CC", "DD", "EE" };
                ListBox1.DataBind();
            }
        }
        protected void ListBox1_DataBound(object sender, EventArgs e)
        {
            // find the CC item.
            ListItem ccItem = ListBox1.Items.FindByText("CC");
            if (ccItem != null)
                ccItem.Attributes.Add("disabled", "");
    
            // find DD item
            ListItem ddItem = ListBox1.Items.FindByText("DD");
            if (ddItem != null)
                ddItem.Attributes.Add("disabled", "");
    
        }


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 4, 2009 5:32 PM
  • User679911585 posted

     Thank u soo much. I have been looking for a solution to this for age. thank u! :)

    Sunday, October 4, 2009 5:38 PM
  • User1692533347 posted

    It is not possible for listbox because it doesn't render "input" tag.

    It works properly in Firefox and Google Chrome but doesn't in Internet explorer, must be a Microsoft's fault.

    Wednesday, December 23, 2009 1:41 AM