ASP.NET Web编程
runat="server"直接回交服务器,处理数据,又以数据加密后的hidden属性的input控件插入回去,实现表单的状态保存 ruant="server"表示这个控件是在服务器端运行的,说简单点就是你可以在.cs后台代码里引用到这个控件。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Registration.aspx.cs" Inherits="EventRegistrationWeb.Registration" %>
//定义了要使用的编程语言和类。属性 AutoEventWireup="true"表示,页面的事件处理程序自动链接到特定方法名上。Inherits="EventRegistrationWeb.Registration"表示ASPX文件中动态生成的类派生于基类Registration,
这个基类位于用CodeBehind属性定义的代码隐藏文件Registration.aspx.cs中。
还有一些带有runat="server"特性的HTML 元素,如form元素. 通过runat="server"特性,
ASP.NET 服务器控件就会与HTML 标记关联起来。这个控件可以用于写入服务器端代码。在form
元素的后面是System.Web.UI.HtmlControls.HtmlForm 类型的一个对象,该对象有一个用id 特性定义
的变量名form1。form1 可以用于调用HtmlForm 类的方法和属性。
HtmlForm 对象创建一个发送给客户端的form标记。
<form id="form1" runat="server">
string selectedEvent = dropDownListEvents.SelectedValue;
SelectedValue属性返回当前的选择
try
{
DropDownList dropDownListEvents =
(DropDownList)PreviousPage.FindControl("dropDownListEvents");
string selectedEvent = dropDownListEvents.SelectedValue;
string firstName = ((TextBox)PreviousPage.FindControl(
"textFirstName")).Text;
string lastName = ((TextBox)PreviousPage.FindControl(
"textLastName")).Text;
string email = ((TextBox)PreviousPage.FindControl(
"textEmail")).Text;
labelResult.Text = String.Format("{0} {1} selected the event {2}",
firstName, lastName, selectedEvent);
}
catch
{
labelResult.Text = "The originating page must contain " +
"textFirstName, textLastName, textEmail controls";
把Registration.aspx 页面上Submit 按钮的PostbackUrl 属性设置为ResultsPage.aspx。
回送
浏览器把第一个页面中窗体的所有数据都发送到新页面上,但是,在新请求的页面上,需要从
前面页面定义的控件中获取数据。为了访问前面页面中的控件,Page 类定义了属性PreviousPage。
PreviousPage 返回一个Page 对象,这个页面的控件可以使用FindControl()方法来访问。FindControl()
定义为返回一个Control 对象,所以必须把返回值的类型转换为所搜索的控件类型。
DropDownList dropDownListEvents =
((DropDownList)PreviousPage.FindControl("dropDownListEvents")).Text;
创建强类型对象
试一试:创建强类型化的PreviousPage
(1) 选择“项目 ➪ 添加类”(Project ➪ Add New Class),为项目添加一个新类,命名为
RegistrationInfo。
(2) 实现RegistrationInfo 类,如下所示:
public class RegistrationInfo
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string SelectedEvent { get; set; }
}
(3) 在Registration.aspx.cs 文件中给Registration 类添加公共属性RegistrationInfo:
public RegistrationInfo RegistrationInfo
{
get
{
return new RegistrationInfo
{
FirstName = textFirstName.Text,
LastName = textLastName.Text,
Email = textEmail.Text,
SelectedEvent = dropDownListEvents.SelectedValue
};
}
}
(4) 在ResultPage.aspx 文件的Page 指令下面添加PreviousPageType 指令:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ResultsPage.aspx.cs"
Inherits="EventRegistrationWeb.ResultsPage" %>
<%@ PreviousPageType VirtualPath="~/Registration.aspx" %>
(5) 在ResultsPage 类的Page_Load()方法中,代码可以简化为:
protected void Page_Load(object sender, EventArgs e)
{
try
{
RegistrationInfo ri = PreviousPage.RegistrationInfo;
labelResult.Text = String.Format("{0} {1} selected the event {2}",
ri.FirstName, ri.LastName, ri.SelectedEvent);
}
catch
{
labelResult.Text = "The originating page must contain " +
"textFirstName, textLastName, textEmail controls";
}
}
PreviousPageType 指令创建了一个PreviousPage 类型的属性,它返回与该指令关联的类型。在
其实现代码中,调用了基类的PreviousPage 属性,如下面的代码所示:
public new EventRegistrationWeb.Default PreviousPage {
get {
return ((EventRegistrationWeb.Default)(base.PreviousPage));
}
}
这里没有使用PreviousPageType 指令的VirtualPath 特性定义上一个页面的类型,而使用了
TypeName 特性。如果前面有多个页面,就可以使用这个特性。此时,需要为前面的所有页面定义
一个基类,并把该基类赋予TypeName 特性。
ASP.NET Web编程的更多相关文章
- asp.net web编程开发将model键值对化
关键字:model属性,反射 正文 model是数据库的映射,在.net web开发中,作为程序的最底层.web开发的一切都是基于数据库的,分了层之后,就基于model了. 为什么要将 ...
- 《ASP.NET MVC4 WEB编程》学习笔记------Web API
本文截取自情缘 1. Web API简单说明 近来很多大型的平台都公开了Web API.比如百度地图 Web API,做过地图相关的人都熟悉.公开服务这种方式可以使它易于与各种各样的设备和客户端平台集 ...
- ASP.NET Web API编程——路由
路由过程大致分为三个阶段: 1)请求URI匹配已存在路由模板 2)选择控制器 3)选择操作 1匹配已存在的路由模板 路由模板 在WebApiConfig.Register方法中定义路由,例如模板默认生 ...
- ASP.NET MVC 5 Web编程2 -- URL映射(路由原理)
本章将讲述ASP.NET MVC5 的路由原理,即URL映射机制. 简单点就是解释:为什么MVC在浏览器输入地址就能访问到类(或类中的方法)?这是怎么做到的?我自己可以通过.NET写出一个自己的MVC ...
- How and Where Concurrent Asynchronous I/O with ASP.NET Web API 对异步编程分析的非常的好
How and Where Concurrent Asynchronous I/O with ASP.NET Web API http://www.tugberkugurlu.com/archive/ ...
- ASP.NET MVC 4 Web编程
http://spu.jd.com/11309606.html 第1章 入门第2章 控制器第3章 视图第4章 模型第5章 表单和HTML辅助方法第6章 数据注解和验证第7章 成员资格.授权和安全性第8 ...
- 《ASP.NET MVC4 WEB编程》学习笔记------Web API 续
目录 ASP.NET WEB API的出现缘由 ASP.NET WEB API的强大功能 ASP.NET WEB API的出现缘由 随着UI AJAX 请求适量的增加,ASP.NET MVC基于Jso ...
- ASP.NET Web API编程——序列化与内容协商
1 多媒体格式化器 多媒体类型又叫MIME类型,指示了数据的格式.在HTTP协议中多媒体类型描述了消息体的格式.一个多媒体类型包括两个字符串:类型和子类型. 例如: text/html.image/p ...
- ASP.NET Web API编程——构建api帮助文档
1 概要 创建ASP.NET Web Api 时模板自带Help Pages框架. 2 问题 1)使用VS创建Web Api项目时,模板将Help Pages框架自动集成到其中,使得Web Api项目 ...
随机推荐
- 将Python代码嵌入C++程序进行编写
将Python代码嵌入C++程序进行编写的实例,python嵌入 把python嵌入的C++里面需要做一些步骤 安装python程序,这样才能使用python的头文件和库 在我们写的源文件中增加“Py ...
- 运行预构建 Linux 映像的 Windows Azure 虚拟机中的交换空间 – 第 1 部分
本文章由 Azure CAT 团队的 Piyush Ranjan (MSFT) 撰写. 随着基础结构服务(虚拟机和虚拟网络)近期在 Windows Azure 上正式发布,越来越多的企业工作负荷正在向 ...
- iso-开发基础知识-1-程序流程
main-应用程序委托-视图控制器 main()---主函数 应用程序委托 ---AppDelegate 视图控制器 ---ViewController - (BOOL)applicatio ...
- windows 7 memcached报failed to install service or service already installed的解决方案
今天心血来潮捣鼓一下memcache,由于系统是windows 7,我参考了 Windows下安装Memcache 使用memcached for Win32. 在运行memcached.exe -d ...
- 【Deep Learning】genCNN: A Convolutional Architecture for Word Sequence Prediction
作者:Mingxuan Wang.李航,刘群 单位:华为.中科院 时间:2015 发表于:acl 2015 文章下载:http://pan.baidu.com/s/1bnBBVuJ 主要内容: 用de ...
- iOS点滴- ViewController详解
一.生命周期 当一个视图控制器被创建,并在屏幕上显示的时候. 代码的执行顺序 1. alloc 创建对象,分配空间 2.init (initW ...
- IHttpModule与IHttpHandler的区别整理
IHttpModule与IHttpHandler的区别整理1.先后次序.先IHttpModule,后IHttpHandler. 注:Module要看你响应了哪个事件,一些事件是在Handler之前运行 ...
- C++ Primer 读书笔记 第2章 变量和基本类型
C++ Primer 第二章 变量和基本类型 2.1 基本内置类型 C++定义了一组表示整数.浮点数.单个字符和布尔值的算术类型(arithmetic type),此外还定义了Void类型. 算术类型 ...
- Ueditor和CKeditor 两款编辑器的使用与配置
一丶ueditor 百度编辑器 1.官方文档,演示,下载地址:http://ueditor.baidu.com/website/index.html 2.百度编辑器的好:Editor是由百度web前端 ...
- Linux分区方案
创建三个分区 1./boot 启动分区 存放内核和启动程序 空间分配:100M 类型:ext4 2./swap 交换分区 虚 ...