我有三个控件:comboBox,button,listBox和一个spl2000建的表(通讯录)我想实现在comboBox显示(通讯录)的某列,而在单击button后,在listBox显示参照comboBox值(通讯录)中的某行,我该怎么实现。
特别是我要在button里的click写什么代码才能按comboBox提供的名字找到该名字所在表(通讯录)中有关该名字的所有信息,并把信息显示在listbox上。数据库要与哪些控件绑定。
特别是我要在button里的click写什么代码才能按comboBox提供的名字找到该名字所在表(通讯录)中有关该名字的所有信息,并把信息显示在listbox上。数据库要与哪些控件绑定。
1。查找comboBox所选的名字的id号
2。根据id号查找数据库中此id号的信息,然后显示在listBox中
那button中要写什么代码才能查找到comboBox中所选的id号并显示出来呢,我是新手,不懂,麻烦高人写个试例让我参考一下。先谢了。
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;namespace DataBindDemo
{
public class DropDownList : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnClick;
protected System.Web.UI.WebControls.DropDownList dropAuthors;
private void Page_Load(object sender, System.EventArgs e)
{
if (! IsPostBack )
{
SqlConnection conPubs;
SqlCommand cmdSelect;
SqlDataReader dtrAuthors; conPubs = new SqlConnection( @"Server=localhost;uid=sa;pwd=sa;Database=Pubs" );
conPubs.Open();
cmdSelect = new SqlCommand( "Select au_id, au_lname From Authors", conPubs );
dtrAuthors = cmdSelect.ExecuteReader(); dropAuthors.DataSource = dtrAuthors;
dropAuthors.DataTextField = "au_lname";
dropAuthors.DataValueField = "au_id";
dropAuthors.DataBind(); dtrAuthors.Close();
conPubs.Close();
}
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.btnClick.Click += new System.EventHandler(this.btnClick_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void btnClick_Click(object sender, System.EventArgs e)
{
Response.Write("作者的id为:" + dropAuthors.SelectedValue);
Response.Write("<br>");
Response.Write("作者的姓名为:" + dropAuthors.SelectedItem.Text);
}
}
}