make dropdown list read only

How can I make a dropdownlist read only pelase I have tried

      ddlRole.Enabled = false;  and
    ddlRole.ReadOnly = true;

Bit nether works! any ideas please
bailey1200Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SriVaddadiCommented:
There is no ReadOnly Property though.  Setting Enabled to false should do the trick. If it is not working then it might be that you are overriding it some where.

Please review your code again.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
scottlafoyCommented:
http://www.codeproject.com/KB/combobox/readonlycombobox.aspx

Above is a project that will create a custom control of a dropdown box with a read only property. There are others on the web if you do not like this one.

Read only is very similar to the enabled = false, the only difference is read only has blacker text so it is a little easier to read.

and if by chance you mean change the drop down box so users cannot type into it and must select from a prebuilt list then in the properties change  the drop down style to drop down list.
0
leakim971PluritechnicianCommented:
You may use javascript to save and restore the currentIndex of the dropdown :


<script language="javascript" type="text/javascript">
        var oldIndex;
        function saveCurrentIndex() {
            oldIndex = document.getElementById("<%= DropDownList1.ClientID %>").selectedIndex;
        }
        function restoreIndex() {
            document.getElementById("<%= DropDownList1.ClientID %>").selectedIndex = oldIndex;
        }
    </script>

Open in new window

0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

leakim971PluritechnicianCommented:
Code behind, we set onfocus and onchange attribut of the dropdown :


protected void Page_Load(object sender, EventArgs e)
        {
            DropDownList1.Attributes.Add("onfocus", "saveCurrentIndex()");
            DropDownList1.Attributes.Add("onchange", "restoreIndex()");
        }

Open in new window

0
leakim971PluritechnicianCommented:
test page :


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication14._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <script language="javascript" type="text/javascript">
        var oldIndex;
        function saveCurrentIndex() {
            oldIndex = document.getElementById("<%= DropDownList1.ClientID %>").selectedIndex;
        }
        function restoreIndex() {
            document.getElementById("<%= DropDownList1.ClientID %>").selectedIndex = oldIndex;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem Text="text1" Value="value1"></asp:ListItem>
        <asp:ListItem Text="text2" Value="value2" Selected></asp:ListItem>
        <asp:ListItem Text="text3" Value="value3"></asp:ListItem>
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

Open in new window

0
leakim971PluritechnicianCommented:
code behind of the test page :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication14
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DropDownList1.Attributes.Add("onfocus", "saveCurrentIndex()");
            DropDownList1.Attributes.Add("onchange", "restoreIndex()");
        }
    }
}

Open in new window

0
sybeCommented:
What is the point of a dropdownlist if it is read-only?

In HTML

<select disabled="disabled">
<option>1</option>
<option>2</option>
<option selected="selected">3</option>
<option>4</option>
</select>

will give you a disabled dropdown. But it seems pretty useless to me, it does not show the non-selected values. Although you could make show the other values with manipulating "size":

<select disabled="disabled" size="4">
<option>1</option>
<option>2</option>
<option selected="selected">3</option>
<option>4</option>
</select>

0
mmtcaptainCommented:
Set Enabled="false" in page..
like this
 <asp:DropDownList ID="DropDownList1" runat="server" Enabled="false">
        <asp:ListItem Text="text1" Value="value1"></asp:ListItem>
        <asp:ListItem Text="text2" Value="value2" Selected></asp:ListItem>
        <asp:ListItem Text="text3" Value="value3"></asp:ListItem>
        </asp:DropDownList>

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.