Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable a dropdownlist in ASP.NET?

How can I disable a DropDownList in ASP.NET?

Code:

<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?

like image 770
Yves Avatar asked Jul 02 '09 17:07

Yves


People also ask

How to disable a dropdown?

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.

How do I turn off syncfusion dropdown?

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.


3 Answers

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>
like image 84
Nikki9696 Avatar answered Sep 29 '22 21:09

Nikki9696


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");
like image 39
Anjisan Avatar answered Sep 29 '22 20:09

Anjisan


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">...
like image 25
drs9222 Avatar answered Sep 29 '22 20:09

drs9222