Solved

ASP.NET PreviousPage question

Posted on 2014-01-13
7
426 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to picture file 3 37
Set cookies HttpOnly and Secure 4 50
Asp.net mvc 5 5 13
Get list of word ducuments in a folder 10 9
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
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 …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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