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项目 ...
随机推荐
- ArcEngine做栅格数据拉伸
//获得已打开的栅格数据 IRasterLayer rasterLayer = new RasterLayerClass(); rasterLayer = (IRasterLayer)axMapCon ...
- Windows 7 with SP1简体中文旗舰版(微软MSDN原版)+ 激活密钥
在Windows 7六个版本中,旗舰版和企业版功能性能完全一样,同属诸版本之中的最高版本.现提供Windows 7 with SP1简体中文旗舰版(微软MSDN最新原版)+ 激活密钥如下: 32位版本 ...
- [原]基于CAS实现单点登录(SSO):登录成功后,cas client如何返回更多用户信息
从cas server登录成功后,默认只能从casclient得到用户名.但程序中也可能遇到需要得到更多如姓名,手机号,email等更多用户信息的情况. cas client拿到用户名后再到数据库中查 ...
- Unix/Linux环境C编程入门教程(1) Solaris 11 64bit环境搭建
Unix/Linux版本众多,我们推荐Unix/Linux初学者选用几款典型的Unix/Linux操作系统进行学习. 本文就带大家来安装Solaris 11 64位并且配置好C/C++开发环境 本文所 ...
- Codeforces 263E
Codeforces 263E 原题 题目描述:一个\(n \times m\)的矩阵,每格有一个数,给出一个整数\(k\),定义函数\(f(x, y)\): \[f(x, y)=\sum_{i=1} ...
- [虚拟化/云] kvm的架构分析
预备知识 1. 客户机物理页框到宿主机虚拟地址转换 http://blog.csdn.net/zhuriyuxiao/article/details/8968781 http://www.tuicoo ...
- C函数数组元素初始化
初始化时,可随意指定初始化的元素或者元素的范围. 附gnu c 手册. http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html 代码: t ...
- CF 293 E Close Vertices (树的分治+树状数组)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题目:给出一棵树,问有多少条路径权值和不大于w,长 ...
- gallery利用代码定位图片并且不丢失动画效果
安卓中,利用gallery.setSelection(position);可以手动定位图片 但是众所周知会丢失动画效果 即使是用gallery.setSelection(position,true); ...
- 部署WSP出现错误—已在此服务器场中安装ID为XXXXX的功能
stsadm -o deploysolution -name ***.wsp -immediate -allowGacDeployment -url http://*** -force