如何在 C# 中使下拉列表只读

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17464709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:32:57  来源:igfitidea点击:

How to make Dropdownlist readonly in C#

c#asp.net.netwebforms

提问by Rajan Vachhani

I am using

我在用

TextBox.ReadOnly = false;

for readonly.

只读。

How can i fix it on DropDownList?

我如何在 DropDownList 上修复它?

I use Enabled = falseproperties like...

我使用Enabled = false属性,例如...

TextBox.Enabled = false;
DropDownList.Enabled = false;

but, after that css class not call in this both control at run-time.

但是,在那之后 css 类不在运行时调用这两个控件。

Please give me any properties like "ReadOnly".

请给我任何属性,如“只读”。

采纳答案by Carlos Landeras

There is no readonly property for DropDownListin asp.net

DropDownListasp.net 中没有 readonly 属性

Try using:

尝试使用:

  <asp:DropDownList ID="DropDownList1" runat="server" Enabled="False">
    </asp:DropDownList>

Or change it at runtime:

或者在运行时更改它:

DropDownList1.Enabled=false;

and change it's css class as well.

并更改它的 css 类。

DropDownList1.CssClass = "class";

回答by Satinder singh

Another way:

其它的办法:

Code Behind:Just add attribute disabled

代码隐藏:只需添加属性disabled

 DropDownList1.Attributes.Add("disabled", "disabled");

Client Side:

客户端:

 $("#DropDownList1").attr("disabled","disabled");

JS FIDDLE

JS小提琴

回答by sagar

Use a panel as with Enabled="false"and put your control inside:

使用与Enabled="false"一样的面板并将您的控件放在里面:

<asp:Panel ID="pnlname" runat="server" Enabled="false">
    <asp:DropDownList ID="DropDownList1" runat="server">
    </asp:DropDownList>
</asp:Panel>

回答by Himalaya Garg

As disabled dropdownlist data cannot be read in postback. So as a workaround do not disable it, but first clear dropdownlist and then bind only already selected item.

由于无法在回发中读取禁用的下拉列表数据。因此,作为一种解决方法,不要禁用它,而是先清除下拉列表,然后仅绑定已选择的项目。

ListItem item = DropDownList.SelectedItem;
DropDownList.Items.Clear();
DropDownList.Items.Add(item);