Currently ASP.Net Dropdown list doesnot have a readonly property,
In some senario user may want a readonly version of Dropdownlist. In most of the cases we make it Disable.
The following code describes the Creation of a webcontol.
[ToolboxData("<{0}:DropDownReadOnly runat=server></{0}:DropDownReadOnly>")]
public class DropDownReadOnly : DropDownList
{
private bool _readOnly;
private TextBox tb = new TextBox();
private Image img = new Image();
Table t = new Table();
public bool ReadOnly
{
get
{
return _readOnly;
}
set
{
_readOnly = value;
}
}
public string DropDownImgUrl
{
set { img.ImageUrl = value; }
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
if (_readOnly)
{
if ((System.Web.UI.WebControls.ListControl)(this) != null && ((System.Web.UI.WebControls.ListControl)(this)).SelectedItem != null)
{
tb.Text = ((System.Web.UI.WebControls.ListControl)(this)).SelectedItem.Text;
tb.ReadOnly = true;
if (this.Width.Value > 24)
tb.Width = new Unit(this.Width.Value - 24);
tb.Style.Add("border", "solid 1px #7F9DB9");
tb.Style.Add("border-right", "0px none");
tb.Style.Add("height", "17px");
t.CellPadding = 0;
t.CellSpacing = 0;
TableCell tc = new TableCell();
tc.Controls.Add(tb);
TableCell tc2 = new TableCell();
tc2.Controls.Add(img);
TableRow tr = new TableRow();
tr.Cells.AddRange(new TableCell[] { tc, tc2 });
t.Rows.Add(tr);
t.RenderControl(writer);
}
}
else
base.Render(writer);
}
Here when readonly property is set we are rendering a textbox and an image, which is dropdown arrow.
Regards,
Debata