简体   繁体   English

如何在ASP.NET中禁用下拉列表?

[英]How can I disable a dropdownlist in ASP.NET?

How can I disable a DropDownList in ASP.NET? 如何在ASP.NET中禁用DropDownList

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? 我究竟做错了什么?

There is no readonly property for a true dropdownlist for asp.net webforms. asp.net Webforms的真实下拉列表没有readonly属性。

        <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. 您没有问任何问题,也没有解释什么不起作用,也没有说您使用的是Webforms还是Winforms,或者是否在背后的代码或aspx页面中。

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. 哦,如果值以任何方式(例如查询更新)传递回去,请确保将绑定而不是Eval用于编辑模板。 Sometimes the platform is doing it behind the scenes, so generally speaking, just use Bind. 有时平台会在后台进行操作,因此通常来说,只需使用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), 在您的.cs中尝试此操作(假设将asp:DropDownList作为ddlCategory的ID),

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): 假设禁用表示“使它不能使用户从列表中选择一个项目”,那么以下示例均产生相同的html(对我有用):

Method 1: 方法1:

<asp:DropDownList ID="dd" Enabled="false" runat="server">...

Method 2: 方法2:

<asp:DropDownList ID="dd" disabled="disabled" runat="server">...

Method 3(aspx): 方法3(aspx):

<asp:DropDownList ID="dd" runat="server">...

Method 3(aspx.cs): 方法3(aspx.cs):

dd.Enabled = false;

Method 4(aspx): 方法4(aspx):

<asp:DropDownList ID="dd" runat="server">...

Method 4(aspx.cs): 方法4(aspx.cs):

dd.Attributes.Add("disabled", "disabled")

Resulting HTML: 产生的HTML:

<select name="dd" id="dd" disabled="disabled">...
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 <= _MAXMINUTES;

    return true;
}


var badRmuHours = function() {
    alert("You are only allowed to make up 4 hours total for a day off");
    var ddlTotal = $("#ctl00_ContentPlaceHolder1_DetailsView1_ddlEditTotalHoursEffect")[0];
    ddlTotal.selectedIndex = 0;
}

This is what I do: 这是我的工作:

ddlEditTotalHoursEffect.Enabled=false; ddlEditTotalHoursEffect.Enabled = false;

I can be done from your code-behind. 我可以从您的后台代码中完成。

Oh, wait, you have enabled equal to false. 哦,等等,您启用了等于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? 您要启用数据输入框但不启用下拉菜单吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM