我想将员工的编号和姓名绑定到列表中,当我点击列表的某一行时,在右边的各个文本框中显示此员工的其他信息
怎么实现啊?求助!!!
namespace case21
{
public class manage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox user_id;
protected System.Web.UI.WebControls.TextBox username;
SqlConnection conn;
DataSet ds;
SqlDataAdapter da;
SqlCommand com;
string userid;
private void Page_Load(object sender, System.EventArgs e)
{
BindDropDownList();
userid=Request.QueryString["UserID"];
string str;
str="select * from user,dep where user.dep_id=dep.dep_id";
BindList(userid,str);
}
public void DBOpen()
{
string ConnectionString;
ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
conn=new SqlConnection(ConnectionString);
conn.Open();
}
private void BindDropDownList()
{
string strsql;
strsql="select dep_name from dep";
DBOpen();
ds=new DataSet();
da=new SqlDataAdapter(strsql,conn);
da.Fill(ds,"Dep");
dep_name.DataSource=ds.Tables[0].DefaultView;
dep_name.DataBind();
conn.Close();
}
private void BindList(string Userid,string str)
{
BindListBox(str,DataList1);
}
private void BindListBox(string str,DataList DataList1)
{
da=new SqlDataAdapter(str,conn);
da.Fill(ds,"User");
DataView dv=ds.Tables[0].DefaultView;
DataList1.DataSource=dv;
DataList1.DataBind();
if(Userid!=0)
{
DataList1.SelectedIndex=Convert.toInt32(Userid);
// ?
}
}
private void LoadInfo()
{
string Userid=DataList1.SelectedIndex;
string sqlstr="select * from user,dep where user_id='" + Userid + " '";
com = new SqlCommand (sqlstr, conn);
SqlDataReader dr =com.ExecuteReader();
if(dr.Read())
{
user_id.Text=dr["user_id"];
username.Text=dr["username"];
sex.SelectedValue=dr["sex"];
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.update.Click += new System.EventHandler(this.update_Click);
this.delete.Click += new System.EventHandler(this.delete_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Update_Click(object sender, System.EventArgs e)
{
string sqlstr="update user set";
sqlstr+="username='"+ username.Text + "'";
sqlstr+="sex='" + sex.SelectedValue + "'";
sqlstr+="hometown='" + hometown.Text + "'";
sqlstr+="nation='" + nation.Text + "'";
com=new SqlCommand(sqlstr,conn);
com.ExecuteNonQuery();
com.Dispose();
conn.Close();
}
private void delete_Click(object sender, System.EventArgs e)
{
string sqlstr;
if(user_id.Text!=null)
{
sqlstr="delete from user where user_id='" + user_id.Text + "'";
com=new SqlCommand(sqlstr,conn);
com.ExecuteNonQuery();
com.Dispose();
conn.Close();
BindList("","select * from user");
}
}
private void DataList1_SelectedIndexChanged(object sender,System.Web.UI.WebControls.DataListItemEventArgs e)
{
LoadInfo();
}
}
}
[此贴子已经被作者于2007-5-5 16:44:40编辑过]