abhid89
abhid89

Reputation: 133

Populate multiple textboxes on dropdownlist value in asp.net c#

I have following Employee table,

  name      | experience |    location
  emp1           3 yrs             aaa
  emp2           2 yrs             bbb
  emp3           4 yrs             ccc
  emp4           1 yr              ddd

I have dropdown list which I have bound to name column of the table. I have 2 textboxes: txtExp and txtLoc. I want to populate textboxes to experience and location columns of the table on the basis of selected value from dropdown list, either by index changed of DDL or click event of a button. i want to do it form a code behind file in asp.net c#

Upvotes: 1

Views: 3252

Answers (2)

Ram Singh
Ram Singh

Reputation: 6938

if you want to show both the things then you have to concatenate the values where you are fetching the records for binding the dropdownlist, i am pasting the code, which will tell you how you can get the experience by name(selected value in dropdown)

first step is :

save the datatable from which you are binding the dropdown into ViewState

then on dropdown's selectedindexchanged event use the following code:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ViewState["dt"] != null)
    {
        DataTable dt = (DataTable)ViewState["dt"];
        if (dt.Rows.Count > 0)
        {
            string sqlFilter = "name = '" + DropDownList1.SelectedItem.Text + "'";
            DataRow[] data = dt.Select(sqlFilter);
            if (data.Length > 0)
            {
                TextBox1.Text = data["Exp"].ToString();

            }
        }
    }
}

or if you want to pass it to db to get the experience and location then do it in the following way:

clsArtBook obj = new clsArtBook();
    DataTable dt = new DataTable();
    dt = obj.getDetailsbyName(DropDownList1.SelectedItem.Text);
    if (dt.Rows.Count > 0) {
        TextBox1.Text = dt.Rows[0]["expeince"].ToString();
        TextBox1.Text = dt.Rows[0]["location"].ToString();
    }

create a stored proc which will get the details by name like :

create proc proc_GetDetailsByName
(
@name varchar(100)
)
as
if exists(select 1 from tblemployee where name=@name)
begin
 select location,experince from tblemployee where name=@name
end

it will resolve your problem

Upvotes: 0

Tanner
Tanner

Reputation: 22753

Step 1: HTML - Have a drop down list that performs a post-back:

<asp:DropDownList ID="ddlName" runat="server" AutoPostBack="True" 
                  OnSelectedIndexChanged="ddlName_SelectedIndexChanged">
    <asp:ListItem Value="1">Emp 1</asp:ListItem>
    <asp:ListItem Value="2">Emp 2</asp:ListItem>
    <asp:ListItem Value="3">Emp 3</asp:ListItem>
</asp:DropDownList>

Step 2: C# - Execute the code that updates the controls:

protected void ddlName_SelectedIndexChanged(object sender, EventArgs e)
{
    // Make a call to database/method to retrieve the Employee record based on 
    // Name/ID, which is bound to the drop down. Assumption that you will
    // use an object called Employee with name & location properties)
    Employee emp = GetEmployeeByID(ddlName.SelectedValue);

    txtExp.Text = emp.Name;
    txtLoc.Text = emp.Location;
}

Upvotes: 1

Related Questions