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 特性。

 

posted @ 2014-12-12 19:45  docyard  阅读(318)  评论(0编辑  收藏  举报