如何在文本框中显示数据库值 [英] how to show the database values in textbox
问题描述
我有一个下拉列表和一个文本框。
我的表中有两个字段viz accno和cust_name。我在dropdownlist中显示acc no。现在我必须从dropdownlist中选择值作为accno并在textbox.how中显示所选acc no的cust_name以显示?
Plz帮助我
I have one dropdownlist and one textbox.
I have two fields viz accno and cust_name in my table. I show the acc no in dropdownlist.Now i have to select value from dropdownlist as accno and show the cust_name of the selected acc no in the textbox.how to show?
Plz help me
推荐答案
只需将dropdownlst与具有两列的数据源绑定。
Just bind the dropdownlst with the datasource having the both columns.
dropdownlistName.DataTextField = "accno";
dropdownlistName.DataValueField = "cust_name";
dropdownlistName.DataSource = yourDataSource;
dropdownlistName.DataBind();
使用下拉列表的selectedindex更改方法在文本框中获取cust_name。
别忘了将dropdownlist的autopostback属性设置为true。
Use dropdownlist''s selectedindex changed method to get cust_name in textbox.
Don''t forget to set autopostback property of dropdownlist to true.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Dropdownlist_click_data_in_textbox._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList><br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</div>
<asp:Button ID="btn_search" runat="server" onclick="btn_search_Click"
Text="Search" />
</form>
</body>
</html>
使用系统;
使用System.Collections;
使用System.Configuration;
使用System.Data;
使用System.Linq;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.UI.HtmlControls;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Xml.Linq;
使用System.Data.SqlClient;
命名空间Dropdownlist_click_data_in_textbox
{
public partial class _Default:System.Web.UI .Page
{
string strConnString = ConfigurationManager.ConnectionStrings [ConnectionString]。ConnectionString;
string str;
SqlCommand com;
protected void Page_Load(object sender,EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
if(!IsPostBack)
{
DropDownList1.Items.Add(选择名称);
con.Open();
str =select * from employee;
com = new SqlCommand(str,con);
SqlDataReader reader = com.ExecuteReader();
while(reader.Read())
{
DropDownList1.Items.Add(reader [empname]。ToString());
}
reader.Close();
con.Close();
}
< br $>
}
private void clear()
{
Label1.Text = ;
Label2.Text =;
}
protected void btn_search_Click(object sender, EventArgs e)
{
clear();
SqlConnection con = new SqlConnection(strConnString);
con .Open();
str =select * from employee wh ere empname =''+ DropDownList1.SelectedItem.Text +'';
com = new SqlCommand(str,con);
SqlDataReader reader = com.ExecuteReader ();
while(reader.Read())
{
Label1.Text = reader [empaddress]。ToString() ;
Label2.Text = reader [sal]。ToString();
}
reader.Close();
con.Close();
}
}
}
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Dropdownlist_click_data_in_textbox
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
if (!IsPostBack)
{
DropDownList1.Items.Add("Choose name");
con.Open();
str = "select * from employee";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
DropDownList1.Items.Add(reader["empname"].ToString());
}
reader.Close();
con.Close();
}
}
private void clear()
{
Label1.Text = "";
Label2.Text = "";
}
protected void btn_search_Click(object sender, EventArgs e)
{
clear();
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from employee where empname=''" + DropDownList1.SelectedItem.Text + "''";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
Label1.Text = reader["empaddress"].ToString();
Label2.Text = reader["sal"].ToString();
}
reader.Close();
con.Close();
}
}
}
使用系统;
使用System.Collections;
使用System.Configuration;
使用System.Data;
使用System.Linq;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.UI.HtmlControls;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Xml.Li nq;
使用System.Data.SqlClient;
命名空间Dropdownlist_click_data_in_textbox
{
public partial class _Default:System .Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings [ConnectionString]。ConnectionString;
string str;
SqlCommand com;
protected void Page_Load(object sender,EventArgs e)
{
DropDownList1.AutoPostBack = true;
SqlConnection con = new SqlConnection(strConnString);
if(!IsPostBack)
{
DropDownList1.Items.Add(选择id);
con.Open();
str =select * from test;
com = new SqlCommand(str,con);
SqlDataReader reader = com.ExecuteReader();
while(reader.Read())
{
DropDownList1.Items.Add(reader [id]。ToString());
}
reader.Close();
con.Close();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender,EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str =select * from test where id =''+ DropDownList1.SelectedItem.Text +'';
com = new SqlCommand(str,con);
SqlDataReader reader = com.ExecuteReader();
while(reader.Read())
{
TextBox1.Text = reader [name]。ToString();
}
reader.Close();
con.Close();
}
}
}
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Dropdownlist_click_data_in_textbox
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.AutoPostBack = true;
SqlConnection con = new SqlConnection(strConnString);
if (!IsPostBack)
{
DropDownList1.Items.Add("Choose id");
con.Open();
str = "select * from test";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
DropDownList1.Items.Add(reader["id"].ToString());
}
reader.Close();
con.Close();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from test where id=''" + DropDownList1.SelectedItem.Text + "''";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
TextBox1.Text = reader["name"].ToString();
}
reader.Close();
con.Close();
}
}
}
这篇关于如何在文本框中显示数据库值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!