Solved

ASP.NET PreviousPage question

Posted on 2014-01-13
7
443 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

710 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