我的主要目的是用ListBox1连接到数据库 然后单机里面任意的一个号码 然后就将此编号 的姓名 成绩等这些显示在TextBox里面。 我写的 这段只能在单机选项然后刷新页面后才会显示在TextBox里 有没有什么办法可以单机后直接就显示在TextBox里呢?
int b = ListBox1.SelectedIndex + 1;
try
{ string connStr = "Provider=Microsoft.jet.oledb.4.0;Data source=" + Server.MapPath("App_Data/ScoreAdmin.mdb");
OleDbConnection myConn = new OleDbConnection(connStr); myConn.Open();
string commStr = "Select * from ScoreTable where 编号=" + b.ToString();
OleDbCommand myDbComm = new OleDbCommand(commStr, myConn);
OleDbDataReader myReader = myDbComm.ExecuteReader(); if (myReader.Read())
{
Label6.Text = myReader.GetValue(0).ToString();
TextBox1.Text = myReader.GetValue(1).ToString();
TextBox2.Text = myReader.GetValue(2).ToString();
TextBox3.Text = myReader.GetValue(3).ToString();
TextBox4.Text = myReader.GetValue(4).ToString();
} myReader.Close(); myConn.Close();
}
catch (Exception)
{
Response.Write("Failed!");
}
int b = ListBox1.SelectedIndex + 1;
try
{ string connStr = "Provider=Microsoft.jet.oledb.4.0;Data source=" + Server.MapPath("App_Data/ScoreAdmin.mdb");
OleDbConnection myConn = new OleDbConnection(connStr); myConn.Open();
string commStr = "Select * from ScoreTable where 编号=" + b.ToString();
OleDbCommand myDbComm = new OleDbCommand(commStr, myConn);
OleDbDataReader myReader = myDbComm.ExecuteReader(); if (myReader.Read())
{
Label6.Text = myReader.GetValue(0).ToString();
TextBox1.Text = myReader.GetValue(1).ToString();
TextBox2.Text = myReader.GetValue(2).ToString();
TextBox3.Text = myReader.GetValue(3).ToString();
TextBox4.Text = myReader.GetValue(4).ToString();
} myReader.Close(); myConn.Close();
}
catch (Exception)
{
Response.Write("Failed!");
}
在这个事件里去的ListBox的selectValue,然后处理后给textbox赋值
当用户选中的项不是SelectedIndex所指的项时
页面回发到服务器才引发ListBox的SelectedIndexChanged事件。
ListBox1.SelectedIndexChanged += new EventHandler(ListBox1_SelectedIndexChanged);
void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}