Default gender, three dropdownlists
<Asp: ListItem Value = "true" Selected = "True"> male </asp: ListItem>
Protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) // data binding {for (int I = DateTime. now. year; I> = 1900; I --) {// Add Year ListItem li = new ListItem (I. toString (), I. toString (); DropDownList2.Items. add (li) ;}for (int I = 1; I <= 12; I ++) {// month ListItem li = new ListItem (I. toString (), I. toString (); DropDownList3.Items. add (li) ;}for (int I = 1; I <= 31; I ++) {// day ListItem li = new ListItem (I. toString (), I. toString (); DropDownList4.Items. add (li) ;}// retrieve the national data DropDownList1.DataSource = new NationDA (). select (); DropDownList1.DataTextField = "NationName"; DropDownList1.DataValueField = "NationCode"; DropDownList1.DataBind ();} Button1.Click + = button#click; // event Delegate}
Void button#click (object sender, EventArgs e)
{
// 1. Construct a Users object
Users u = new Users ();
U. UserName = TextBox1.Text;
U. PassWord = TextBox3.Text;
U. NickName = TextBox4.Text;
U. Sex = Convert. ToBoolean (RadioButtonList1.SelectedItem. Value );
String date = DropDownList1.SelectedValue + "-" + DropDownList2.SelectedValue + "-" + DropDownList3.SelectedValue;
U. Birthday = Convert. ToDateTime (date );
U. Nation = DropDownList4.SelectedItem. Value;
// 2. Add this object to the database
Bool OK = new UsersData (). Insert (u );
// 3. A message is displayed, indicating that the instance is successfully added.
If (OK)
{
Response. Write ("<script> alert ('added successfully! ') </Script> ");
Response. Redirect ("Default. aspx ");
}
Else
{
Response. Write ("<script> alert ('failed to add! ') </Script> ");
}
// 4. Close this page and refresh the display page
}
}
Ii. Delete
Operation: Add a column to the data display on the default. aspx homepage, click delete, open a new webpage, delete. aspx, run the code, and close the column. Refresh the homepage.
<Td> operation </td> <a href = "delete. aspx "? Un = <% # Eval ("UserName ")
> Delete </a> </td>
// Delete Code executed on the new webpage // 1. Obtain the primary key value and username to be deleted. The deletion method is string Uname = Request ["un"]. toString (); GET request // 2. Delete new UsersDA (). delete (Uname); // 3. Call back the Main page Response. redirect ("Main. aspx ");
3. Modify
Add a column to the new form xiugai. aspx data display page and click to enter xiugai. aspx
<Td> <a href = "#"> modify </a> </td>
How to add a data operation class:
public bool Update(Users u) { bool isok = false; cmd.CommandText = "update Users set PassWord=@b,NickName=@c,Sex=@d,Birthday=@e,Nation=@f where UserName=@a"; cmd.Parameters.Clear(); cmd.Parameters.Add("@a", u.UserName); cmd.Parameters.Add("@b", u.PassWord); cmd.Parameters.Add("@c", u.NickName); cmd.Parameters.Add("@d", u.Sex); cmd.Parameters.Add("@e", u.Birthday); cmd.Parameters.Add("@f", u.Nation); conn.Open(); try { cmd.ExecuteNonQuery(); isok = true; } catch { } conn.Close(); return isok; }
// Step 1: Construct a Users object Users u = new Users (); u. userName = Label1.Text; if (TextBox3.Text = "" & TextBox4.Text = "") {// u used to determine the password. passWord = pwd;} else {u. passWord = TextBox3.Text;} u. nickName = TextBox6.Text; u. sex = Convert. toBoolean (RadioButtonList1.SelectedItem. value); string data = DropDownList2.SelectedValue + "-" + DropDownList3.SelectedValue + "-" + DropDownList4.SelectedValue; u. birthday = C Onvert. toDateTime (data); u. nation = DropDownList1.SelectedItem. value; // Step 2: add the object to the database. First, modify bool OK = new UsersDA () in UserDA (). update (u); // Step 3. Prompt if (OK) {Response. write ("<script> alert ('modification successful! ') </Script> "); Step 4: Response. write ("<script> this. opener. location. href = 'main. aspx '; this. close (); </script> "); // Response. redirect ("Main. aspx "); // reset the item} else {Response. write ("<script> alert ('modification failed! ') </Script> ");} // 4. Close this page and refresh the display page. // write with JS }}
Cookies
Create a login interface
<Form id = "form1" runat = "server">
Username: <asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox> <br/>
Password: <asp: TextBox ID = "TextBox2" runat = "server"> </asp: TextBox> <br/>
<Asp: CheckBox ID = "CheckBox1" runat = "server"/> <label for = "CheckBox1"> remember the logon status for 7 days </label> <br/>
<Asp: Button ID = "Button1" runat = "server" Text = "login"/>
</Form>
Click Event
protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; } void Button1_Click(object sender, EventArgs e) { bool ok = new UsersData().Select(TextBox1.Text, TextBox2.Text); if (ok) { Response.Cookies["user"].Value = TextBox1.Text; if (CheckBox1.Checked) { Response.Cookies["user"].Expires = DateTime.Now.AddDays(7); } Response.Redirect("Default.aspx"); } }
Login status Persistence:
Cookies
A piece of text saved on the hard drive of your computer
The http protocol includes a browser that allows websites to temporarily store data on users' computers in the form of Cookies.
If no storage time is set, session cookies
1. If you do not refresh the page again within 20 minutes, the cookies will be automatically deleted.
2. When the current access connection is interrupted, such as closing the browser, the cookies will be automatically deleted.
Purpose:
Maintain the user's login status