Disable DropDownList Item (Option) in ASP.Net using C#

Theo: nguyenhaidang.name.vn | 25/04/2016 - 09:09

 In this article I will explain with an example, how to disable an item (option) i.e. make not Selectable in ASP.Net DropDownList using C# and VB.Net.

 
 
Database
I have made use of the following table Customers with the schema as follows.

Make First (Default) item (option) not Selectable in ASP.Net DropDownList using C# and VB.Net

I have already inserted few records in the table.

Make First (Default) item (option) not Selectable in ASP.Net DropDownList using C# and VB.Net

 
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
HTML Markup
The HTML Markup consists of an ASP.Net DropDownList which will be populated from database.
<asp:DropDownList ID = "ddlCustomers" runat="server">
</asp:DropDownList>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
 
 
Disable DropDownList Item (Option) in ASP.Net
Inside the Page Load event of the page, the DropDownList is populated with the records of the Customers Table using SqlDataReader. For more details about populating DropDownList using SqlDataReader, please refer my article Bind (Populate) ASP.Net DropDownList using DataReader in C# and VB.Net.
Once the records from database are populated, a default item (option) is inserted at the first position and is marked as Selected. Finally the default item (option) is disabled (not selectable) by adding HTML disabled attribute.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name FROM Customers"))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                con.Open();
                ddlCustomers.DataSource = cmd.ExecuteReader();
                ddlCustomers.DataTextField = "Name";
                ddlCustomers.DataValueField = "CustomerId";
                ddlCustomers.DataBind();
                con.Close();
            }
        }
 
        //Add a default item at first position.
        ddlCustomers.Items.Insert(0, new ListItem("Please select"""));
 
        //Set the default item as selected.
        ddlCustomers.Items[0].Selected = true;
 
        //Disable the default item.
        ddlCustomers.Items[0].Attributes["disabled"] = "disabled";
    }
}
 
Back Head Print
Tin khác

Search GridView with Paging on TextBox KeyPress using jQuery in ASP.Net    (28/07/2010)

Bootstrap AutoComplete TextBox example using jQuery TypeAhead plugin in ASP.Net with C# and VB.Net    (28/07/2010)

Disable Button and Submit button after one click using JavaScript and jQuery    (29/07/2010)

Split and convert Comma Separated (Delimited) String to Table in SQL Server    (01/09/2010)

Select Column values as Comma Separated (Delimited) string in SQL Server using COALESCE    (01/09/2010)