[2005] Newbie question - Creating a "confirmation" form
Every time I think I have finished this project, they come up with another "good idea"....sigh
Anyway, I have a page that lets a user select software they wish to have installed. They select this from a listbox and then have to enter a Business Justification in a text box.
At the moment, when the Submit button is clicked it runs some code to
a) Create a call in our Helpdesk system
b) Add the user to an Active Directory group.
What they want now is for the user to be sent to a confirmation page that shows a summary of what they have selected and what will be done and then have to click either a PROCEED button (which will run the code) or a CANCEL button which will take them back to the main page.
I'm rather new to ASP so I'd like to know a couple of things
1) How do I pass the information on what the user has selected to the Confirmation page?
2) Should the user click CANCEL how do I return them to the main page without it reloading everything (i.e. it just goes back to the page in the state they left it, with all the data filled in etc)
3) How can I access methods (such as the logging function I have) on the main page from the confirmation page?
Re: [2005] Newbie question - Creating a "confirmation" form
Ok, I've figured on 1 & 2 on my own. However, #3 is stumping me. When the user clicks the "CONFIRM" button I need it to run code from the previous page. Is this possible?
Re: [2005] Newbie question - Creating a "confirmation" form
Just out of curiosity, how are you accomplishing the first two?
For number 3, if you have a function that you want more than one page to access, that function should be in a separate class file that can be used by any page in the website. In fact, as I'm sure you already know, ANY functions that can be shared across pages should be in a separate class file.
Re: [2005] Newbie question - Creating a "confirmation" form
Yes, I've come to that conclusion now as well. There's one slight issue in that the shared function I really need to use was one that let me output trace messages to a textbox on the mainform. Not very efficient I know but I have inherited this code and am under pressure to get it live.
I'll just have to move the textbox across to the confirmation form (as that's where things are likely to go wrong, if at all!)
As for question 1 I stored the values I needed in Session variables.
For option 2 I added this code to the HTML
Code:
<input type='button' onclick='history.go(-1)' value='Cancel' id='Cancelbutton' name='Cancelbutton'/>
Re: [2005] Newbie question - Creating a "confirmation" form
Your solution of choice for number one is naughty, naughty, naughty!
While session variables are easy to create, they can cause problems quickly. What if the app is on a web farm? If a session is created on Server A, but the user hits Server B on his next request, session = fail.
Furthermore, exactly how many session variables are you creating? Are you just storing the user's selection? If so, save yourself unnecessary server load and pass the value through the querystring. And if you send the value through the querystring, it doesn't matter which server the request hits.
Re: [2005] Newbie question - Creating a "confirmation" form
Yes I know it's naughty. There's not a chance this will be hosted on a serverfarm, it's going to be hosted on a single Windows 2003 server.
However, just to be compliant, how would I go about doing with the querystring? (yes, I am a COMPLETE noob with ASP.NET)
Re: [2005] Newbie question - Creating a "confirmation" form
What values are you passing to the next page? How many?
Re: [2005] Newbie question - Creating a "confirmation" form
They are all string fields and there's about 6 or 7 of them
Re: [2005] Newbie question - Creating a "confirmation" form
ASP.NET has a few methods that are better suited for this than session state and querystring. Its called Cross-Page Posting! For example, with this code in the main page, the confirmation page has access to the previous page and all its controls.
<asp:Button ID="Button1" PostBackUrl="~/confirmation.aspx" runat="server" Text="Confirm" />
"Redirecting Users to Another Page" http://msdn.microsoft.com/en-us/library/x3x8t37x.aspx
"Cross-Page Posting in ASP.NET Web Pages" http://msdn.microsoft.com/en-us/library/ms178139.aspx
Another option is to use the Wizard control and/or the MultiView control.
Re: [2005] Newbie question - Creating a "confirmation" form
Yes, I know about that and could never get it to work. I always get an "Object reference not set to an instance of an object" when trying to access any control.
Also, it's not just control values I need. There are also some variables set "User's Full Name, UserID" that are not assigned to any control.
Here's my button code
Code:
asp:Button ID="_SendRequest" type="button" runat="server"
Font-Bold="True" Text="Submit" Width="82px"
PostBackUrl="~/ConfirmationPage.aspx" />
Protected Sub SendRequest_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles _SendRequest.Click
If Page.IsValid Then
If Not _UserComputerList.SelectedItem Is Nothing Then
If Not _UserSoftwareList.SelectedItem Is Nothing Then
Response.Redirect("confirmationpage.aspx")
End If
End If
End If
End Sub
Then in the confirmationpage.aspx page I have
Code:
Dim TB As TextBox = CType(PreviousPage.FindControl("_UserConnected"), TextBox)
And that code fails. What am I doing wrong?
I have added <%@ PreviousPageType VirtualPath="~/Default.aspx" %> to the confirmation page
I have even tried adding a property to the Default.aspx page as so
Code:
Public ReadOnly Property SoftwareName() As ListBox
Get
Return _UserSoftwareList
End Get
End Property
But, it's never exposed in the Confirmationpage.aspx page. If I type PreviousPage. then the property does not show up in Intellisense. As far as I can see I've done what I'm supposed to do to get cross-page posting working!
Re: [2005] Newbie question - Creating a "confirmation" form
I guess I spoke too soon. I am getting rather frustrated now. Can someone please just tell me why this isn't working?
Properties from main page
Code:
Public ReadOnly Property SoftwareName() As String
Get
Return _UserSoftwareList.SelectedItem.Text
End Get
End Property
Public ReadOnly Property SoftwareID() As String
Get
Return _UserSoftwareList.SelectedItem.Value
End Get
End Property
Public ReadOnly Property Computer() As String
Get
Return _UserComputerList.SelectedItem.Text
End Get
End Property
Public ReadOnly Property Justification() As String
Get
Return _businessJustification.Text
End Get
End Property
Submit button has postback URL of confirmation page.
Confirmation page has previouspage directive set
Page Load of confirmation page is
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = Softwarename
Me.CancelButton.Attributes.Add("onClick", "javascript:window.history.go(-1);return false;")
Computer = PreviousPage.Computer
SoftwareID = PreviousPage.SoftwareID
Softwarename = PreviousPage.SoftwareName
Justification = PreviousPage.Justification
End Sub
Confirmation page doesn't load because of "Object reference not set to an instance of an object" errors
Re: [2005] Newbie question - Creating a "confirmation" form
I think there must be some larger problem at work here. I just tried, on the off-chance, to use a Querystring instead. Now, from what I understand, this is pretty basic stuff and there's no reason at all why it should not work.
So, submit button code on default form was
Code:
Protected Sub SendRequest_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles _SendRequest.Click
If Page.IsValid Then
If Not _UserComputerList.SelectedItem Is Nothing Then
If Not _UserSoftwareList.SelectedItem Is Nothing Then
Response.Redirect("confirmationpage.aspx?Justification=" + _businessJustification.Text)
End If
End If
Else
End If
End Sub
Then try to retrieve that value in confirmation page load and, lo and behold, there's nothing there. I also notice that the URL for confirmation page does not contain the querystring.
I'm stumped.
Re: [2005] Newbie question - Creating a "confirmation" form
EUREKA!
I've got the QueryString working (though not the Cross-Page posting). Can someone tell me why?
The problem with Querystring values not being passed is because my Submit button on the default form had UseSubmitBehaviour set to True. Setting it to False allowed the Querystring to work.
Re: [2005] Newbie question - Creating a "confirmation" form
If you are using a Master Page you need to get a reference to the ContentPlaceholder first. Something like
Code:
Confirm.aspx code behind
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If PreviousPage IsNot Nothing Then
If PreviousPage.IsCrossPagePostBack Then
Dim ph As ContentPlaceHolder = CType(PreviousPage.Master.FindControl("cphBodyContent"), ContentPlaceHolder)
Dim txt As TextBox
If Not ph Is Nothing Then
txt = ph.FindControl("txtQuantity")
If Not txt Is Nothing Then
lblQuantity.Text = "You have ordered " & txt.Text & " " & PreviousPage.SoftwareName & " items."
End If
End If
End If
End If
End Sub
Re: [2005] Newbie question - Creating a "confirmation" form
I'm not using a master page (as far as I am aware!). Anyway, I'm using the Querystring now as it's the only way I could get it to work
Re: [2005] Newbie question - Creating a "confirmation" form
AHA! I have got cross-page posting working finally. Well, sort of!
However, I've hit a stumbling block. In the Page_Load event of my confirmation form I can now retrieve the values of properties from the default page.
Like so....
Code:
Imports System.Data
Partial Class ConfirmationPage
Inherits System.Web.UI.Page
Dim Computer As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Cancel.Attributes.Add("onClick", "javascript:window.history.go(-1);return false;")
Me.Computer = PreviousPage.SelectedComputer
Now, the problem occurs when I click my button to submit the form. I need to re-use that data but I get that blasted "Object reference not set to an instance of an object" error and the highlighted line is from Page_Load!!
Code:
Me.Computer = PreviousPage.SelectedComputer
At the moment the only button only has the following code, which should out the value of Me.Computer to a textbox.
Code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
TraceDebug("computer = " + Computer)
End Sub
Why is this happening? Surely the me.computer variable was set during page_load? Or am I still in "Windows Forms" mode and ASP.NET behaves differently?
Re: [2005] Newbie question - Creating a "confirmation" form
Quote:
Or am I still in "Windows Forms" mode and ASP.NET behaves differently?
Yes, web development is different because HTTP is a disconnected environment. Basically, as soon as the page has been created and sent to the Client browser it is deleted from Server memory.
In order to pass data back and forth between client and server you need to store it somewhere. Typically, a Confirmation page shows the same information as the PreviousPage using read only or hidden controls. If the user then hits the Proceed button all the information is readily available on the PostBack.
Workflow of Confirmation page.
If PreviousPage is not nothing and IsCrossPagePostBack is True
Load Controls on Confirmation Page with info from PreviousPage.
Else If IsPostBack is true and user clicked Proceed button
Save Information
Else If IsPostBack is true and user clicked Cancel button
Go Back to Previous Page
Else
Invalid state 'ie user is trying to access the confirmation.aspx page directly.
Re: [2005] Newbie question - Creating a "confirmation" form
So how can I access the variables I created during form load once the user clicks the Submit button on the confirmation page
Sorry for being dense, this is my first dip in to ASP infested waters. You are being extremely patient!
Re: [2005] Newbie question - Creating a "confirmation" form
Quote:
So how can I access the variables I created during form load once the user clicks the Submit button on the confirmation page
You cannot use variables. Their values are gone once the page is sent to the browser the first time, before the Submit button is clicked.
Again, the easiest solultion is to simply transfer the data you need into controls on the Confirmation page from the PreviousPage. This is a chance to show the users the information they entered and satisfies the requirement you mentioned in the original post.
Quote:
the user to be sent to a confirmation page that shows a summary of what they have selected and what will be done
Then if the Submit/Proceed button is clicked the data is all there on PostBack and can easily be saved to the database. lblComputer and lblSoftware could be ASP.NET Label controls on the Confirmation page.
Code:
Imports System.Data
Partial Class ConfirmationPage
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.Cancel.Attributes.Add("onClick", "javascript:window.history.go(-1);return false;")
If PreviousPage IsNot Nothing Then
If PreviousPage.IsCrossPagePostBack Then
lblSoftware.Text = PreviousPage.SoftwareName
lblComputer.Text = PreviousPage.SelectedComputer
End If
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
TraceDebug("computer = " + lblComputer.Text)
End Sub
There are other options of course. One is to directly utilize Page.ViewState instead of Controls.
Re: [2005] Newbie question - Creating a "confirmation" form
Ah, OK. Some of the values the users don't need to see but I guess those controls could just be hidden.
I shall give this a try. Thank you for all your advice. As I said, I'm obviously thinking in a "Windows Forms" mentality still. This whole "session" business is all a bit confusing ;)