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

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

问题描述

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

(Iam遵循分层结构)





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

ddlVndrName.DataSource = objVndrList;

ddlVndrrName.DataValueField = VndrID;

ddlVndrName.DataTextField =VndrName;

ddlVndrName.DataBind();

如何用PhoneNumber填充文本框对应到db中的上述VndrID

解决方案





将AutoPostback属性设置为DropDownList的True并且编写代码以在DropDownList1_SelectedIndexChanged事件中获取phonenumber。


Quote:

但Iam使用3tier架构

然后在每个层中编写一个方法,并尝试将参数从一个层传递到另一个层并获取数据。


如果要弹出从数据库中获取并拥有类似PhoneNumber的数据。简单





 <  < span class =code-leadattribute> asp:DropDownList     ID   =  ddlVndrName    runat   =  server    AutoPostBack   =  True < span class =code-attribute>   onselectedindexchanged   =  ddlVndrName_SelectedIndexChanged /> 



然后代码





 受保护  void  ddlV ndrName_SelectedIndexChanged( object  sender,EventArgs e)
{
string selectedValue = ddlVndrName。 SelectedValue.Tostring();


TextBox1.Text = GetData(selectedValue);

}

public string GetData(< span class =code-keyword> string id)
{
var ConnectionString = 您的连接字符串;
var query = string .formet(SELECT PhoneNumber FROM tableName WHERE id = { 0 },id};
使用 var conn = new SqlConnection(ConnectionString))
{
try
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}

string returnValue = string .Empty;
< span class =code-keyword> 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 (例外e)
{
throw new 异常(e.Message);
}

}


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

解决方案

Hi,

Set AutoPostback property as True of the DropDownList and write code to fetch phonenumber in DropDownList1_SelectedIndexChanged event.


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.


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

}


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

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