I have disable the List option in which the values are N
HTML:
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" OnDataBound="DropDownList1_OnDataBound">
</asp:DropDownList>
</div>
</form>
C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GetProduct();
}
}
protected void DropDownList1_OnDataBound(object sender, EventArgs e)
{
foreach (ListItem item in DropDownList1.Items)
{
//Disable the item with value as N
if (item.Value.ToUpper() == "N")
{
item.Attributes.Add("style", "color:gray;");
item.Attributes.Add("disabled", "true");
}
}
}
private void GetProduct()
{
string constr = ConfigurationManager.ConnectionStrings["ConString2"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT * FROM TempTable", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
con.Open();
DataSet ds = new DataSet();
da.Fill(ds);
this.DropDownList1.DataTextField = "Name";
this.DropDownList1.DataValueField = "Status";
this.DropDownList1.DataSource = ds;
this.DropDownList1.DataBind();
}
}
}
}
SQL:
CREATE TABLE [dbo].[TempTable](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NOT NULL,
[Status] [varchar](50) NOT NULL,
CONSTRAINT [PK_TempTable] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
Thank You.