In
this article I am going to explain how to disable Dropdownlist some items based
on condition in asp.net using C#, VB.net
In
the previous article I have explained how to remove the selected item fromdropdownlist in asp.net using C#, VB.net, how to show the data in Gridviewclient side using Jquery, Json and Ajax and how to populate Category andSub-category in single dropdownlist in Asp.net.
Implementation:
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>
</asp:DropDownList>
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)
{
DisableDropdownItem();
}
}
protected void DisableDropdownItem()
{
try
{
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
DisableDropdownItem()
End If
End Sub
Protected Sub DisableDropdownItem()
Try
For
Each item As ListItem In
ddlItems.Items
If
item.Text = "Item1" OrElse item.Text = "Item4"
Then
item.Attributes.Add("disabled", "disabled")
End
If
Next
Catch
ex As Exception
End Try
End Sub
No comments:
Post a Comment