Solved

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

Posted on 2014-01-21
6
486 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Closing Comment

by:Siv
Comment Utility
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
Comment Utility
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
Comment Utility
@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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video teaches users how to migrate an existing Wordpress website to a new domain.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

772 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

11 Experts available now in Live!

Get 1:1 Help Now