Link to home
Create AccountLog in
Avatar of Siv
SivFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ASP.NET PreviousPage question

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
ASKER CERTIFIED SOLUTION
Avatar of Imran Javed Zia
Imran Javed Zia
Flag of Pakistan image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Siv

ASKER

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
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
Avatar of Siv

ASKER

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
Avatar of Siv

ASKER

Imran,
Thanks for your help and patience as I worked through this.
Server.Transfer seems to be the way to go in future.
Siv
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
Avatar of Siv

ASKER

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