上一篇我们用一般处理程序写了(ashx)写了一遍增删改查,今天我们将用Web窗体(aspx)写一遍增删改查。我们平时写的时候到底用一般处理程序呢?还是用Web窗体呢?
简单来说web窗体(aspx)是一般处理程序(ashx)的一个复杂版本。一般处理程序需要调用一个静态的Html页面,当HTML布局特别复杂的时候,这时候一般处理程序将会写起来很麻烦,开发效率太低。于是出现了Web窗体。当我们建一个Web窗体的时候,会自动生成一个WebForm.aspx.cs这样的一个类。它相当于Web窗体Form1.aspx的父类。

我们先来欣赏一下WebForm1.aspx界面
如果在WebForm1.aspx界面写C#代码必须在<% %>里面
Inherits表示继承:WebForm.aspx.cs类,它相当于Web窗体Form1.aspx类的父类。
CodeBehind:表示后置代码要在WebForm.aspx.cs里面写。

总结一点:复杂的页面布置用web窗体,简单的用一般处理程序
一、查
第一种写法
WebForm1.aspx页面代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebUI2.WebForm1" %>
<!DOCTYPE html>
<%@ Import Namespace="Model" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<link href="CSS/tableStyle.css" rel="stylesheet" />
<script type="text/javascript">
window.onload = function () {
var datas = document.getElementsByClassName("deletes");
var dataLength = datas.length;
for (var i = 0; i < dataLength; i++) {
datas[i].onclick = function () {
if (!confirm("确定要删除吗?")) {
return false;
}
}
}
};
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="Adduser.aspx">添加用户</a>
<table>
<tr>
<th>编号</th>
<th>用户名</th>
<th>密码</th>
<th>邮箱</th>
<th>时间</th>
<th>删除</th>
<th>详细</th>
<th>编辑</th>
</tr>
<%--<% =StrHtml %>--%>
<% foreach (userInfo user in userList)
{%>
<tr>
<td><%=user.Id %></td>
<td><%=user.userName %></td>
<td><%=user.userPass %></td>
<td><%=user.Email %></td>
<td><%=user.regTime.ToShortDateString()%></td>
<td><a href="Del.ashx?id=<%=user.Id %>"class="deletes">删除</a></td>

本文介绍了如何使用ASP.NET的Web窗体(aspx)进行增删改查操作。对比了一般处理程序(ashx),强调Web窗体在复杂页面布局时的优势。内容包括WebForm1.aspx的两种查询方式、AddUser.aspx的新增操作、使用一般处理程序进行删除以及UpdateUser.aspx的更新操作。重点讲解了IsPostBack属性在判断GET和POST请求中的作用,并提醒注意Form标签的runat="server"属性对于__VIEWSTATE的影响。
最低0.47元/天 解锁文章
1711

被折叠的 条评论
为什么被折叠?



