ASP.NET: Fill Textbox field upon dropdownlist selection by user

1.2k Views Asked by At

i have four dropdown lists, upon selecting each dropdown list in asp.net page textbox field should fill the value, which user selected in dropdownlist page, there is no SQL business here, for ex, ddl1 contains...Tiger Lion Deer ddl2 contains...Siberia RSA Namibia ddl3 contains...Carnivore Carnivore Herbivore

if users select tiger from ddl1, siberia from ddl2 and cornivore from ddl3, textbox field should update as, Tiger Siberia Carnivore i have used StringBuilder with append class below like this,

        public List<StringBuilder> GetAnimalDetails()
    {
        StringBuilder typo = new StringBuilder();
        typo.Append(DropDownListBranch.SelectedValue);
        typo.Append(" ");
        typo.Append(DropDownListMilestone.SelectedValue);
        typo.Append(" ");
        typo.Append(DropDownListType.SelectedValue);
        typo.Append(" - ");
        typo.Append(DropDownListVersion.SelectedValue);
        return typo.ToString().ToList();

    }
1

There are 1 best solutions below

1
On BEST ANSWER

Considering the simple textbox filling with dropdown values, you could try something like,

    public string GetAnimalDetails()
    {
        string typo = string.Empty;

        typo = DropDownListBranch.SelectedValue;
        if (!string.IsNullOrEmpty(typo) && !string.IsNullOrEmpty(DropDownListMilestone.SelectedValue))
        {
            typo += " ";
        }
        typo = typo + DropDownListMilestone.SelectedValue;
        if (!string.IsNullOrEmpty(typo) && !string.IsNullOrEmpty(DropDownListType.SelectedValue))
        {
            typo += " ";
        }
        typo = typo + DropDownListType.SelectedValue;
        if (!string.IsNullOrEmpty(typo) && !string.IsNullOrEmpty(DropDownListVersion.SelectedValue))
        {
            typo += " - ";
        }
        typo += DropDownListVersion.SelectedValue;

        return typo;
    }


    protected void DropDownListBranch_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtAnimalDetails.Text = GetAnimalDetails();
    }

    protected void DropDownListMilestone_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtAnimalDetails.Text = GetAnimalDetails();
    }

    protected void DropDownListType_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtAnimalDetails.Text = GetAnimalDetails();
    }

    protected void DropDownListVersion_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtAnimalDetails.Text = GetAnimalDetails();
    }

Hope this helps...