ASP.NET - 选择dropDownList项时自动完成TextBox

时间:2016-04-12 06:47:35

标签: c# asp.net web-services

大家。我有一个网络服务,公司里的所有车辆都有数据: CarNumber和CarBrand。

我想,我从DropDownList中选择CarNumber,在textBox中自动完成CarBrand。

这是我的Web控件声明;

        ddCarNumber = new DropDownList();
        ddCarNumber.Items.Add("-- Choose Car Number --");
        ddCarNumber.SelectedIndexChanged += new EventHandler(ddCarNumber_SelectedIndexChanged);
        GetCars();
        this.Controls.Add(ddCarNumber);

        lblCarBrand.Text = "Car Brand";
        txtCarBrand = new TextBox();
        txtCarBrand.MaxLength = 255;
        this.Controls.Add(txtCarBrand);

使用此方法,我从Web服务获取CarNumbers:

private void GetCars()
        {
            Service1SoapClient client = new Service1SoapClient();
            UserDetails details = new UserDetails();

            details.userName = "Weber";
            details.password = "!Q2w#4r";

            DataTable dt = client.GetCars(details);

            foreach (DataRow row in dt.Rows)
            {
                ddCarNumber.Items.Add(row[0].ToString());
            }
        }
  

我想要从DropDown选择CarNumber,使用CarBrand选择TextBox autoComplete。

2 个答案:

答案 0 :(得分:0)

这是:我的ddCarNumber_SelectedIndexChanged方法,这是有效的,但是问题在于事件,而不是刷新页面...当我推F5是好的...

    void ddCarNumber_SelectedIndexChanged(object sender, EventArgs e)
    {
        Service1SoapClient client = new Service1SoapClient();
        UserDetails details = new UserDetails();

        details.userName = "Weber";
        details.password = "!Q2w#4r";

        DataTable dt = client.GetCars(details);

        foreach (DataRow row in dt.Rows)
        {
            if (ddCarNumber.SelectedValue == Convert.ToString(row[0]))
            {
                txtCarBrand.Text = row[1].ToString();
            }
        }
    }

答案 1 :(得分:0)

在Dropdown selectedIndex Change Event中 ddCarNumber_SelectedIndexChanged

写下面的代码

this.Controls.findcontrol("txtCarBrand").Text = ddCarNumber.SelectedItem.Text/Value //Based on your Data DataTextField/DataValueField

希望这有帮助。