ASP:在Web表单之间传递值

时间:2011-05-01 19:06:13

标签: asp.net vb.net

我试图将日期控件的值从form1传递给form2。

表单上的

1.aspx.vb:

   Public ReadOnly Property Property1() As Date
        Get
            Return StartDate.SelectedDate
        End Get
    End Property

在Form2.aspx上:

<%@ PreviousPageType VirtualPath="~/form1.aspx" %> 

在form2.aspx.vb上:

 Label14.Text = PreviousPage.Property1

当我运行它时,编译器给出了一个错误:

"Object reference not set to an instance of an object."

标记为红色:

Label14.Text = PreviousPage.Property1

尝试将属性分配给字符串,它也不起作用。

任何建议???

的问候。

2 个答案:

答案 0 :(得分:0)

我认为Startdate未声明/初始化,您如何为此readonly属性设置数据?

答案 1 :(得分:0)

如果直接访问Form2.aspx页面而没有跨页面发布,则PreviousPage属性为null。您应该在检索Property1的值之前添加此检查:

if (PreviousPage != null && PreviousPage.IsCrossPagePostBack) {
    Label14.Text = PreviousPage.Property1
}

使用PreviousPageType VirtualPath =“〜/ form1.aspx”指令比Form1.aspx的其他页面跨页面发布到Form2时有点危险。 PreviousPage属性抛出InvalidCastException(它需要Form1页面,但它会得到其他东西)。

有关详细信息,请参阅:http://msdn.microsoft.com/en-us/library/ms178139.aspx