Click here to Skip to main content
Licence CPOL
First Posted 11 Nov 2008
Views 25,959
Downloads 1,104
Bookmarked 13 times

Editable DropDownlist

By | 11 Nov 2008 | Article
How to create and use editable dropdownlist in asp.net

Introduction

In ASP.Net Dropdownlist control cannot be editable by default. We can easily overcome this problem. This article is used to overcome this issue.

Using the code

Create one blank asp.net web page(Default.aspx) and include one textbox and dropdownlist control. see the following code.

<asp:TextBox ID="txtDisplay" runat="server"></asp:TextBox>

<asp:DropDownList ID="ddSelect" runat="server">

    <asp:ListItem Value="test1" >test1</asp:ListItem>

    <asp:ListItem Value="test2">test2</asp:ListItem>

</asp:DropDownList>
        

After inserted this controls into web page we need to set the following style into textbox control.

style="position:absolute"  

The position property places an element in a static, relative, absolute or fixed position. There are four types of position like static, relative, absolute and fixed.

Value Description
static Default. An element with position: static always has the position the normal flow of the page gives it (a static element ignores any top, bottom, left, or right declarations)
relative An element with position: relative moves an element relative to its normal position, so "left:20" adds 20 pixels to the element's LEFT position
absolute An element with position: absolute is positioned at the specified coordinates relative to its containing block. The element's position is specified with the "left", "top", "right", and "bottom" properties
fixed An element with position: fixed is positioned at the specified coordinates relative to the browser window. The element's position is specified with the "left", "top", "right", and "bottom" properties. The element remains at that position regardless of scrolling. Works in IE7 (strict mode)

<asp:TextBox style="position:absolute" ID="txtDisplay" runat="server"></asp:TextBox>  


After adding the position style we need to set the equal width for both textbox and dropdownlist control.

<asp:TextBox style="width:120px;position:absolute" ID="txtDisplay" runat="server"></asp:TextBox>

<asp:DropDownList ID="ddSelect" style="width:140px" runat="server">

<asp:ListItem Value="test1" >test1</asp:ListItem>

<asp:ListItem Value="test2">test2</asp:ListItem>

</asp:DropDownList>

After adding the Styles we need to create javascript function. This function is used to display the selected dropdwon 
text into textbox control.

<script language="javascript" type="text/javascript">

function DisplayText()
{
    document.getElementById("txtDisplay").value = document.getElementById("ddSelect").value;
    document.getElementById("txtDisplay").focus();
}
</script> 
After adding the javascript function. use this function for Dropdown change event fires. 
Add the following code into Default.aspx.cs file.

protected void Page_Load(object sender, EventArgs e)
{
	ddSelect.Attributes.Add("onChange", "DisplayText();");
}

Conclusion:

So we can easily create a editable dropdownlist.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

muralikannan_t



India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalyour article is not working in IE6 ? [modified] PinmemberMember 453251919:44 14 Sep '10  
GeneralMy vote of 1 Pinmembertsoh_tan23:18 10 May '10  
QuestionWhat the heck? PinmemberJags12124:02 7 Feb '10  
GeneralIE Pinmemberspandal22:20 15 Mar '09  
Generalthe user control is missing in the code Pinmemberasp991:37 23 Dec '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 12 Nov 2008
Article Copyright 2008 by muralikannan_t
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid