views:

3283

answers:

5

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?

+9  A: 

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>
Nikki9696
A: 
var rmu_minutes = 0;

    var initRmuChangeHandlers = function() {

        $(".container select").change(function(e) {
            var x = 0;
            var dropDowns = $(".container select");
            dropDowns.each(function() {
                var ddl = this;
                x += parseInt(ddl.value);
                if (!dayOffRmuValidator(x)) {
                    alert("4 Hours MAX");
                    ddl.selectedIndex = 0;
                }
            });
            rmu_minutes = x;
            updateTotalRmu();

        });
    }

    var initClearRmuDropDowns = function() {

        var dropDowns = $(".container select");
        dropDowns.each(function() {
            var ddl = this;
            ddl.selectedIndex = 0;
        });
    }

    var dayOffRmuValidator = function(rmu_minutes) {
        var _MAXMINUTES = 240;

        var ddl = $get('ctl00_ContentPlaceHolder1_DetailsView1_ddlEditType');
        var ddlVal = parseInt(ddl.options[ddl.selectedIndex].value);

        if (ddlVal == RequestTypes["Day Off"])
            return rmu_minutes 
Kombucha
A: 

This is what I do:

ddlEditTotalHoursEffect.Enabled=false;

I can be done from your code-behind.

Oh, wait, you have enabled equal to false. What is it you are looking to do, then? Do you want to make the data entry box enabled but not the pull-down?

Mark Brittingham
+4  A: 

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");
Anjisan
+2  A: 

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">...
drs9222