Wrox Programmer Forums
|
BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9
This is the forum to discuss the Wrox book Professional ASP.NET 2.0 Special Edition by Bill Evjen, Scott Hanselman, Devin Rader, Farhan Muhammad, Srinivasa Sivakumar; ISBN: 9780470041789
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 2nd, 2007, 05:58 PM
Registered User
 
Join Date: Dec 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default PreviousPage.FindControl returns nothing

Thanks to a previous post I figured out how to use IsCrossPagePostBack, however I still can't get any data from the previous page. I have the following:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If PreviousPage.IsCrossPagePostBack Then
                Dim pp_Type As TextBox

                pp_Type = CType(PreviousPage.FindControl("txtSearch"), TextBox)
                Label1.Text = pp_Type.Text
            End If
        Catch ex As System.NullReferenceException
            Response.Redirect("default.aspx")
        End Try
    End Sub

When I run it, I get returned to the first page. This does work:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If PreviousPage.IsCrossPagePostBack Then
                Label1.Text = "Recognize page post back"
            End If
        Catch ex As System.NullReferenceException
            Response.Redirect("default.aspx")
        End Try
    End Sub

So I removed the If PreviousPage.IsCrossPagePostBack line so i could see what was going on. The following:

Dim pp_Type As TextBox
pp_Type = PreviousPage.FindControl("txtSearch")
Label1.Text = pp_Type.Text

And I get

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.

Source Error:

Line 13: pp_Type = PreviousPage.FindControl("txtSearch")
Line 14: Label1.Text = pp_Type.Text

Diane

 
Old March 3rd, 2007, 01:43 PM
Authorized User
 
Join Date: Sep 2006
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi diane!
Code for the first Page-Code within the Aspx Page
//-------------------------------------------------------------------

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<script runat="server">

Public ReadOnly Property pp_TextBox1() As TextBox
        Get
            Return TextBox1
        End Get
    End Property

    Public ReadOnly Property pp_Calendar1() As Calendar
        Get
            Return Calendar1
        End Get
    End Property

    ' this Concept called Properties

    'The above code creates an,let's call them - "instances" of a original objects.
    'Another words by creating these ReadOnly properties you will able
    'To work with this 2 objects from another page.
    'For example: If you going to use a pp_TextBox1 property in the Second page,
    'the pp_TextBox will basically equals to the TextBox1 of the first page.

    'You might think of the pp_TextBox1 like of some kind of a bridge, that connects
    'to the Physical TextBox1 in the first Page.

    'The truth is, this concept is a really basics of a programming of VB and of C#
    'This means that whenever we want to modify these controls later in the code, or
    'in another page like in Asp.net 2.0 we need to set up the PROPERTIES of these controls,
    'Otherwise these controls will be unreachable (In some cases)

    'Why we are using pp_? we could use any word we want.
    'pp_ means PreviousPage... So, by typing pp_TextBox1 in the second page you will clearly understand that
    'we are going to work with the PreviousPageTextBox. Very Simple!

    'Now, do not let the code to confuse you.
    'The below Button_Click Event means this:
    'When you Click the button, the Page is going to PostBack Information to It-Self.
    'He gonna show you Hello + text that you typed in
    'theTextBox1,and he will show you your selected Date from the calendar.

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        Label1.Text = "Hello " & TextBox1.Text & "<br />" & "Date Selected: " & Calendar1.SelectedDate.ToShortDateString()
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>VB Page1</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Enter your Name:<br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
            ErrorMessage="Please Enter your name"></asp:RequiredFieldValidator><br />
        <br />
        Please select Date.<br />
        <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
        <br />
            
        <asp:Label ID="Label1" runat="server"></asp:Label>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Submit Page to itself" OnClick ="Button1_Click" />&nbsp;
        <asp:Button ID="Button2" runat="server" Text="Submit to Page 2" PostBackUrl ="~/2.aspx" /><br />
        <br />

        <a href="2.aspx">Go Directly to the Page 2</a></div>
    </form>
</body>
</html>
---------------------------------------------------------------------------

this is Second Page

<%@ Page Language="VB" %>
<%@ PreviousPageType VirtualPath ="~/1.aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        Try
            If PreviousPage.IsCrossPagePostBack Then

                Label1.Text = "Hello " & PreviousPage.pp_TextBox1.Text & "!" & "<br />" & "Date Selected: " & PreviousPage.pp_Calendar1.SelectedDate.ToShortDate String() & "<br />" & "*You have reached the Second Page."

            Else

                Response.Redirect("1.aspx")
            End If
    

        Catch ex As System.NullReferenceException
            Response.Redirect("1.aspx")


        End Try
    

    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>This is Page 2</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
        <br />
        <br />
        <a href="1.aspx">Back</a></div>
    </form>
</body>
</html>
*******************************************
Copy this code and have fun :)
hope this will help.
Good luck!
 
Old March 3rd, 2007, 04:19 PM
Registered User
 
Join Date: Dec 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you! The code you have in red? This is the first I've seen anything like it in reference to the cross page post back. Would it go in the page load event in the code-behind on the first page?

Diane

 
Old March 6th, 2007, 08:35 AM
Authorized User
 
Join Date: Sep 2006
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Nope, No Need To Put the Properties in the Page_Load Event

This is the VB Code Behind. FIRST PAGE

************************************************** ********
Partial Class _Default
    Inherits System.Web.UI.Page
    Public ReadOnly Property pp_TextBox1() As TextBox
        Get
            Return TextBox1
        End Get
    End Property

    Public ReadOnly Property pp_Calendar1() As Calendar
        Get
            Return Calendar1
        End Get
    End Property
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Label1.Text = "Hello " & TextBox1.Text & "<br />" & "Date Selected: " & Calendar1.SelectedDate.ToShortDateString()
    End Sub

End Class

************************************************** ***********

VB Code behind for SECOND Page
-------------------------------------
Partial Class Page2
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If PreviousPage.IsCrossPagePostBack Then

                Label1.Text = "Hello " & PreviousPage.pp_TextBox1.Text & "!" & "<br />" & "Date Selected: " & PreviousPage.pp_Calendar1.SelectedDate.ToShortDate String() & "<br />" & "*You have reached the Second Page."

            Else

                Response.Redirect("1.aspx") 'Redirect Me To the First Page
            End If


        Catch ex As System.NullReferenceException
            Response.Redirect("1.aspx") 'Redirect Me To The First Page


        End Try
    End Sub
End Class
************************************************** *************





Similar Threads
Thread Thread Starter Forum Replies Last Post
PreviousPage.IsCrossPagePostBack workidoo BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 2 January 9th, 2010 04:29 PM
PreviousPage.IsCrossPagePostBack workidoo ASP.NET 3.5 Professionals 3 November 5th, 2008 05:26 PM
!!! ERROR In Wrox*PreviousPage.IsCrossPagePostBack dagad ASP.NET 2.0 Basics 1 March 22nd, 2007 09:57 AM
!!! ERROR In Wrox*PreviousPage.IsCrossPagePostBack dagad ASP.NET 2.0 Professional 0 September 28th, 2006 05:14 AM
FindControl Wervis C# 7 November 30th, 2005 09:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.