bailey1200
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
ddlRole.Enabled = false; and
ddlRole.ReadOnly = true;
Bit nether works! any ideas please
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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>
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()");
}
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>
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()");
}
}
}
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</opt ion>
<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</opt ion>
<option>4</option>
</select>
In HTML
<select disabled="disabled">
<option>1</option>
<option>2</option>
<option selected="selected">3</opt
<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</opt
<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:ListI tem>
<asp:ListItem Text="text2" Value="value2" Selected></asp:ListItem>
<asp:ListItem Text="text3" Value="value3"></asp:ListI tem>
</asp:DropDownList>
like this
<asp:DropDownList ID="DropDownList1" runat="server" Enabled="false">
<asp:ListItem Text="text1" Value="value1"></asp:ListI
<asp:ListItem Text="text2" Value="value2" Selected></asp:ListItem>
<asp:ListItem Text="text3" Value="value3"></asp:ListI
</asp:DropDownList>
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.