Response對象:響應請求 Response.Write("<script>alert('添加成功!')</script>"); Response.Redirect("Default.aspx"); Request對象:獲取請求 Request["key"]來獲取傳遞過來的值 QueryStri ...
Response對象:響應請求
Response.Write("<script>alert('添加成功!')</script>");
Response.Redirect("Default.aspx");
Request對象:獲取請求
Request["key"]來獲取傳遞過來的值
QueryString:地址欄數據傳遞 ?key=value&key=value
註意事項:不需要保密的東西可以傳
不要傳過長東西,因為長度有限,過長會造成數據丟失
對數據表的增刪改:
Default.aspx中添加用戶
<input id="btn1" type="button" value="添加用戶" /> <script> document.getElementById("btn1").onclick = function () { window.open("Default3.aspx", "_self"); }; </script>
首先數據訪問類造一個添加方法
public bool Insert(Users u) {//添加 bool isok = false; cmd.CommandText = "insert into Users values(@a,@b,@c,@d,@e,@f)"; 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; }
添加:
<body> <form id="form1" runat="server"> <h1>用戶添加</h1> 用戶名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><br /> 密碼:<asp:TextBox ID="TextBox3" runat="server" TextMode="Password"></asp:TextBox><br /><br /> 確認密碼:<asp:TextBox ID="TextBox4" runat="server" TextMode="Password"></asp:TextBox><br /><br /> 昵稱:<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox><br /><br /> 性別:<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow"> <asp:ListItem Value="true" Selected="True">男</asp:ListItem> <asp:ListItem Value="false">女</asp:ListItem> </asp:RadioButtonList><br /><br /> 生日:<asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>年<asp:DropDownList ID="DropDownList3" runat="server"></asp:DropDownList>月<asp:DropDownList ID="DropDownList4" runat="server"></asp:DropDownList>日<br /><br /> 民族:<asp:DropDownList ID="DropDownList1" runat="server" Width="122px"></asp:DropDownList><br /><br />         <asp:Button ID="Button1" runat="server" Text="註 冊" /><br /> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> </form> </body>
密碼JS驗證
<script type="text/javascript"> window.onload = function () {/*document操作取出密碼框里內容*/ document.getElementById("Button1").onclick = function () { var pwd1 = document.getElementById("TextBox3").value; var pwd2 = document.getElementById("TextBox4").value; /* alert(pwd1);檢測一下*/ /* alert(pwd2);*/ if (pwd1 != pwd2) { document.getElementById("Label2").innerText = "兩次密碼輸入不一致"; return false;/*密碼不一阻止刷新,一樣就刷新*/ } }; }; </script> <style type="text/css"> #Label2 { color:red;/*Label2里所呈現的文字顯示紅色*/ } </style> </head>
性別預設選中,生日需三個DropDownList
<asp:ListItem Value="true" Selected="True">男</asp:ListItem>
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack)//數據綁定 { for (int i = DateTime.Now.Year; i >= 1900; i--) { //添加年 ListItem li = new ListItem(i.ToString(),i.ToString()); DropDownList2.Items.Add(li); } for (int i = 1; i <= 12; i++) { //月 ListItem li = new ListItem(i.ToString(), i.ToString()); DropDownList3.Items.Add(li); } for (int i = 1; i <= 31; i++) { //日 ListItem li = new ListItem(i.ToString(), i.ToString()); DropDownList4.Items.Add(li); } //取出民族的數據 DropDownList1.DataSource = new NationDA().Select(); DropDownList1.DataTextField = "NationName"; DropDownList1.DataValueField = "NationCode"; DropDownList1.DataBind(); } Button1.Click += Button1_Click;//事件委托 }
void Button1_Click(object sender, EventArgs e)
{
//1、構建一個Users對象
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、將此對象添加到資料庫去
bool ok = new UsersData().Insert(u);
//3、提示添加成功
if (ok)
{
Response.Write("<script>alert('添加成功!')</script>");
Response.Redirect("Default.aspx");
}
else
{
Response.Write("<script>alert('添加失敗!')</script>");
}
//4、關閉此頁面,刷新展示頁面
}
}
二、刪除
操作,在default.aspx主頁數據顯示中添加一列,點刪除,打開新的網頁delete.aspx執行代碼後關閉,刷新主頁面
<td>操作</td> <td><a href="delete.aspx"?un=<%#Eval("UserName")
>刪除</a></td>
//新網頁中執行的刪除代碼 //1、獲取要刪除的主鍵值,username,做刪除的方法 string Uname = Request["un"].ToString(); 獲取請求 //2、刪除 new UsersDA().Delete(Uname); //3、調回Main頁面 Response.Redirect("Main.aspx");
三、修改
新建窗體xiugai.aspx 數據展示頁面添加一列修改,點擊進入xiugai.aspx
<td><a href="#">修改</a></td>
數據操作類添加方法:
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; }
//1步、構建一個Users對象 Users u = new Users(); u.UserName = Label1.Text; if (TextBox3.Text == "" && TextBox4.Text == "") {//判斷密碼的 u.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 = Convert.ToDateTime(data); u.Nation = DropDownList1.SelectedItem.Value; //2步、將此對象添加到資料庫去,先在UserDA里修改方法 bool ok = new UsersDA().Update(u); //3步、提示修改成功 if (ok) { Response.Write("<script>alert('修改成功!')</script>"); 4步、Response.Write("<script>this.opener.location.href='Main.aspx';this.close();</script>"); //Response.Redirect("Main.aspx");//重定項 } else { Response.Write("<script>alert('修改失敗!')</script>"); } //4、關閉此頁面,刷新展示頁面 //用JS寫 } }