• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1248
  • Last Modified:

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
0
bailey1200
Asked:
bailey1200
1 Solution
 
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
 
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now