如何从Dropdownlist(Asp.net C#)中选择值时自动填充文本框 [英] How to Auto populate text boxes when selecting value from Dropdownlist (Asp.net C#)

查看:206
本文介绍了如何从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

推荐答案

中的上述VndrID你的问题不明确。所选项目下拉列表的值可以从 ddlVndrName.SelectedItem.Value



获取,如果您需要显示文本框中选择的ddl值



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

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

}







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



http://forums.asp.net/发布/ 4197936.aspx [ ^ ]


如果要从数据库填充并拥有类似PhoneNumber的数据,那么。简单



If you want to populate from database and have a data filed like PhoneNumber then. simple

<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#)中选择值时自动填充文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆