一、添加

前台代码:

<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>

需要 对 性别、生日、民族、密码 操作:

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 >= ; i--)
{
//添加年,创造一个ListItem,让其每循环一次就造一个
ListItem li = new ListItem(i.ToString(),i.ToString());
DropDownList2.Items.Add(li);
} for (int i = ; i <= ; i++)
{
//月
ListItem li = new ListItem(i.ToString(), i.ToString());
DropDownList3.Items.Add(li);
} for (int i = ; i <= ; 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;
}

<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>

二、删除

重新开一个窗体:Delete.aspx,

在Main前台密码加上一列:  <td>操作</td>

<td><a href="delete.aspx">删除</a></td>

asox.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class delete : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//1、获取要删除的主键值,username,做删除的方法
string Uname = Request["un"].ToString(); 获取请求 //2、删除
new UsersDA().Delete(Uname);
//3、调回Main页面
Response.Redirect("Main.aspx");
}
}

Main界面的前台代码:

           <ItemTemplate>
<tr class="tr_Item" style='<%#Eval("red") %>'>
<td><%#Eval("UserName") %></td>
<td><%#Eval("Password") %></td>
<td><%#Eval("NickName") %></td>
<td><%#Eval("Ssex") %></td>
<td><%#Eval("Birthday2") %></td>
<td><%#Eval("Age") %></td>
<td><%#Eval("Nation") %></td>
<td><a href="delete.aspx?un=<%#Eval("UserName") %>">删除</a></td> QueryString:传递
</tr> </ItemTemplate>

内置对象:

【1、】Response对象:响应请求
Response.Write("<script>alert('添加成功!')</script>");--显示
Response.Redirect("Default.aspx");--界面重定项

【2、】Request对象:获取请求
Request["key"]来获取传递过来的值

QueryString:地址栏数据传递 ?key=value&key=value

注意事项:不需要保密的东西可以传
不要传过长东西,因为长度有限,过长会造成数据丢失

三、修改

重新开一个窗体:xiugai.aspx,

在Main前台密码  列加:

<td><a href="#">修改</a></td>

修改窗体 aspx前台代码:

 <h1>用户修改</h1>
用户名:<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br /><br />
密码:<asp:TextBox ID="TextBox3" runat="server" TextMode="Password"></asp:TextBox><br /><br />
确认密码:<asp:TextBox ID="TextBox4" runat="server" TextMode="Password"></asp:TextBox><asp:Label ID="Label2" runat="server" Text=""></asp:Label><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 />

Main 前台代码:

<td><a href="delete.aspx?un=<%#Eval("UserName") %>">删除</a>
<a href="xiugai.aspx?un=<%#Eval("UserName") %>">修改</a>
</td>

修改的方法:

    //修改用

    public Users Select(string Uname)
{
Users u = new Users();
cmd.CommandText = "select * from Users where UserName=@username ";
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@username", Uname); conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read(); u.UserName = dr[].ToString();
u.PassWord = dr[].ToString();
u.NickName = dr[].ToString();
u.Sex = Convert.ToBoolean(dr[]);
u.Birthday = Convert.ToDateTime(dr[]);
u.Nation =dr[].ToString(); } conn.Close();
return u;
} 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;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class xiugai : System.Web.UI.Page
{
string pwd = "";//定义一个全局变量
protected void Page_Load(object sender, EventArgs e)
{ //1、将传过来的主键值接收
string uname = Request["un"].ToString(); //2、通过主键值将对象查出来
Users u = new UsersDA().Select(uname);
pwd = u.PassWord;//先记录一下 if (!IsPostBack)//数据绑定
{
for (int i = DateTime.Now.Year; i >= ; i--)
{
//添加年,创造一个ListItem,让其每循环一次就造一个
ListItem li = new ListItem(i.ToString(), i.ToString());
DropDownList2.Items.Add(li);
} for (int i = ; i <= ; i++)
{
//月
ListItem li = new ListItem(i.ToString(), i.ToString());
DropDownList3.Items.Add(li);
} for (int i = ; i <= ; 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(); //3、将对象中的数据绑定到每一个控件上去
Label1.Text = u.UserName;
TextBox6.Text = u.NickName; foreach (ListItem li in RadioButtonList1.Items)
{//性别
if (u.Sex)
{
if (li.Value == "true")
{
li.Selected = true;
}
}
else
{
if (li.Value == "false")
{
li.Selected = true;
}
}
}
DropDownList2.SelectedValue = u.Birthday.Year.ToString();
DropDownList3.SelectedValue = u.Birthday.Month.ToString();
DropDownList4.SelectedValue = u.Birthday.Day.ToString();
DropDownList1.SelectedValue = u.Nation; } Button1.Click += Button1_Click; } void Button1_Click(object sender, EventArgs e)
{
//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写
}
}

WebForm---增删改(内置对象)的更多相关文章

  1. Webform(五)——内置对象(Response、Request)和Repeater中的数据增删改

    一.内置对象 (一)Response对象 1.简介:response 对象在ASP中负责将信息传递给用户.Response对象用于动态响应客户端请求,并将动态生成的响应结果返回到客户端浏览器中,使用R ...

  2. WebForm 内置对象、数据增删改、状态保持

    一.内置对象 1.Response对象:响应请求 Response.Write("<script>alert('添加成功!')</script>"); → ...

  3. Webform(内置对象-Response与Redirect、QueryString传值、Repeater删改)

    一.内置对象(一)Response - 响应请求对象1.定义:Response对象用于动态响应客户端请示,控制发送给用户的信息,并将动态生成响应.Response对象只提供了一个数据集合cookie, ...

  4. WebForm 内置对象QueryString、Repeater删改

    一.内置对象QueryString--地址栏数据拼接 格式:?key=value 如:string path = "Default2.aspx?aaa=" + TextBox1.T ...

  5. WebForm 内置对象

    内置对象: Response对象:响应请求 Response.Write("<script>alert('添加成功!')</script>"); Respo ...

  6. C#WebForm内置对象

    内置对象: Response对象:响应请求Response.Write("<script>alert('添加成功!')</script>");Respons ...

  7. Webform(七)——内置对象(Session、Application)和Repeater的Command操作

    内置对象:用于页面之间的数据交互 为什么要使用这么内置对象?因为HTTP的无状态性. 一.内置对象 (一)Session 跟Cookies一样用来存储用户数据 1.Session.Cookies对比 ...

  8. WebForm 内置对象2

    Session: 与Cookies相比 相同点:每一台电脑访问服务器,都会是独立的一套session,key值都一样,但是内容都是不一样的 以上所有内容,都跟cookies一样 不同点: 1.Sess ...

  9. C# WebForm内置对象2+Repeater的Command

    内置对象:用于页面之间的数据交互 为什么要使用这么内置对象?因为HTTP的无状态性. Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及 ...

随机推荐

  1. Swift基础语法 、 元组(Tuple)

    字符串的使用 1.1 问题 Swift的String和Character类型提供了一个快速的,兼容Unicode的方式来处理代码中的文本信息.创建和操作字符串的语法与C语言中字符串类似.本案例将学习如 ...

  2. nat转换

    实验目的: (1)     了解nat转换 (2)     了解nat转换配置命令 (3)     了解哪些是私有ip地址哪些不是私有ip地址 实验工具: 华为eNSP模拟器和Wireshar 实验拓 ...

  3. POJ 2893 M × N Puzzle(树状数组求逆序对)

                                                               M × N Puzzle Time Limit: 4000MS   Memory ...

  4. 【转载】ANSYS动力学分析-瞬态分析

    原文地址:http://www.cnblogs.com/ylhome/archive/2009/12/02/1615172.html 三种求解方法 瞬态动力学分析可采用三种方法:完全(Full)法.缩 ...

  5. SQL数据库的十条命令

    --(1)查询每个总学时数 select GradeId,SUM(classHour) from subject group by GradeId order by(SUM(classHour)) - ...

  6. LeetCode 【47. Permutations II】

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  7. Semantic UI 中文参考手册

    一个拥有友好词汇表的前端框架,完全语义化的前端界面开发框架,为一组开发人员之间共享UI元素配备了规格.使用的词汇(类和ID)相比其它替代品更加简洁,从而降低了学习曲线.有许多HTML元素,UI元素和场 ...

  8. elasticsearch【更新】操作

    基于上一篇博文基础上,进行es的操作,document的新增比较简单,就不说了,这里主要说说更新操作. 更新操作,有两大类,一个是Replace,一个是Update,就是说一个是替换,一个是更新. 替 ...

  9. winform里dataGridView分页代码,access数据库

    winform里dataGridView分页,默认dataGridView是不分页的和webform里不一样,webform中GridView自带自带了分页. 现在c/s的程序很多时候也需要webfo ...

  10. Lucene/Solr搜索引擎开发笔记 - 第1章 Solr安装与部署(Jetty篇)

    一.为何开博客写<Lucene/Solr搜索引擎开发笔记> 本人毕业于2011年,2011-2014的三年时间里,在深圳前50强企业工作,从事工业控制领域的机器视觉方向,主要使用语言为C/ ...