教你看懂C#web页面

C#web页面详解

因为要做一个C#的web系统,所以想着就用web窗体来做,遇到的第一个问题,就是C#的web面其实跟html是有差别的。所以,这里还是记录一下学习的历程。
code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>图书馆管理系统登录页面</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<link href="css.css" rel="stylesheet" type="text/css"/>
<style type="text/css">
<!--
body {
	background-color: #DDDDDD;
}
-->
</style>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<div align="center">
  <table id="Table1" width="914" height="759" border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td rowspan="5" bgcolor="#DDDDDD">&nbsp;</td>
	    <td height="253" valign="bottom" bgcolor="#65D7D4">&nbsp;</td>
	    <td rowspan="5" bgcolor="#DDDDDD">&nbsp;</td>
    </tr>
    <tr><form name="form1" method="post" action="" runat="server">
      <td height="249" valign="top" background="images/denglu.gif"><table width="777" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="421" height="103">&nbsp;</td>
          <td width="65">&nbsp;</td>
          <td colspan="3">&nbsp;</td>
        </tr>
        <tr>
          <td height="26">&nbsp;</td>
          <td><span class="daohang1">用户登录:
            </span>
            <label></label></td>
          <td colspan="3"><label>
            <asp:TextBox ID="txtAdmin" runat="server"></asp:TextBox>
              &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
          </label></td>
        </tr>
        <tr>
          <td height="22">&nbsp;</td>
          <td class="daohang1">用户密码:</td>
          <td colspan="3"><label>
            <asp:TextBox ID="txtPwd" runat="server" Width="148px" TextMode="Password"></asp:TextBox>
              &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;</label></td>
        </tr>
        <tr>
          <td height="31">&nbsp;</td>
          <td><span class="daohang1">验证码:</span>
            <label></label></td>
          <td colspan="3"><label>
            <asp:TextBox ID="txtCode" runat="server" Width="96px"></asp:TextBox><asp:Image ID="Image1" runat="server" ImageUrl="~/Common/checkcode.aspx"/>
              &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
          </label></td></tr>
        <tr>
          <td height="27">&nbsp;</td>
          <td>&nbsp;</td>
          <td width="69"><label>
            <asp:Button ID="btnLogin" runat="server" Text="登录" OnClick="btnLogin_Click" />
          </label></td>
          <td width="51"><label>
            <asp:Button ID="btnCancel" runat="server" Text="取消" OnClick="btnCancel_Click" />
          </label></td>
          <td width="171"><label></label></td>
        </tr>
      </table></td>
    </form>
    </tr>
    <tr>
      <td width="777" height="272" bgcolor="#65D7D4">&nbsp;</td>
    </tr>
    <tr>
      <td height="66" background="images/index_14.gif">&nbsp;</td>
    </tr>
    <tr>
      <td colspan="3" bgcolor="#DDDDDD">&nbsp;</td>
    </tr>
  </table>
</div>
</body>
</html>
  1. AutoEventWireup: 如果Page指令的AutoEventWireup属性被设置为 true(或者如果缺少此属性,因为它默认为true),该``页框架将自动调用页事件,即Page_Init 和 Page_Load方法。在这种情况下,不需要任何显式的Handles子句或委托。
  2. Inherits:inherits是定义供应用程序类继承的代码隐藏类。
  3. runat:runat="server"直接回交服务器,处理数据.
  4. nbsp:它叫不换行空格,全称No-Break Space,它是最常见和我们使用最多的空格,大多数的人可能只接触了 ,它是按下space键产生的空格。在HTML中,如果你用空格键产生此空格,空格是不会累加的(只算1个)。要使用html实体表示才可累加,该空格占据宽度受字体影响明显而强烈。

不换行空格和常规空格的区别包括:它不允许断行;另外因为它不常规,所以它一般不会被当作普通空格合并。
不换行空格本来应该用在文本中需要避免断行的地方,比如数值的数字和单位之间(比如「100 千米」不应该在中间换行)。

  1. TextMode:TextMode 属性用于设置或返回 TextBox 控件的行为模式。
  2. span: 标签被用来组合文档中的行内元素。
  3. colspan:colspan 属性规定单元格可横跨的列数。
  4. MasterPageFile:MasterPageFile其实是定义一个了外围的框架的页面,并且在里面设定了具体内容存放的位置
  5. ContentPlaceHolder:用来对应包含与当前内容关联的 ContentPlaceHolder 的 ID啊
    说白了就是去找母版页相应的ContentPlaceHolder ,然后把内容扔进那里面去
  6. GridView:它可以非常快速的将数据以表格方式显示在web页面上。下面就是一个利用GridView控件进行数据绑定的小例子.
    在这里插入图片描述
  7. colspan:colspan 属性规定单元格可横跨的列数
  8. EnableEventValidation:EnableEventValidation="true"则会在本地验证,消耗CPU;EnableEventValidation="false"则会回传服务器验证,重新加载页面.
  9. CausesValidation: 属性规定当 Button 控件被点击时是否验证页面。
    当按钮被点击时,默认执行页面验证。该属性常用于当取消按钮或重置按钮被点击时阻止进行验证。
  10. IsPostBack:是否是第一次被加载。IsPostBack只有在第一次打开的时候是false,其它时候都是true。两种情况所要显示的按钮是不一样的。
  11. javascript:history.go(-1): 在修改完成后不采用页面跳转的方式,而是采用javascript:history.go(-1),这样返回到了联系人管理页面后再按返回键会直接跳转到个人中心.同样如果中间连续跳了两个页面,比如修改手机号,从个人信息–>点击修改手机号–>验证手机号密码–>修改手机号–>跳转回个人信息,此时也不应使用页面跳转的方式,而是采用javascript:history.go(-2),这样修改完手机号后返回到个人信息,如果用户再按返回键会直接跳转到个人中心,而不是修改手机号页面。
  12. SqlDataAdapter:SqlDataAdapter是 DataSet和 SQL Server之间的桥接器。SqlDataAdapter通过对数据源使用适当的Transact-SQL语句映射 Fill(它可填充DataSet中的数据以匹配数据源中的数据)和 Update(它可更改数据源中的数据以匹配 DataSet中的数据)来提供这一桥接。当SqlDataAdapter填充 DataSet时,它为返回的数据创建必需的表和列(如果这些表和列尚不存在)。

all in all:sqlDataadapter的作用是实现Dataset和DB之间的桥梁:比如将对DataSet的修改更新到数据库。

  1. DropDownList:创建下拉列表

页面下的逻辑控制类

  1. DataSet:是不依赖于数据库的独立数据集合。
  2. DataBind::数据绑定就是将请求中的参数接收到处理器即控制器中。
    在这里插入图片描述
  3. [SqlParameter]:(https://www.cnblogs.com/angelfeather/articles/1225902.html)SqlParameter 构造函数 (String, SqlDbType, Int32, ParameterDirection, Byte, Byte, String, DataRowVersion, Boolean, Object, String, String, String)
    初始化 SqlParameter 类的一个新实例,该类使用参数名、参数的类型、参数的长度、方向、精度、小数位数、源列名称、DataRowVersion 值之一、用于源列映射的布尔值、SqlParameter 的值、此 XML 实例的架构集合所在的数据库的名称、此 XML 实例的架构集合所在的关系架构以及此参数的架构集合的名称。

sqlParameter对象的作用是将要用于操作数据库的数据(如根据ID查询时要用到id)以参数的形式加入到sql语句中,防止因为拼接字符串而引起的安全问题并且提高可读性。所以使用是要先创建一个sqlParameter对象,在定义时或定义后(取决于创建时调用的哪一个构造函数)将数据绑定到参数。

SqlParameter[] prams = {
	data.MakeInParam("@name",  SqlDbType.VarChar,50,bookcasemanage.Name+"%"),
};

比较重要的功能就是连接字符串啦!.能减少sql 注入的危险。所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令。

  1. System.Web.UI.Page

在ASP.NET中,任何页面都是继承于System.Web.UI.Page,他提供了ASP.NET中的Response,Request,Session,Application的操作.在使用Visual Studio 创建ASP页面时,系统会自动为你创建一个继承与System.Web.UI.Page的类与页面文件关联.

页面存在的问题

  1. 更新图书馆信息有问题:
    在这里插入图片描述

项目存在的问题:

  1. 书架上面的书籍无法获取。

猜你喜欢

转载自blog.csdn.net/weixin_38809485/article/details/85331240
今日推荐