Solved

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

Posted on 2014-01-21
6
499 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Foreword (May 2015) This web page has appeared at Google.  It's definitely worth considering! https://www.google.com/about/careers/students/guide-to-technical-development.html How to Know You are Making a Difference at EE In August, 2013, one …
Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
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…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

617 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