Wednesday, August 11, 2004

Editable dropdown list box


Editable dropdown list box









An
"editable drop-down list" or a combo-box per se is not supported by any
browser, at least not by IE and Netscape. So having this type of
control is not possible. However, what could be done is to use a
textbox along with a drop-down list and add the contents of the textbox
to the drop-down when the TextChanged event of the textbox fires.
Here's how:
Technique 1:
Using client-side code (note that asp.net controls can also be used here)
<script>
function addToCombo(val, obj)
{
if(val.length > 0)
{
obj.options.add(new Option(val));
}
}
</script>
<input type=text name="txtCombo" onchange="addToCombo(this.value, this.form.selCombo)"><br>
<select name="selCombo"></select>
Technique 2:
Using code-behind (C#)
<asp:TextBox id="txtCombo" runat="Server"/><br>
<asp:dropdownlist id="selCombo" runat="Server"/>
code behind:
private void txtCombo_TextChanged(...)
{
if(txtCombo.Text.Trim().Length > 0)
{
selCombo.Items.Add(new ListItem(txtCombo.Text));
}
}

1 comment:

Mitesh V. Mehta said...

Hi,
you need to decide whether to use Free Custome controls (3rd party) or writing your own code(creating new user control like editable drop down).

Try this Free 3rd party controls http://www.progstudios.com/

or i can give you an idea how to create a custom control like that in your own.please let me know

Regards,
Mitesh Mehta