ASP .NET PreviousPage property not working

54 views Asked by At

I am trying cross page posting in asp web form using vb .net. My target page is not receiving the previous page and is returning null. Therefore I'm getting 'none' as output on my target page.

Source.aspx

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Source.aspx.vb" Inherits="WebApplication2.Source" EnableViewState="true"%>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="txtname" runat="server"></asp:TextBox><br />
            <asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="~/Target.aspx" />
        </div>
    </form>
</body>
</html>

Target.aspx

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Target.aspx.vb" Inherits="WebApplication2.Target" EnableViewState="true"%>
<%@ PreviousPageType VirtualPath="~/Source.aspx" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
    </form>
</body>
</html>

Target.aspx.vb

Public Class Target
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If PreviousPage IsNot Nothing Then
            Dim con As Control = PreviousPage.FindControl("txtname")
            Dim txt As TextBox = DirectCast(con, TextBox)
            Dim sol As String = txt.Text
            Response.Write(sol)
        Else
            Response.Write("none")
        End If
    End Sub

End Class

I've tried using EableViewState property true and false. But still no change in output. I'm expexting to get the input given in my textbox in source page to be displayed as output on target page.

1

There are 1 answers

0
Albert D. Kallal On

I would check your code behind on source page. I suspect you have a button event.

With your sample 2 pages, then even this code works:

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

    If PreviousPage IsNot Nothing Then


        Dim con As Control = PreviousPage.FindControl("txtname")
        Dim txt As TextBox = DirectCast(con, TextBox)
        Dim sol As String = txt.Text
        Response.Write(sol)

        Dim tbox As TextBox = PreviousPage.FindControl("txtname")
        Response.Write("<br/> Text Box = " & tbox.Text)


    Else
        Response.Write("none")
    End If
End Sub

result:

enter image description here

So, your posted example code does work.

So, not clear why your posted code does not work.

In fact, you can even leave out the page directive of this in Target.aspx

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

And, you find that the page still works.

Also, do note that pageload on the source page STILL triggers!!!

The reason is that a whole new instance of that page is created when you reference and use it in the target page.

So, say this in page load (source).

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

    Debug.Print("page load source")
    Debug.Print("Page load source post back = " & IsPostBack)
    Debug.Print("Page load source is cross post = " & IsCrossPagePostBack)

End Sub

Now, when I enter some text, and click the button?

You see this:

enter image description here

So, on-load of the page DOES fire again!! And note how cross post also is true.

So, keep in mind that everything in that source page, including up to page load complete event wise from the source page WILL trigger.....

So, note that your posted code for me works just fine. This suggests there are additional page events in the source page that on page load is doing something to the source page text box control of txtname.