Solved

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

Posted on 2014-01-21
6
489 Views
Last Modified: 2014-01-21
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
Comment
Question by:Siv
6 Comments
 
LVL 1

Expert Comment

by:fostersoft
ID: 39798978
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
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 39799010
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
 

Author Comment

by:Siv
ID: 39799075
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Closing Comment

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

Siv
0
 
LVL 4

Expert Comment

by:ItWorked
ID: 39799079
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
 

Author Comment

by:Siv
ID: 39799081
@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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vb.net 2 37
Data is not showing from images 15 36
Close tabpage of custom control 9 23
Show/Hide ASP.NET controls with JQuery best practices 3 0
A Change in PHP Behavior with Session Write Short Circuit (http://php.net/manual/en/book.session.php#116217) (Winter 2014)** With the release of PHP 5.6 the session handler changed in a way that many think should be considered a bug.  See the note …
Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now