Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
4.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

Hi,

Set AutoPostback property as True of the DropDownList and write code to fetch phonenumber in DropDownList1_SelectedIndexChanged event.
 
Share this answer
 
Comments
MAGuru 21-Apr-14 3:25am    
But Iam using 3tier architecture
Quote:
But Iam using 3tier architecture
Then write a method in each tier and try to pass parameter from one tier to other and fetch the data.
 
Share this answer
 
Comments
MAGuru 21-Apr-14 5:56am    
Please give sample code
No, I can't and we don't provide samples here. Please try to do it yourself. If you face any particular issue while implementing, then come back and add another question.

This is very easy. Just a matter of adding methods in each tier and calling it.
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
 
v2
Comments
MAGuru 22-Apr-14 0:21am    
But Iam using 3tier architecture
[no name] 22-Apr-14 5:13am    
It's Not a Problem. Put GetData Method into your Business Layer and make it static.
Then use it like,
TextBox1.Text = ClassName.GetData( selectedValue);
MAGuru 24-Apr-14 0:20am    
Is it Possible to use json for this task...??

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