• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 512
  • Last Modified:

ASP.NET 4.00 Losing Public Property Values When clicking button to display next page?

Hi,
I have a page with a public property that stores the ID of the record I am displaying.  When I click a button to move off that page I want to read the value of that property so that I can use it to get the data on the next page. What I am finding is that the property holds the ID right up to the moment I click the button that triggers the switch to the next page.  I am using Server.Transfer("frmIncidentReportP3.aspx", True) in the click event of the button that transfers me from the first to the second page.

In the top of the new page I have this:
<%@ PreviousPageType VirtualPath="~/frmIncidentReportP2.aspx" %>

And in the Page_Load of the new page I have:

        If Not IsNothing(PreviousPage) Then
            If PreviousPage.IncidentReportID > 0 Then
                Me.IncidentReportID = PreviousPage.IncidentReportID
            End If
        End If

The issue is that just prior to clicking the button IncidentReportID in "frmIncidentReportP2.aspx" holds the ID value, the moment I click the button it zeros so that by the time I get to the new page its value is lost.

What am I missing, why does the click of the button on the first page cause the Public property to instantly lose its value?

Siv
0
Siv
Asked:
Siv
1 Solution
 
fostersoftCommented:
I think its because you page is an object. When you call the other page, the current page(object) is close and loose is property. If you want to keep the value you can put it in the viewstate, or you can pass the parameter in the url QueryString parameter.


I hope i help you. If you need more information i'll be happy to give you some example.
0
 
Paul JacksonSoftware EngineerCommented:
Using Server.Transfer you can access previous page control values but not a code-behind variable like you are trying to do.
A work-around would be to have a hidden field on your first page populated with the ID that you then can access it from the second page using findcontrol :

If Not IsNothing(PreviousPage) Then
    HiddenField hfIncidentReportId = (HiddenField)PreviousPage.FindControl
("hfIncidentReportId");
    If hfIncidentReportId.Value > 0 Then
         Me.IncidentReportID = hfIncidentReportId.Value
End If

Open in new window


Another alternative would to be to use a session object to store the ID.
0
 
SivAuthor Commented:
Paul,
That was the answer, I changed the code to:

        If Page.IsPostBack Then
            Exit Sub
        End If
        Dim hf As HiddenField
        If Not IsNothing(PreviousPage) Then
            hf = PreviousPage.FindControl("hfIncidentReportID")
            If hf.Value <> "" Then
                If hf.Value > 0 Then
                    Me.IncidentReportID = hf.Value
                End If
            Else
                Me.IncidentReportID = 0
            End If
        End If

        If Not IsNothing(PreviousPage) Then
            hf = PreviousPage.FindControl("hfActionID")
            If hf.Value <> "" Then
                If hf.Value > 0 Then
                    Me.ActionCalendarID = hf.Value
                End If
            Else
                Me.ActionCalendarID = 0
            End If
        End If

Now what I intended is working!
Brilliant!

Thanks for your help!

Siv
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
SivAuthor Commented:
Thanks, this lead me to the solution using PreviousPage.FindControl("HiddenControlName") now my ASP pages are behaving as expected.

Siv
0
 
Hiran DesaiSolution ArchitectCommented:
Yes @Paul Jackson is right.
You can have your data on server only if you have used ViewState(which is helpful only for single page) or You try to postback data to another page.

So to get the data in next page(second) you must have a control which can postback the data.
0
 
SivAuthor Commented:
@fostersoft,

Thanks for your reply, I found that Paul Jackson's solution worked best for me as I don't want to use QueryStrings.

Siv
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now