button中click代码

isihaoren 2006-06-11 09:31:53
我有三个控件:comboBox,button,listBox和一个spl2000建的表(通讯录)我想实现在comboBox显示(通讯录)的某列,而在单击button后,在listBox显示参照comboBox值(通讯录)中的某行,我该怎么实现。
特别是我要在button里的click写什么代码才能按comboBox提供的名字找到该名字所在表(通讯录)中有关该名字的所有信息,并把信息显示在listbox上。数据库要与哪些控件绑定。
...全文
138 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Knight94 2006-06-13
  • 打赏
  • 举报
回复
to 那button中要写什么代码才能查找到comboBox中所选的id号并显示出来呢,我是新手,不懂,麻烦高人写个试例让我参考一下。先谢了。

Use "ComboBox.SelectedValue" to get the current selected value
isihaoren 2006-06-13
  • 打赏
  • 举报
回复

那button中要写什么代码才能查找到comboBox中所选的id号并显示出来呢,我是新手,不懂,麻烦高人写个试例让我参考一下。先谢了。
liuqian0415 2006-06-13
  • 打赏
  • 举报
回复
学习 学习
amandag 2006-06-13
  • 打赏
  • 举报
回复
//给一段示例代码

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

amandag 2006-06-13
  • 打赏
  • 举报
回复
设置comboBox的Text属性显示通讯录的某列,Value属性用来存放id(也就是通讯录的主键)

将来在其他地方使用的时候,根据Value属性再找到对应的纪录
Knight94 2006-06-11
  • 打赏
  • 举报
回复
你可以使用combobox的绑定,然后用displaymember显示name字段信息,然后用valuemember表明id字段信息。这样你就可以在button事件中获得ID,就可以方便进行显示。
liuqian0415 2006-06-11
  • 打赏
  • 举报
回复
up
OLoLO 2006-06-11
  • 打赏
  • 举报
回复
同意楼上的做法,就是先将comboBox绑定,然后在button中写:
1。查找comboBox所选的名字的id号
2。根据id号查找数据库中此id号的信息,然后显示在listBox中

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧