第1种回送方式:同页面回送测试数据
新建几个Label和一个Button 在同一页面旋转一个隐藏Label用于显示传递数据显示。OnButtonClick代码如下:
同页面显示数据:
-
protected void buttonSubmit_Click(object sender, EventArgs e)
-
{
-
string selectedEvent = dropDownListEvents.SelectedValue;
-
string txtFirstName = textFirstName.Text;
-
string txtLastName = textLastName.Text;
-
string txtEmail = textEmail.Text;
-
labelResult.Text = string.Format("{0} {1} selected {2},the Email Address is {3}",txtFirstName,txtLastName,selectedEvent,txtEmail);
-
}
第2种回送方式:第二页面显示测试数据(非调用类方法传递数据)
1.关键在提交按钮中设置:postbackurl地址设置为新结果而Resultpage.aspx;
2.在resultpage.aspx页面中page_load中加入此代码:
-
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";
-
}
第3种回送方式:(使用公共类方法调用传递参数数据)
1.新建一个公共类方法(RegistrationInfo类):
-
public class RegistrationInfo
-
{
-
public string FirstName { get; set; }
-
public string LastName { get; set; }
-
public string Email { get; set; }
-
public string SelectedEvent { get; set; }
-
}
2.在Registration.aspx中调用这个公共类并赋值:
-
public RegistrationInfo RegistrationInfo
-
{
-
get
-
{
-
return new RegistrationInfo
-
{
-
FirstName = textFirstName.Text,
-
LastName = textLastName.Text,
-
Email = textEmail.Text,
-
SelectedEvent = dropDownListEvents.SelectedValue
-
};
-
}
-
}
3.在结果页resultpage.aspx中显示出来,使用previouspage获取前面页面数据
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
try
-
{
-
if (!PreviousPage.IsValid)
-
{
-
labelResult.Text = "Error in previous page!";
-
return;
-
}
-
RegistrationInfo ri = PreviousPage.RegistrationInfo;
-
labelResult.Text = String.Format("{0} {1} selected the event {2} ,Email Address is {3}",ri.FirstName, ri.LastName, ri.SelectedEvent,ri.Email);
-
}
-
catch
-
{
-
labelResult.Text = "The originating page must contain " + "textFirstName, textLastName, textEmail controls";
-
}
-
}
阅读(1233) | 评论(0) | 转发(0) |