码迷,mamicode.com
首页 > Web开发 > 详细

WebForm---增删改查

时间:2016-09-23 23:14:29      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:

---恢复内容开始---

一、添加

前台代码:

技术分享
<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 />
       &nbsp &nbsp &nbsp &nbsp <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---增删改查

标签:

原文地址:http://www.cnblogs.com/yp11/p/5899986.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!