I was wondering if anyone can help me with this. I want to have a form that the user fills out, then when they submit it, it displays the values on the page in a summary with two buttons: Submit and Cancel. The Submit button directs the user to the main page, while the Cancel button takes them back to the form, with the values entered still filled in. How do I do this? I have a simple form that shows the values on the next page, but I don't know how to make it go back with the values filled in. Maybe I am doing the whole page wrong. Can someone tell me how to do this?

Recommended Answers

All 8 Replies

There are 2 ways to do this: using same page 2 panels or 2 pages for
1. data entry and 2. Confirmation. You can hide-undhide panels (panelConfirm.visible= true) if you decide only one page.
For one page solution:
By default, show data-entry panel , hide other. On submit, read text box etc and populate confirmation labels etc. Hide 1st and show other.
Now on canel or submit you show panels or server.transfer
If this helps, please mark thread as solved.

I didn't quite understand what you wrote. I do need two pages:

page one:
form

page two:
summary of form values,
confirm button (takes to default.aspx),
cancel button (takes back to form with values still filled in)

I have these pages currently:

page one:

<!-- UserForm.aspx -->
<%@ Page Language="VB" ClassName="SenderClass" %>

<script runat="server">
	' Readonly property for name
	Public ReadOnly Property Name() As String
		Get
			Return USerName.Text
		End Get
	End Property

	'Readonly Property for phone
	Public ReadOnly Property Phone() As String
		Get
			Return UserPhone.Text
		End Get
	End Property

	'Event to transfer page control to Result.aspx
	Sub Page_Transfer(sender As Object, e As EventArgs)
		Server.Transfer("Result.aspx")
	End sub
</script>

<html>
<head>
</head>
<body>
	<form id="Form1" runat="server">
		User Name: 
		<asp:TextBox ID="UserName" runat="server" />
		Phone: 
		<asp:TextBox ID="UserPhone" runat="server" /><br>
		<asp:Button ID="Button1" Text="Submit" OnClick="Page_Transfer"
			runat="server" />
	</form>
</body>
</html>

page two:

<!-- Result.aspx -->
<%@ Page Language="VB" %>
<%@ Reference Page="UserForm.aspx" %>

<script runat="server">
    Dim result As SenderClass

	Sub Page_load(obj as Object, e as EventArgs)
		Dim content As String

		If Not IsPostBack Then
			result = CType(Context.Handler, SenderClass)
			content = "Name: " + result.Name + "<br>" _
				+ "Phone: " + result.Phone
			Label1.text = content
		End If
    End Sub

</script>
<html>
<head>
</head>
<body>
<h1>Summary:</h1>
	<i><form id="Form1" runat="server">
		<asp:Label id="Label1" runat="server" /></i><br /><br />
    <asp:Button ID="Confirm" runat="server" Text="Confirm" 
            PostBackUrl="default.aspx" />
		&nbsp;
    <asp:Button ID="Cancel" runat="server" Text="Cancel" /></form>
</body>
</html>

Page two displays the summary, and has the confirm and cancel button. I have confirm set to go to the default.aspx page, but how do I get the cancel button to take the user back to the form (userform.aspx) with the form fields filled in with the previous data the user had filed in?

You can get the values of the controls from the previous page using a feature called Cross Page Posting.

Refer this link. Cross-Page Posting in ASP.NET Web Pages

Otherwise you need to store the values in Session and retrieve it in the form page when Cancel is clicked.

The pages I have already does display the form values on the next page. What I need to know how to do is when they press cancel, to have the user be redirected back to the form from the previous page and to have the form fields be populated with the values they had previously filled in. How can I do that? Or is the Cross-Page Posting the way to do it?

If you redirect from the form page to another page, the values in the form page will be lost. If you want to persist the values betweeb page redirections, you need to store it in Session.

Altenatively you can also use the cross-page posting feature to get the values of the previous page.

In your scenario, you are contenating the values in Label control in Result.aspx.

If you use cross page posting there, you can only get the value of Label in the form page again. Then you need to parse the values from the Label and display it in UserForm.aspx. Therefore you can display the values in separate label controls in result page. It will help you to get values from those labels separately during cross page posting.

Also you can consider storing and retriving values from Session.

If I chose to use my script the way I currently have it, how would I go about saving the values into sessions? And then how would I pull those values back into the form when the user goes back to the form? I'm trying to find some tutorials on this, but it is confusing to me.

I have changed your code to store and retrieve the values from Session.

<!-- UserForm.aspx -->
<%@ Page Language="VB" ClassName="SenderClass" %>

<script runat="server">
	' Readonly property for name
	Public ReadOnly Property Name() As String
		Get
			Return USerName.Text
		End Get
	End Property

	'Readonly Property for phone
	Public ReadOnly Property Phone() As String
		Get
			Return UserPhone.Text
		End Get
	End Property

	'Event to transfer page control to Result.aspx
	Sub Page_Transfer(sender As Object, e As EventArgs)
		Server.Transfer("Result.aspx")
    End Sub
    
    
   Sub Page_load(ByVal obj As Object, ByVal e As EventArgs)
        
        If Not IsPostBack Then
            UserName.Text = Session("Name")
            UserPhone.Text = Session("Phone")
        End If
        
    End Sub

</script>

<html>
<head>
</head>
<body>
	<form id="Form1" runat="server">
		User Name: 
		<asp:TextBox ID="UserName" runat="server" />
		Phone: 
		<asp:TextBox ID="UserPhone" runat="server" /><br>
		<asp:Button ID="Button1" Text="Submit" OnClick="Page_Transfer"
			runat="server" />
	</form>
</body>
</html>
<!-- Result.aspx -->
<%@ Page Language="VB" %>
<%@ Reference Page="UserForm.aspx" %>

<script runat="server">
    Dim result As SenderClass

	Sub Page_load(obj as Object, e as EventArgs)
		Dim content As String

		If Not IsPostBack Then
			result = CType(Context.Handler, SenderClass)
			content = "Name: " + result.Name + "<br>" _
				+ "Phone: " + result.Phone
            Label1.Text = content
            
            Session("Name") = result.Name
            Session("Phone") = result.Phone
            
            
       End If
    End Sub
    Sub Cancel_Click(ByVal sender As Object, ByVal e As EventArgs)
        Server.Transfer("UserForm.aspx")
    End Sub

</script>
<html>
<head>
</head>
<body>
<h1>Summary:</h1>
	<i><form id="Form1" runat="server">
		<asp:Label id="Label1" runat="server" /></i><br /><br />
    <asp:Button ID="Confirm" runat="server" Text="Confirm" 
            PostBackUrl="default.aspx" />
		&nbsp;
    <asp:Button ID="Cancel" runat="server" Text="Cancel" OnClick="Cancel_Click"/></form>
</body>
</html>
commented: Exactly what I needed! +2

Thank you so much! I have one more question. How can I get the value of a checkbox to display on my page and then back into the form, if I have multiple checkboxes?

Example:

Pets Owned:

<asp:CheckBoxList ID="CheckBoxList1" runat="server" 
            RepeatDirection="Horizontal">
            <asp:ListItem>Cat</asp:ListItem>
            <asp:ListItem>Dog</asp:ListItem>
            <asp:ListItem>Bird</asp:ListItem>
            <asp:ListItem>Fish</asp:ListItem>
            <asp:ListItem>Reptiles</asp:ListItem>
        </asp:CheckBoxList>

When I use the method I have been using, it will only return the value of one selected checkbox. How can I do this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.