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>");
}
}