How can I disable a DropDownList
in ASP.NET?
<asp:TemplateField HeaderText="Effective Total Hours">
<ItemTemplate>
<%# Eval("TotalHoursEffect")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlEditTotalHoursEffect" AppendDataBoundItems="true"
DataSourceID="dsTHMsql" DataValueField="Minutes" Enabled="false"
ReadOnly="true" DataTextField="Display"
SelectedValue='<%# Eval("TotalHoursEffect") %>' runat="server">
<asp:ListItem Selected="True" Value="">(Choose Minutes)</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
This is not working for me. What am I doing wrong?
We use <select> and <option> elements to create a drop-down list and use disabled attribute in <select> element to disable the drop-down list element. A disabled drop-down list is un-clickable and unusable. It is a boolean attribute.
Thank you for contacting Syncfusion support. Yes, it is possible to change the enabled / disabled status of a DropDownList through a button. All you need to do is use the enabled property in the control and set it as true to enable and false to disable it.
There is no readonly property for a true dropdownlist for asp.net webforms.
<asp:DropDownList ID="DropDownList1" runat="server" Enabled="False">
</asp:DropDownList>
If that isn't what you're doing, you will need to be a lot more specific. You didn't ask a question, you didn't explain WHAT isn't working, or say if you're using webforms or winforms, or if it's in the code behind or the aspx page.
ETA: remove the readonly property from the dropdownlist, it isn't valid. AFTER you test that part and see if it fixed it, if it still isn't doing what you want, please tell us what it isn't doing. Is it not disabling? Is it not databinding? What's going on with it?
Oh, and make sure you use Bind, not Eval, for edit templates if the value is being passed back in any way such as to a query update. Sometimes the platform is doing it behind the scenes, so generally speaking, just use Bind.
One more edit: this works for me in the most basic sense in that it binds and the dropdown is not selectable.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"
DataSourceID="sqldsProducts" AutoGenerateEditButton="True">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" SortExpression="ProductID" />
<asp:TemplateField HeaderText="CategoryID" InsertVisible="False" SortExpression="CategoryID">
<EditItemTemplate>
<asp:DropDownList Enabled="false" ID="ddlCategory" runat="server" DataSourceID="sqldsCategories"
DataTextField="CategoryName" DataValueField="CategoryID" SelectedValue='<%# Bind("CategoryID") %>' AppendDataBoundItems="True">
<asp:ListItem Selected="True" Value="" Text="-- choose one --" />
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblCategory" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
</Columns>
</asp:GridView>
You can disable a dropdownlist but you'll need to do it from the code behind.
Try this in your .cs (assuming your asp:DropDownList as an id of ddlCategory),
ddlCategory.Attributes.Add("disabled", "disabled");
Assuming that by disable you mean "make it so that the user can't select an item from the list" then the following examples all result in the same html (which works for me):
Method 1:
<asp:DropDownList ID="dd" Enabled="false" runat="server">...
Method 2:
<asp:DropDownList ID="dd" disabled="disabled" runat="server">...
Method 3(aspx):
<asp:DropDownList ID="dd" runat="server">...
Method 3(aspx.cs):
dd.Enabled = false;
Method 4(aspx):
<asp:DropDownList ID="dd" runat="server">...
Method 4(aspx.cs):
dd.Attributes.Add("disabled", "disabled")
Resulting HTML:
<select name="dd" id="dd" disabled="disabled">...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With