Webform 内置对象(Response对象、Request对象,QueryString)
Response对象:响应请求
Response.Write("<script>alert('添加成功!')</script>");
Response.Redirect("Default.aspx");
Request对象:获取请求
Request["key"]来获取传递过来的值
QueryString:地址栏数据传递 ?key=value&key=value
注意事项:不需要保密的东西可以传
不要传过长东西,因为长度有限,过长会造成数据丢失
Cookies:
在用户电脑的硬盘上保存的一段文本
http协议包括浏览器,允许站点在用户电脑上以Cookies的形式来临时保存数据
如果没有设置保存时间,会话cookies
1、如果你20分钟内没有再次刷新页面,那么此cookies就会自动删除掉
2、当当前访问连接中断,如关闭浏览器,那么cookies会自动删除
作用:
保持用户的登陆状态
对数据表的增删改:
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 >= ; i--)
{
//添加年
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();
}
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写
}
}
Cookies:
Webform 内置对象(Response对象、Request对象,QueryString)的更多相关文章
- webform内置对象
1.Response和Request地址栏数据拼接 QueryString 优点:简单好用:速度快:不消耗服务器内存. 缺点:只能传字符串:保密性差(调转页面后在地址栏显示):长度有限.响应请求对象 ...
- python 迭代器(一):迭代器基础(一) 语言内部使用 iter(...) 内置函数处理可迭代对象的方式
简介 在 Python 中,所有集合都可以迭代.在 Python 语言内部,迭代器用于支持: 1.for 循环2.构建和扩展集合类型3.逐行遍历文本文件4.列表推导.字典推导和集合推导5.元组拆包6. ...
- Webform 内置对象 Response对象、Request对象,QueryString
Request对象:获取请求Request["key"]来获取传递过来的值 QueryString:地址栏数据传递 ?key=value&key=value注意事项:不需要 ...
- Webform(内置对象-Response与Redirect、QueryString传值、Repeater删改)
一.内置对象(一)Response - 响应请求对象1.定义:Response对象用于动态响应客户端请示,控制发送给用户的信息,并将动态生成响应.Response对象只提供了一个数据集合cookie, ...
- WebForm 内置对象2
Session: 与Cookies相比 相同点:每一台电脑访问服务器,都会是独立的一套session,key值都一样,但是内容都是不一样的 以上所有内容,都跟cookies一样 不同点: 1.Sess ...
- WebForm 内置对象
内置对象: Response对象:响应请求 Response.Write("<script>alert('添加成功!')</script>"); Respo ...
- C# WebForm内置对象2+Repeater的Command
内置对象:用于页面之间的数据交互 为什么要使用这么内置对象?因为HTTP的无状态性. Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及 ...
- C#WebForm内置对象
内置对象: Response对象:响应请求Response.Write("<script>alert('添加成功!')</script>");Respons ...
- WebForm 内置对象QueryString、Repeater删改
一.内置对象QueryString--地址栏数据拼接 格式:?key=value 如:string path = "Default2.aspx?aaa=" + TextBox1.T ...
随机推荐
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
Visual Studio 2017中使用正则修改部分内容 最近在项目中想实现一个小工具,需要根据类的属性<summary>的内容加上相应的[Description]特性,需要实现的效 ...
- C#建立最简单的web服务,无需IIS
软件架构师何志丹 本程序仅仅是入门级程序.所以不考虑 1.多线程. 2,安全性. 3,不考虑端点下载文件. 4,Keep-Alive. 5,不考虑head. 6,为了简洁,删掉了catch的内容. e ...
- hdu 4902 Nice boat--2014 Multi-University Training Contest 4
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=4902 Nice boat Time Limit: 30000/15000 MS (Java/Othe ...
- 怎样免费设置QQ空间背景音乐
怎样免费设置QQ空间背景音乐 1.打开QQ空间,点击 2. 3. 4.这里它要求我们输入歌曲的在线路径,并且必须是MP3格式的,这就简单了,我们仅仅要去网上找在线的MP3音乐就能够了.可是如今非常多提 ...
- Servlet第七课:ServletContext HttpSession 以及HttpServletRequest之间的关系
课程目标: ① 在Servlet中懂得ServletContext HttpSession 以及HttpServletRequest之间的关系 ② 懂得怎样使用它们 概念介绍: 1. [共同点]不管对 ...
- 基于docker容器搭建fastdfs分布式文件系统
本次环境的搭建参考了 https://blog.csdn.net/qq_43455410/article/details/84797814, 感谢博主. 主要流程如下: 1. 下载fastdfs镜像 ...
- setTimeout不可靠的修正办法及clearTimeout
javascript里的这两个定时器函数,大家一定耳熟能详: setTimeout("函数()",毫秒)就是开启一个计时器,指定毫秒后执行该函数一次. 有关定时器,javascri ...
- 2016/4/1 PDO:: 数据访问抽象层 ? :
①PDO方式连接 数据库 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- leetcode 660. Remove 9
Start from integer 1, remove any integer that contains 9 such as 9, 19, 29... So now, you will have ...
- Android5.1修改以太网MAC地址(SElinux)【转】
本文转载自:http://blog.csdn.net/LoongEmbedded/article/details/71477203 最近高通平台Android5.1项目中有个关于设置以太网MAC的需求 ...