public partial class Register : System.Web.UI.Page
{
private SqlConnection conn;
private SqlDataAdapter dad;
private DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
this.conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DBuserConnectionString"].ToString());
this.dad = new SqlDataAdapter("select id,username,userpwd,sex,address,birthday from userdetails", conn);
this.ds=new DataSet();
this.dad.Fill(ds);
if (!this.IsPostBack)
{
//绑定下拉列表框
this.BindDropDowLlist();
//绑定表单
this.BindForm();
this.Button2.Attributes.Add("onclick", "return confirm('Delete?')");
this.Button3.Attributes.Add("onclick", "return confirm('insert?')");
this.Button4.Attributes.Add("onclick", "return confirm('update?')");
}
}
private void BindDropDowLlist()
{
//为下拉列项提供数据源字段
this.DropDownList5.DataTextField = "username";
this.DropDownList5.DataValueField = "id";
//指定数据源
this.DropDownList5.DataSource = ds.Tables[0];
//将数据绑定到控件中
this.DropDownList5.DataBind();
this.DropDownList1.DataTextField = "address";
this.DropDownList1.DataSource = ds.Tables[0];
this.DropDownList1.DataBind();
}
private void BindForm() {
//是一条记录的集合,所以要用数组;
DataRow[] dr = ds.Tables[0].Select("id = "+ this.DropDownList5.SelectedValue);
//这里访问的也是当前根据id筛选的数据;
this.TextBox1.Text = dr[0]["username"].ToString();
this.TextBox2.Text = dr[0]["userpwd"].ToString();
this.TextBox3.Text = dr[0]["userpwd"].ToString();
//if (dr[0]["sex"].ToString().Equals("boy")) {
// this.RadioButton1.Checked=true;
// this.RadioButton2.Checked = false;
//}
//else if (dr[0]["sex"].ToString().Equals("girl"))
//{
// this.RadioButton2.Checked = true;
// this.RadioButton1.Checked = false;
//}
//else {
// this.RadioButton2.Checked = false;
// this.RadioButton1.Checked = false;
//}
//for (int index = 0; index < this.DropDownList1.Items.Count; index++)
//{
// if (dr[0]["address"].ToString().Equals(this.DropDownList1.Items[index].Text))
// {
// this.DropDownList1.Items[0].Text = dr[0]["address"].ToString();
// }
//}
}
private void DeleteRow() {
//是一条记录的集合,所以要用数组;
DataRow[] dr = ds.Tables[0].Select("id = " + this.DropDownList5.SelectedValue);
//删除当前行;
dr[0].Delete();
}
private void UpdateSQL() {
//利用此对象自动生成insert,update,delete三种SQL语句
SqlCommandBuilder cb = new SqlCommandBuilder(this.dad);
//利用SqlDataAdapter对象的Update方法修改数据库的记录;
this.dad.Update(this.ds);
//重新绑定列表和表单;
this.BindDropDowLlist();
this.BindForm();
}
private void InsertRow() {
//添加一行数据
DataRow dr = ds.Tables[0].NewRow();
dr["username"] = this.TextBox1.Text;
dr["userpwd"] = this.TextBox2.Text;
dr["userpwd"] = this.TextBox3.Text;
ds.Tables[0].Rows.Add(dr);
}
private void updateRow() {
//是一条记录的集合,所以要用数组;
DataRow[] dr = ds.Tables[0].Select("id = " + this.DropDownList5.SelectedValue);
dr[0]["username"] = this.TextBox1.Text;
dr[0]["userpwd"] = this.TextBox2.Text;
dr[0]["userpwd"] = this.TextBox3.Text;
}
protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
{
this.BindForm();
}
protected void Button1_Click(object sender, EventArgs e)
{
if (this.Button1.Text == "New")
{
this.Button1.Text = "cancel";
this.DropDownList5.Enabled = false;
this.TextBox1.Text = " ";
this.TextBox2.Text = " ";
this.TextBox3.Text = " ";
}
else {
this.Button1.Text = "New";
this.DropDownList5.Enabled = true;
this.TextBox1.Enabled = true;
this.TextBox2.Enabled = true;
this.TextBox3.Enabled = true;
this.BindDropDowLlist();
this.BindForm();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
this.DeleteRow();
this.UpdateSQL();
this.RegisterStartupScript("", "<script>alert('删除成功')</script>");
}
protected void Button3_Click(object sender, EventArgs e)
{
this.InsertRow();
this.UpdateSQL();
this.RegisterStartupScript("", "<script>alert('插入成功')</script>");
}
protected void Button4_Click(object sender, EventArgs e)
{
this.updateRow();
this.UpdateSQL();
this.RegisterStartupScript("", "<script>alert('修改成功')</script>");
}
}