本文介绍了如何从Dropdownlist(Asp.net C#)中选择值时自动填充文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从Dropdownlist(Asp.net C#)中选择值时自动填充文本框

(Iam遵循分层结构)





简单地说,我从下拉列表中选择一个值...它

ddlVndrName.DataSource = objVndrList;

ddlVndrrName.DataValueField = VndrID;

ddlVndrName.DataTextField =VndrName;

ddlVndrName.DataBind();

如何用PhoneNumber填充文本框对应到db

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

推荐答案

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:DropDownList ID="ddlVndrName" runat="server" AutoPostBack="True" onselectedindexchanged="ddlVndrName_SelectedIndexChanged"







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);

}







如果您需要创建基于的动态文本框选定价值



[]


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





然后代码支持





Then code behind

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);
          }

}


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();
          }

      }


这篇关于如何从Dropdownlist(Asp.net C#)中选择值时自动填充文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 08:12