.net控件显示动态的数据库信息

来源:百度知道 编辑:UC知道 时间:2024/05/26 20:14:59
有一个表有两列信息。在DropDownlist中数据绑定第一列,是否能用Label动态的现实数据库中的另一列,在DropDownlist中的选择改变的时候,Label显示的信息是表中同意行的另一列信息。

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Bind();
}
}
public void Bind()
{
int i=1;
string commstr="select * from biao";//biao的组成为id,name,realname
string connstr = "server=(local);database=aaa;uid=sa;pwd=";
SqlConnection conn = new SqlConnection(connstr);
SqlCommand comm = new SqlCommand(commstr, conn);
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
DropDownList1.Items.Add(new ListItem(reader[name].ToString()));
DropDownList1.Items[i].Value = reader[id].ToString();
i++;
}
conn.Close();
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DataRow dr = null;
string connstr = "server=(local);database=aaa;uid=sa;pwd=";
SqlConnection conn = new SqlConnection(connstr);
string strsql=&q