Webform——内置对象(ResponseRequest)和Repeater中的增删改
Posted 这都不是事
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Webform——内置对象(ResponseRequest)和Repeater中的增删改相关的知识,希望对你有一定的参考价值。
一、内置对象
(一)Response对象
1、简介:response 对象在ASP中负责将信息传递给用户.Response对象用于动态响应客户端请求,并将动态生成的响应结果返回到客户端浏览器中,使用Response对象可以直接发送信息给浏览器,重定向浏览器到另一个URL或设置cookie的值等.
2、方法:①、write方法:response.write **
(二)Request对象
●不要传过长东西,因为长度有限,过长会造成数据丢失。
</style>
<%--光棒效果--%>
<script type="text/javascript">
window.onload = function () {
var items = document.getElementsByClassName("tr_Item");
var oldColor = "";
for (var i = 0; i < items.length; i++) {
items[i].onmouseover = function () {
oldColor = this.style.backgroundColor;
this.style.backgroundColor = "yellow";
};
items[i].onmouseout = function () {
this.style.backgroundColor = oldColor;
};
}
};
</script>
</head>
<body>
<form id="form1" runat="server">
<div >
<a href ="Login.aspx"><asp:Label ID="Labdl" runat="server" Text="[请登录]"></asp:Label></a>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
<asp:Button ID="Btntc" runat="server" Text="退出登陆" />
</div>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table id="tb1">
<tr id="tr_head">
<td>用户名</td>
<td>密码</td>
<td>昵称</td>
<td>性别</td>
<td>生日</td>
<td>年龄</td>
<td>民族</td>
<td>操作</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="tr_Item" style="<%#Eval("Red")%>">
<td><%#Eval("UserName") %></td>
<td><%#Eval("PassWord") %></td>
<td><%#Eval("NickName") %></td>
<td><%#Eval("SexStr") %></td>
<td><%#Eval("BirthdayStr") %></td>
<td><%#Eval("Age") %></td>
<td><%#Eval("NationName") %></td>
<td> <a href="Delete.aspx?un=<%#Eval("UserName") %>" onclick="Del" >删除</a>
<a href="Update.aspx?un=<%#Eval("UserName") %>" target="_blank" onclick="Update">修改</a>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:Button ID="btn1" runat="server" Text="添加用户" />
<%--<input id="btn1" type="button" value="添加用户" /><br />--%>
<%--<script>
document.getElementById("btn1").onclick = function () {
window.open("Add.aspx", "_blank");
};
</script>--%>
</form>
</body>
</html>
主页后台代码:
protected void Page_Load(object sender, EventArgs e) { if (Request.Cookies["user"] != null) { Users u = new UsersDA().Select(Request.Cookies["user"].Value); Labdl.Text = u.NickName; Literal1.Text = ",欢迎你!"; } if (!IsPostBack) { Repeater1.DataSource = new UsersDA().Select(); Repeater1.DataBind(); } Btntc.Click += Btntc_Click; btn1.Click += btn1_Click; } void btn1_Click(object sender, EventArgs e) { if (Request.Cookies["user"] != null) { Response.Redirect("Add.aspx"); } else { Response.Redirect("Login.aspx"); } } void Btntc_Click(object sender, EventArgs e) { //1清除cookies Response.Cookies["user"].Expires = DateTime.Now.AddDays(-5); //2刷新页面/跳到登陆页面 Response.Redirect("Login.aspx"); } public void Del(object sender, EventArgs e) { if (Request.Cookies["user"] != null) { Response.Redirect("Delete.aspx"); } else { Response.Redirect("Login.aspx"); } } public void Update(object sender, EventArgs e) { if (Request.Cookies["user"] != null) { Response.Redirect("Update.aspx"); } else { Response.Redirect("Login.aspx"); } }
点击主页“”增加用户“按钮”,跳转到Add(添加)页面。
(一)增加
Add页面前台代码:
<title></title> <%--判断两次密码是否一致--%> <script type="text/javascript"> window.onload = function () { document.getElementById("Button1").onclick = function () { var pwd1 = document.getElementById("TextBox2").value; var pwd2 = document.getElementById("TextBox3").value; if (pwd1 != pwd2) { document.getElementById("Label1").innerText = "两次密码不一致!"; return false; } }; }; </script> <style type="text/css"> #Label1 { color: red; } </style> </head> <body> <form id="form1" runat="server"> <h1>用户添加</h1> 用户名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> <br /> 密码:<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox><br /> <br /> 重复密码:<asp:TextBox ID="TextBox3" runat="server" TextMode="Password"></asp:TextBox><asp:Label ID="Label1" runat="server" Text=""></asp:Label><br /> <br /> 昵称:<asp:TextBox ID="TextBox4" 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="DropDownList1" runat="server"></asp:DropDownList>年 <asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>月 <asp:DropDownList ID="DropDownList3" runat="server"></asp:DropDownList>日 <br /> <br /> 民族:<asp:DropDownList ID="DropDownList4" runat="server"></asp:DropDownList><br /> <br /> <asp:Button ID="Button1" runat="server" Text="添加" /> </form> </body> </html>
Add页面后台代码:
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()); DropDownList1.Items.Add(li); } for (int i = 1; i <= 12; i++) { ListItem li = new ListItem(i.ToString(), i.ToString()); DropDownList2.Items.Add(li); } for (int i = 1; i <= 31; i++) { ListItem li = new ListItem(i.ToString(), i.ToString()); DropDownList3.Items.Add(li); } DropDownList4.DataSource = new NationData().Select(); DropDownList4.DataTextField = "NationName"; DropDownList4.DataValueField = "NationCode"; DropDownList4.DataBind(); } Button1.Click += Button1_Click; } void Button1_Click(object sender, EventArgs e) { 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; 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>"); } else { Response.Write("<script>alert(\'添加失败!\')</script>"); } }
注:在Add页面中点击“”“添加”按钮,会提示添加是否成功,若添加成功,关闭Add页面,刷新Main主页面。在这里用到了Response对象中的write方法!
(二)、删除
Delete页面前台代码:无
Delete页面后台代码:
protected void Page_Load(object sender, EventArgs e) { //1、获得要删除的主键值,username string Uname = Request["un"].ToString(); //2、删除 new UsersDA().Delete(Uname); //3、调回显示页面 Response.Redirect("Main.aspx"); }
注:在这里用到了Response对象中的Redirect方法和Request对象中的QueryString集合,当然,在用QueryString集合时,需要Main主页里面写传递,这个可以见上面的Main主页前台代码。
(三)、修改
Update前台代码:
<form id="form1" runat="server"> <h1>用户修改</h1> 用户名:<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br /> <br /> 密码:<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox><br /> <br /> 重复密码:<asp:TextBox ID="TextBox3" runat="server" TextMode="Password"></asp:TextBox><asp:Label ID="Label1" runat="server" Text=""></asp:Label><br /> <br /> 昵称:<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br /> <br /> 性别:<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow"> <asp:ListItem Value="True">男</asp:ListItem> <asp:ListItem Value="False">女</asp:ListItem> </asp:RadioButtonList><br /> <br /> 生日:<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>年 <asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>月 <asp:DropDownList ID="DropDownList3" runat="server"></asp:DropDownList>日 <br /> <br /> 民族:<asp:DropDownList ID="DropDownList4" runat="server"></asp:DropDownList><br /> <br /> <asp:Button ID="Button1" runat="server" Text="修改" /> </form>
Update后台代码:
//建一个变量来存储原密码
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 >= 1900; i--)
{
ListItem li = new ListItem(i.ToString(), i.ToString());
DropDownList1.Items.Add(li);
}
for (int i = 1; i <= 12; i++)
{
ListItem li = new ListItem(i.ToString(), i.ToString());
DropDownList2.Items.Add(li);
}
for (int i = 1; i <= 31; i++)
{
ListItem li = new ListItem(i.ToString(), i.ToString());
DropDownList3.Items.Add(li);
}
DropDownList4.DataSource = new NationData().Select();
DropDownList4.DataTextField = "NationName";
DropDownList4.DataValueField = "NationCode";
DropDownList4.DataBind();
//3、将对象中的数据绑定到每一个控件上去
Label2.Text = u.UserName;
TextBox4.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;
}
}
}
DropDownList1.SelectedValue = u.Birthday.Year.ToString();
DropDownList2.SelectedValue = u.Birthday.Month.ToString();
DropDownList3.SelectedValue = u.Birthday.Day.ToString();
DropDownList4.SelectedValue = u.Nation;
}
Button1.Click += Button1_Click;
}
void Button1_Click(object sender, EventArgs e)
{
//1、构建一个Users对象
Users u = new Users();
u.UserName = Label2.Text;
//获取密码
if (TextBox2.Text == "" && TextBox3.Text == "")
{
u.PassWord = pwd;
}
else
{
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 UsersDA().Update(u);
//3、提示添加成功
if (ok)
{
Response.Write("<script>alert(\'修改成功!\')</script>");
Response.Write("<script>this.opener.location.href=\'Main.aspx\';this.close();</script>");
}
else
{
Response.Write("<script>alert(\'修改失败!\')</script>");
}
注:●在这里用到了Response对象中的write方法和Request对象中的QueryString集合,当然,在用QueryString集合时,需要Main主页里面写传递,这个可以见上面的Main主页前台代码。
●点击“”修改“”按钮,弹提示框,若添加成功,关闭Update页面,刷新Main主页面。
后注:部分重要代码
1、js代码判断两次密码是否一致