標籤:
---恢複內容開始---
一、添加
前台代碼:
<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>
View Code
需要 對 性別、生日、民族、密碼 操作:
1、性別:預設選中: <asp:ListItem Value="true" Selected="True">男</asp:ListItem>
2、生日 3、民族:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack)//資料繫結 { for (int i = DateTime.Now.Year; i >= 1900; i--) { //添加年,創造一個ListItem,讓其每迴圈一次就造一個 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(); } }
4、密碼 :
JS驗證一個 點擊添加按鈕,如果兩次密碼輸入不一致,點擊添加按鈕不重新整理頁面,首先在前台確定密碼框後面加一個Lable,來顯示
JS寫在head裡:
<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>
點擊添加按鈕,把添加的內容填到資料庫,並能顯示在介面。添加點擊事件裡共 4步。
資料操作類裡 做一個添加的方法:
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; }View Code
<a href="zhuce.aspx" target="_blank">添加使用者</a> 超連結到註冊介面並新開一個網頁頁面;
Response.Write("<script>this.opener.location.href=‘Main.aspx‘;this.close();</script>"); 添加完點確定關閉當前頁,顯示首頁並且重新整理。
void Button1_Click(object sender, EventArgs e)//委託 { //1、構建一個Users對象 Users u = new Users(); u.UserName = TextBox1.Text; 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().Insert(u); //3、提示添加成功 if (ok) { Response.Write("<script>alert(‘添加成功!‘)</script>"); Response.Write("<script>this.opener.location.href=‘Main.aspx‘;this.close();</script>"); //Response.Redirect("Main.aspx");//重定項 } else { Response.Write("<script>alert(‘添加失敗!‘)</script>"); } //4、關閉此頁面,重新整理展示頁面 //用JS寫 }
Main代碼:
<input id="btn1" type="button" value="添加使用者" /> 添加按鈕 <script> document.getElementById("btn1").onclick = function () { window.open("zhuce.aspx","_blank");開啟一個新的頁面 }; 點擊添加按鈕出來 添加頁面 </script> </form></body></html>
二、刪除
---恢複內容結束---
WebForm---增刪改查