使用文本框asp.net填充下拉列表

时间:2014-02-14 05:16:21

标签: asp.net

enter image description here enter image description here当用户在文本框中输入一些文本时,如何填充下拉列表,我尝试在文本框控件中使用文本更改事件控件可以正常工作,但需要点击两次下拉下来,当我首先点击它加载数据然后淹没被关闭,然后我再次点击它显示 我使用asp文本框和asp下拉

<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true">  
        </asp:TextBox>

Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    DropDownList1.Items.Clear()
    DropDownList1.Items.Add(TextBox1.Text)
    Dim icount As Integer
    icount = DropDownList1.Items.Count
    DropDownList1.Items.Add("Select")
    DropDownList1.SelectedIndex = icount
End Sub

enter image description here

2 个答案:

答案 0 :(得分:1)

试试这个:

<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
    </asp:DropDownList>

然后:

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        int count = Convert.ToInt32(this.TextBox1.Text);
        for (int i = 1; i <= count; i++)
        {
            this.DropDownList1.Items.Add(i.ToString());
        }

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Write("Selected Value Is :" + this.DropDownList1.SelectedValue);
    }

如果您遇到困难,请告诉我。

答案 1 :(得分:0)

示例:txtCity.text =“迈索尔,班加罗尔,德里”

protected void txtCity_TextChanged(object sender, EventArgs e)
{
    string[] _cities=this.txtCity.text.split(',');
    int _maxCities = _cities.count();
    for (int _item = 1; _item <= _maxCities; _item++)
    {
        this.ddlCity.Items.Add(_item.ToString());
    }

}