程序是在第一个页面上输入事件,姓名,email地址,然后在第二个页面上显示。
第一个页面的代码是:public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void buttonSubmit_Click(object sender, EventArgs e)
{
}
}
第二个页面的代码是:public partial class ResultPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
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 = firstname + " " + lastname + " selected the event " + selectedEvent;
}
catch
{
labelResult.Text = "The originating page must contain " + "textFirstname, textLastname, textEmail controls";
}
}
}
我把第一个页面的SUBMIT按钮的PostbackUrl属性设为了第二个页面。
第一个页面上有一个DropDownlist,和3个textBox。在里面分别输入数据,然后按SUBMIT按钮,就会在第二个页面上显示。但是发现按下SUBMIT按钮后第二个页面只会显示catch块中的内容。“The originating page must contain textFirstname, textLastname, textEmail controls”是不是使用PreviousPage.FindControl()有错呢,或者是什么其他愿意,怎样更改呢?
多谢了。
第一个页面的代码是:public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void buttonSubmit_Click(object sender, EventArgs e)
{
}
}
第二个页面的代码是:public partial class ResultPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
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 = firstname + " " + lastname + " selected the event " + selectedEvent;
}
catch
{
labelResult.Text = "The originating page must contain " + "textFirstname, textLastname, textEmail controls";
}
}
}
我把第一个页面的SUBMIT按钮的PostbackUrl属性设为了第二个页面。
第一个页面上有一个DropDownlist,和3个textBox。在里面分别输入数据,然后按SUBMIT按钮,就会在第二个页面上显示。但是发现按下SUBMIT按钮后第二个页面只会显示catch块中的内容。“The originating page must contain textFirstname, textLastname, textEmail controls”是不是使用PreviousPage.FindControl()有错呢,或者是什么其他愿意,怎样更改呢?
多谢了。
if(PreviousPage != null)
{
if(PreviousPage.IsCrossPagePostBack == true)
{
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 = firstname + " " + lastname + " selected the event " + selectedEvent;
}
}
else
{
labelResult.Text = "The originating page must contain " + "textFirstname, textLastname, textEmail controls";}
你的第一个页面应该叫Default.aspx
所以记得第二个页面加上<%@ PreviousPageType VirtualPath="~/Default.aspx" %>
如果不是
把红色部分换掉
现在第一个页面是:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
第二个页面是:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ResultPage.aspx.cs" Inherits="ResultPage" %>
<%@ PreviousPageType VirtualPath="~/Default.aspx" %>第二个页面的代码换成了上面的代码
运行后点击第一个页面的按钮,结果提示无法找到第二个页面。
Server Error in '/EventRegistrationWeb' Application
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
DropDownList dropDownListEvents =
(DropDownList)PreviousPage.FindControl("a");//dropDownListEvents string selectedEvent = dropDownListEvents.SelectedValue; string firstname =
((TextBox)PreviousPage.FindControl("b")).Text;//textFirstname
string lastname = ((TextBox)PreviousPage.FindControl("c")).Text;//textLastname
string email = ((TextBox)PreviousPage.FindControl("d")).Text;//textEmail
结果神奇般的成功了……。运行后可以显示
labelResult.Text = firstname + " " + lastname + " selected the event " + selectedEvent;的结果。
然后我再将a,b,c,d改回去(用复制粘贴的方法-_-|),结果再运行,也成功了。我重启C#,然后打开网站,再运行也成功了。
看来只有天知道这是为什么了。
做为一种确实可行的方法,LZ可以试试……