ASP.net页面数据回送三种方法

1250阅读 0评论2016-05-23 icepole
分类:C#/.net

第1种回送方式:同页面回送测试数据
新建几个Label和一个Button  在同一页面旋转一个隐藏Label用于显示传递数据显示。OnButtonClick代码如下:
同页面显示数据:
  1. protected void buttonSubmit_Click(object sender, EventArgs e)
  2.         {
  3.             string selectedEvent = dropDownListEvents.SelectedValue;
  4.             string txtFirstName = textFirstName.Text;
  5.             string txtLastName = textLastName.Text;
  6.             string txtEmail = textEmail.Text;
  7.             labelResult.Text = string.Format("{0} {1} selected {2},the Email Address is {3}",txtFirstName,txtLastName,selectedEvent,txtEmail);
  8.         }
第2种回送方式:第二页面显示测试数据(非调用类方法传递数据)
1.关键在提交按钮中设置:postbackurl地址设置为新结果而Resultpage.aspx;
2.在resultpage.aspx页面中page_load中加入此代码:

  1. try
  2.                     {
  3.                         DropDownList dropDownListEvents = (DropDownList)PreviousPage.FindControl("dropDownListEvents");
  4.                         string selectedEvent = dropDownListEvents.SelectedValue;
  5.                         string firstName = ((TextBox)PreviousPage.FindControl("textFirstName")).Text;
  6.                         string lastName = ((TextBox)PreviousPage.FindControl("textLastName")).Text;
  7.                         string email = ((TextBox)PreviousPage.FindControl("textEmail")).Text;
  8.                         labelResult.Text = String.Format("{0} {1} selected the event {2}",firstName, lastName, selectedEvent);
  9.                      }
  10.                 catch
  11.                     {
  12.                         labelResult.Text = "The originating page must contain " + "textFirstName, textLastName, textEmail controls";
  13.                     }

第3种回送方式:(使用公共类方法调用传递参数数据)
1.新建一个公共类方法(RegistrationInfo类):

  1. public class RegistrationInfo
  2.     {
  3.         public string FirstName { get; set; }
  4.         public string LastName { get; set; }
  5.         public string Email { get; set; }
  6.         public string SelectedEvent { get; set; }
  7.     }
2.在Registration.aspx中调用这个公共类并赋值:

  1. public RegistrationInfo RegistrationInfo
  2.         {
  3.             get
  4.             {
  5.                 return new RegistrationInfo
  6.                 {
  7.                     FirstName = textFirstName.Text,
  8.                     LastName = textLastName.Text,
  9.                     Email = textEmail.Text,
  10.                     SelectedEvent = dropDownListEvents.SelectedValue
  11.                 };
  12.             }
  13.         }
3.在结果页resultpage.aspx中显示出来,使用previouspage获取前面页面数据
  1. protected void Page_Load(object sender, EventArgs e)
  2.         {
  3.             try
  4.             {
  5.                 if (!PreviousPage.IsValid)
  6.                 {
  7.                     labelResult.Text = "Error in previous page!";
  8.                     return;
  9.                 }
  10.                 RegistrationInfo ri = PreviousPage.RegistrationInfo;
  11.                 labelResult.Text = String.Format("{0} {1} selected the event {2} ,Email Address is {3}",ri.FirstName, ri.LastName, ri.SelectedEvent,ri.Email);
  12.             }
  13.             catch
  14.             {
  15.                 labelResult.Text = "The originating page must contain " + "textFirstName, textLastName, textEmail controls";
  16.             }
  17. }




上一篇:盘点科技公司面试中出现的十大奇葩问题
下一篇:ASP.net中使用Ajax Postback回送和ASP.NET Postback传送的区别演示