Solved

ASP.NET PreviousPage question

Posted on 2014-01-13
7
434 Views
Last Modified: 2014-01-14
Hi,
I have a first page with a ListView on it that has a "select" entry in the first column that uses a LinkButton which when clicked triggers the SelectedIndexChanging event.  A code behind routine stores the Database ID field of the row being selected in a public Property of the first page called "SelID". That's working fine.

After storing the SelID I then go to the second page (using a response.redirect).  In the Page_Load of the second page I then want to pick up the SelID from the previous page and use that to populate my screen from the database. For some unknown reason when I test the PreviousPage it's always set at Nothing?

I have added the following line to the top of the second page's source:

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

Is it down to the fact that I have used a response.redirect or am I missing something else?

Any Help appreciated,

Siv
0
Comment
Question by:Siv
  • 4
  • 3
7 Comments
 
LVL 16

Accepted Solution

by:
Imran Javed Zia earned 500 total points
ID: 39778528
Hi,

PreviousPage only work with Server.Transfer and/or cross-page-posting, please refer to following
http://msdn.microsoft.com/en-us/library/system.web.ui.page.previouspage.aspx


secondly Make sure, your virtual path and its name is correct.
Please go to following:
http://msdn.microsoft.com/en-us/library/6c3yckfw(v=vs.90).aspx

Thanks
0
 

Author Comment

by:Siv
ID: 39778890
Imran,
I changed my code in the SelectedIndexChanging event from:

        Dim item As ListViewItem
        Dim lbl As Label
        'Store the Selected Item in the public property.
        item = lstIncidentReports.Items(e.NewSelectedIndex)
        lbl = item.FindControl("IncidentReportIDLabel")
        SelID = CType(lbl.Text, Integer)
        Response.Redirect("frmIncidentReportP2.aspx", True)

To:
        Dim item As ListViewItem
        Dim lbl As Label
        'Store the Selected Item in the public property.
        item = lstIncidentReports.Items(e.NewSelectedIndex)
        lbl = item.FindControl("IncidentReportIDLabel")
        SelID = CType(lbl.Text, Integer)
        Server.Transfer("frmIncidentReportP2.aspx", True)

However when I ran the code and get this error on the Server.transfer line:

System.Web.HttpException was unhandled by user code
  ErrorCode=-2147467259
  HResult=-2147467259
  Message=Error executing child request for frmIncidentReportP2.aspx.
  Source=System.Web
  WebEventCode=0
  StackTrace:
       at System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride)
       at System.Web.HttpServerUtility.Execute(String path, TextWriter writer, Boolean preserveForm)
       at System.Web.HttpServerUtility.Transfer(String path, Boolean preserveForm)
       at _Default.lstIncidentReports_SelectedIndexChanging(Object sender, ListViewSelectEventArgs e) in D:\Dev\AIPWeb\frmIncidentReport.aspx.vb:line 127
       at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
       at System.Web.UI.WebControls.ListView.OnSelectedIndexChanging(ListViewSelectEventArgs e)
       at System.Web.UI.WebControls.ListView.HandleSelect(Int32 itemIndex)
       at System.Web.UI.WebControls.ListView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)
       at System.Web.UI.WebControls.ListView.OnBubbleEvent(Object source, EventArgs e)
       at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
       at System.Web.UI.WebControls.ListViewDataItem.OnBubbleEvent(Object source, EventArgs e)
       at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
       at System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
       at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: System.Web.HttpUnhandledException
       ErrorCode=-2147467259
       HResult=-2147467259
       Message=Exception of type 'System.Web.HttpUnhandledException' was thrown.
       Source=System.Web
       WebEventCode=0
       StackTrace:
            at System.Web.UI.Page.HandleError(Exception e)
            at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
            at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
            at System.Web.UI.Page.ProcessRequest()
            at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
            at System.Web.UI.Page.ProcessRequest(HttpContext context)
            at ASP.frmincidentreportp2_aspx.ProcessRequest(HttpContext context)
            at System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride)
       InnerException: System.ArgumentOutOfRangeException
            HResult=-2146233086
            Message='cboCustomerNames' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
            ParamName=value
            Source=System.Web
            StackTrace:
                 at System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource)
                 at System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e)
                 at System.Web.UI.WebControls.ListControl.PerformSelect()
                 at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
                 at System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()
                 at System.Web.UI.WebControls.ListControl.OnPreRender(EventArgs e)
                 at System.Web.UI.Control.PreRenderRecursiveInternal()
                 at System.Web.UI.Control.PreRenderRecursiveInternal()
                 at System.Web.UI.Control.PreRenderRecursiveInternal()
                 at System.Web.UI.Control.PreRenderRecursiveInternal()
                 at System.Web.UI.Control.PreRenderRecursiveInternal()
                 at System.Web.UI.Control.PreRenderRecursiveInternal()
                 at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
            InnerException:

If I step the code It seems to get to the frmIncidentReportP2.aspx page and this time the PreviousPage is not nothing so the Page_Load of the new page works but then it returns to this routine with an error?

The code in the page_load is this:

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If IsNothing(Session("LoggedIn")) Or Session("LoggedIn") = False Then
            Response.Redirect("NotLoggedIn.aspx")
        Else
            If Not IsNothing(PreviousPage) Then
                If PreviousPage.SelID <> 0 Then
                    'We are showing an existing item.
                    PopulateScreen(PreviousPage.SelID)
                Else
                    'we are not
                    ClearScreen()
                End If
            End If
        End If
    End Sub

interestingly the above code executes and calls a routine to populate the screen using the SelID from the first page so it's all working, it just errors when that completes and I would expect the second page to appear?

So we get to the new page, execute the stuff  in the Page_Load and when that completes instead of control being passed to the new page the code goes back to the previous page and errors on the Server.Transfer call??

Any idea what I am doing wrong?

Siv
0
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 39778929
Dear Siv,

As per provided Stack trace, there is some issue with cboCustomerNames:

InnerException: System.ArgumentOutOfRangeException
            HResult=-2146233086
            Message='cboCustomerNames' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
            ParamName=value
            Source=System.Web

Thanks
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:Siv
ID: 39778980
Imran,

I saw that but assumed that as the error was occurring on the call to the Server.Transfer it was something to do with the transfer not the issue on the population of the new page. If that's what it is then I am OK as I know what that issue is.

Thanks for your help, I can now move on again.

What you have shown me has cured the main issue of the PreviousPage being set to nothing.
I will award you the points for solving the problem for me.

If you have time could you answer one more question; in your opinion is there any benefit or penalty of using Server.transfer as opposed to response.redirect? Given that this method works I am now considering using Server.Transfer all the time as often I need to access properties from one page on another.

I prefer not to use QueryStrings as a) it looks messy for the user and more importantly b) seems to be a bit of a security risk as it may expose some of the internal workings of a page.

Siv
0
 

Author Closing Comment

by:Siv
ID: 39778982
Imran,
Thanks for your help and patience as I worked through this.
Server.Transfer seems to be the way to go in future.
Siv
0
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 39778992
My pleasure, Hope you may find following helpful:

http://www.codeproject.com/Questions/420469/Response-Redirect-and-Server-Transfer
stackoverflow.com/questions/6778870/difference-between-response-redirect-and-server-transfer

Thanks
0
 

Author Comment

by:Siv
ID: 39779011
Imran,

Thanks, I'll stick with Server.transfer as it seems better all round as long as your not going to a different server which I never will be.

Siv
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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