Click here to Skip to main content
15,917,618 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How to Auto populate a text box when selecting a value from Dropdownlist (Asp.net C#)
(Iam following layered structure)


simply say that ,iam selecting a value from dropdownlist...its
ddlVndrName.DataSource = objVndrList;
ddlVndrrName.DataValueField = "VndrID";
ddlVndrName.DataTextField = "VndrName";
ddlVndrName.DataBind();
How to populate a textbox with PhoneNumber corresponds to the above VndrID in db
Posted
Updated 28-May-16 23:27pm
v3
Comments
Bh@gyesh 21-Apr-14 0:42am    
Hi,
Give details in questions. does it means, you select '1' then one textbox, '2' then 2 textboxes etc..?
MAGuru 21-Apr-14 0:50am    
hi,
simply say that ,iam selecting a value from dropdownlist...its
ddlVndrName.DataSource = objVndrList;
ddlVndrrName.DataValueField = "VndrID";
ddlVndrName.DataTextField = "VndrName";
ddlVndrName.DataBind();
How to populate a textbox with another value corresponds to the above VndrID

your question is not clear. Value of selected item of dropdown can be taken from ddlVndrName.SelectedItem.Value

if you need to show the selected value of ddl in textbox

C#
TextBox1.Text =  Convert.Tostring(ddlVndrName.SelectedItem.Value);


if you need to process something based on the selected value and display results in a textbox:

ASP.NET
<asp:DropDownList ID="ddlVndrName" runat="server" AutoPostBack="True" onselectedindexchanged="ddlVndrName_SelectedIndexChanged"


C#
protected void ddlVndrName_SelectedIndexChanged(object sender, EventArgs e)
{
  string selectedValue =  Convert.Tostring(ddlVndrName.SelectedItem.Value)
  // process or call other method with the value above 
  TextBox1.Text = ProcessText( selectedValue); 

}



if you need to create dynamic text boxes based on selected value

http://forums.asp.net/post/4197936.aspx[^]
 
Share this answer
 
v3
Comments
Member 12590563 22-Jun-16 4:00am    
does convert have tostring method
If you want to populate from database and have a data filed like PhoneNumber then. simple

XML
<asp:DropDownList ID="ddlVndrName" runat="server" AutoPostBack="True" onselectedindexchanged="ddlVndrName_SelectedIndexChanged/>"


Then code behind

C#
protected void ddlVndrName_SelectedIndexChanged(object sender, EventArgs e)
{
  string selectedValue = ddlVndrName.SelectedValue.Tostring();
  

  TextBox1.Text = GetData( selectedValue);

}

public string GetData(string id)
{
var ConnectionString= "your connection string";
var query = string.formet( SELECT PhoneNumber FROM tableName WHERE id ={0},id};
 using (var conn = new SqlConnection(ConnectionString))
          {
              try
              {
                  if (conn.State == ConnectionState.Closed)
                  {
                      conn.Open();
                  }

                  string returnValue = string.Empty;
                  var selectCommand = new SqlCommand(query, conn);

                      var myReader = selectCommand.ExecuteReader();
                      while (myReader.Read())
                      {
                          returnValue = myReader.GetString(0);
                      }
                  
                  if (conn.State == ConnectionState.Open)
                  {
                      conn.Close();
                  }
                  return returnValue;
              }
		  }
          catch (Exception e)
          {
             throw new Exception(e.Message);
          }

}
 
Share this answer
 
C#
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
      {
          try
          {
              SQLconn.Open();
              string req = "select distinct Distance from Ligne where AeroportDarrive=" + DropDownList3.Text + "";
              SqlCommand com = new SqlCommand(req, SQLconn);
              SqlDataReader dr = com.ExecuteReader();
              if (dr.Read())
              {
                  TextBox4.Text = dr.GetValue(1).ToString();

              }

          }
          catch(Exception ex)
          {
              HttpContext.Current.Response.Write("<script>alert('Probleme de la base de donnee : " + ex.Message + "')</script>");
          }
          finally
          {
              SQLconn.Close();
          }

      }
 
Share this answer
 
Comments
CHill60 29-May-16 6:00am    
If you are going to answer old posts make sure that you are bringing something new to the conversation - this is no different to the solution posted 2 years ago

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900