Link to home
Start Free TrialLog in
Avatar of bailey1200
bailey1200Flag for United Kingdom of Great Britain and Northern Ireland

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Ravi Vaddadi
Ravi Vaddadi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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

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

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

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

Avatar of sybe
sybe

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>

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>