Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Dropdownlist readonly in C#

I am using

TextBox.ReadOnly = false;

for readonly.

How can i fix it on DropDownList?

I use Enabled = false properties like...

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

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

Please give me any properties like "ReadOnly".

like image 414
Rajan Vachhani Avatar asked Jul 04 '13 07:07

Rajan Vachhani


People also ask

How do I make a drop down list in readonly?

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.

How do I make a dropdown readonly in VB net?

When you disable the dropdownlist by setting Disabled=true; or Enabled=false (whatever the case is), you can also change the Font properties to make it easier to read. You can set other properties such as Border, BorderStyle, etc, etc.

How enable and disable dropdown in MVC?

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.

What is selected value in DropDownList?

Cause SelectedValue will give you the value stored for current selected item in your dropdown and SelectedItem. Value will be Value of the currently selected item.


4 Answers

There is no readonly property for DropDownList in asp.net

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.

DropDownList1.CssClass = "class";
like image 56
Carlos Landeras Avatar answered Oct 16 '22 18:10

Carlos Landeras


Another way:

Code Behind: Just add attribute disabled

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

Client Side:

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

JS FIDDLE

like image 26
Satinder singh Avatar answered Oct 16 '22 18:10

Satinder singh


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

<asp:Panel ID="pnlname" runat="server" Enabled="false">
    <asp:DropDownList ID="DropDownList1" runat="server">
    </asp:DropDownList>
</asp:Panel>
like image 21
sagar Avatar answered Oct 16 '22 20:10

sagar


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);
like image 20
Himalaya Garg Avatar answered Oct 16 '22 19:10

Himalaya Garg