Juhil H Patel
Juhil H Patel

Reputation: 1

How to make dropdownlist item's unselectable,once it has been selected


                    <asp:ListItem>1</asp:ListItem>

                    <asp:ListItem>2</asp:ListItem>

                    <asp:ListItem>3</asp:ListItem>

I have one dropdownlist in my form in .aspx File.

Once I select "1", then next time if I redirect to same page then "1" should be

unselectable or Hidden for making sense that I have selected "1" previously because I have large items in my example.

Upvotes: 0

Views: 2086

Answers (4)

Bro
Bro

Reputation: 327

To remove the last selected item in your dropdownlist (DDL), considering the following code for your DDL form, you can use the onselectedindexchanged event of your DDL (that is raised when you click on one of your DDL items):

<asp:DropDownList ID="_DDL" runat="server" 
        onselectedindexchanged="_DDL_SelectedIndexChanged" AutoPostBack="true">
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>
        <asp:ListItem>3</asp:ListItem>
</asp:DropDownList>

And put on the code behind the RemoveAt() method to remove the selected Item from your DDL:

        protected void _DDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        int ItemToRemove = _DDL.SelectedIndex;
        _DDL.Items.RemoveAt(ItemToRemove);
    }

Hope this will be helpful :)

Upvotes: 0

Fahad Hussain
Fahad Hussain

Reputation: 1185

You can track your selection using Cookies, on the "onChange" event of your dropdown list, create a cookie with the selected value, then on body "onLoad" u can check and disable the items that are there in Cookie.

Or you can hold your selected values in a hidden field

Upvotes: 0

coder
coder

Reputation: 13250

Disabling any particular item in a dropdownlist is not possible.

Alternate:

You can use a BulletedList Server Control and use its Enable = False property to disable any particular item and all user can see that item as disabled.. here is a design time example..

<asp:BulletedList ID="BulletedList1" runat="server">
            <asp:ListItem>1</asp:ListItem>
            <asp:ListItem>2</asp:ListItem>
            <asp:ListItem Enabled="False">3</asp:ListItem>
            <asp:ListItem>4</asp:ListItem>
</asp:BulletedList>

Took from here

Upvotes: 1

Sumant
Sumant

Reputation: 163

You can use a hidden field to store which item was selected in code behind and disable that item using the following code.

//Code Starts
$(document).ready(function() {   
   $('#ddlList option:contains("HTML")').attr("disabled","disabled");
});​
//Code Ends

Upvotes: 0

Related Questions