Disable Dropdownlist some items based on condition in asp.net - Free Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server,WCF,Entity framework snippets and tutorial

Disable Dropdownlist some items based on condition in asp.net

In this article I am going to explain how to disable Dropdownlist some items based on condition in asp.net using C#, VB.net

HML Markup:

<asp:DropDownList ID="ddlItems" runat="server" CssClass="dropdownlist" AutoPostBack="true" >
         <asp:ListItem Selected="True" Value="-1">--Select Item--</asp:ListItem>
            <asp:ListItem>Item 1</asp:ListItem>
            <asp:ListItem>Item 2</asp:ListItem>
            <asp:ListItem>Item 3</asp:ListItem>
            <asp:ListItem>Item 4</asp:ListItem>
            <asp:ListItem>Item 5</asp:ListItem>             

Disable dropdownlist item
To disable the dropdownlist  some item create a method and  call it on page load.

C# Code:
protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)

protected void DisableDropdownItem()
            foreach (ListItem item in ddlItems.Items)
                if (item.Text == "Item 1" || item.Text == "Item 4")
                    item.Attributes.Add("disabled", "disabled");
        catch (Exception ex)

VB.net Code:

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
        End If
    End Sub
  Protected Sub DisableDropdownItem()
            For Each item As ListItem In ddlItems.Items
                If item.Text = "Item1" OrElse item.Text = "Item4" Then
                    item.Attributes.Add("disabled", "disabled")
                End If
        Catch ex As Exception
        End Try
    End Sub

What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.

Share this

Share on FacebookTweet on TwitterPlus on Google+