asp.net 可选可输入的dropdownlist

来源:互联网 发布:徽商银行网络培训平台 编辑:程序博客网 时间:2024/06/06 12:19

    我们都知道,dropdownlist控件是无法直接输入文本的,只能选择下拉菜单中的选项。为了方便使用,我们可以用多种方法来实现既可选择又可输入的dropdownlist的效果。主要思路有.通过js代码来实现,或是在后台代码中实现。但是今天主要介绍通过html中的select和input控件相结合的的来实现。

<html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">    <title>可输入的dropdownlist</title></head><body><table>    <tr>        <td  style="font-size: 12px;">              既可以输入新的信息,又可以从下拉框中选择:        </td>        <td>            <select name="myselect"  id="myselect" runat="server" style=" width:200px; position:absolute; clip:rect(auto auto auto 181px); " onchange="document.getElementById('txtPlace').value=this.value" >                <option value="">请选择</option>                <option value="大一"> 大一 </option>                <option value="大二"> 大二 </option>                    <option value="大三"> 大三 </option>                <option value="大四"> 大四 </option>            </select>            <input name="txtPlace" id="txtPlace" style=" width:200px;"  type="text"  />           </td>    </tr></table></body></html>
     我们还可以在css文件中设置他们的显示效果,比如:

input, select {    font-size:16px;                 //设置字体大小    border:1px solid #CFCFCE;       //设置边框效果}
     如果下拉框里的内容需要从数据库里获取的话,可以为select设置数据源并绑定:

List<suppliesinfo> unitlist = new CommonFunction().BCheckSupplies();myselect.DataSource = unitlist;myselect.DataTextField = "unit";myselect.DataBind();
     下面就是最终的实现效果:





0 0