3

I'm trying to make a confirmation page for a reservation form. And I'm having trouble getting the value from my previous pages controls. I have everything running at server and I have my submit button's PostBackURL pointing to the confirmation page. Everything compiles, but when i put in info and hit submit the only thing that pops up are my booleans, which don't really reflect the true value because of my if-elses. I've tried a million different casting/value grabbing attempts and this current form is the closest I've come. Any help would be greatly appreciated. Also how do I get a next line like \n in the String.format? \n doesn't seem to work. The confirmation page looks like this

ConfirmationPage.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
            string numNights = Convert.ToString((TextBox)PreviousPage.FindControl("txtNights.Text"));
            string arrivalDate = Convert.ToString((TextBox)PreviousPage.FindControl("txtArrivalDate.Text"));
            string numAdults = Convert.ToString((DropDownList)PreviousPage.FindControl("ddlAdults.SelectedValue.Text"));
            string numChildren = Convert.ToString((DropDownList)PreviousPage.FindControl("ddlChildren.SelectedValue.Text"));
            string roomTypeBusiness = "false";
            string roomTypeSuite = "false";
            string roomTypeStandard = "false";
            string bedTypeKing = "false";
            string bedTypeDouble = "false";
            string smokingOption = "false";

            if (Convert.ToBoolean(PreviousPage.FindControl("rdoBusiness.Checked")))
                roomTypeBusiness = "true";
            else if (Convert.ToBoolean(PreviousPage.FindControl("rdoSuite.Checked")))
                roomTypeSuite = "true";
            else
                roomTypeStandard = "true";

            if (Convert.ToBoolean(PreviousPage.FindControl("rdoKing.Checked")))
                bedTypeKing = "true";
            else
                bedTypeDouble = "true";

            string specialRequests = Convert.ToString((TextBox)PreviousPage.FindControl("txtSpecialRequests.Text"));
            string name = Convert.ToString((TextBox)PreviousPage.FindControl("txtName.Text"));
            string email = Convert.ToString((TextBox)PreviousPage.FindControl("txtEmail.Text"));

            lblResults.Text = String.Format("Arrival Date: {0} \r\n Number of Nights: {1} \n Number of Adults: \r {2} Number of Children: {3} Business Room: {4} Suite Room: {5} Standard Room: {6} King Bed: {7} Double Bed: {8} Smoking: {9} Special Requests: {10} Name: {11} E-mail: {12}",
            arrivalDate, numNights, numAdults, numChildren, roomTypeBusiness, roomTypeSuite, roomTypeStandard, bedTypeKing, bedTypeDouble, smokingOption, specialRequests, name, email);
    }
}

The previous page's aspx is

<%@ Page Title="" Language="C#" MasterPageFile="~/ResultsPageHTML5MasterPage/ResultsPageHTML5MasterPage.master" AutoEventWireup="true" CodeFile="ReservationForm_SqlInject.aspx.cs" Inherits="Default3" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <h1>My Home Away From Home Hotels</h1>
        <h2>Where you&rsquo;re always treated like family</h2>
    <p>
        Arrival date: &nbsp;

        <asp:TextBox ID="txtArrivalDate" runat="server" Width="75px">TextBoxHere</asp:TextBox>
        &nbsp;

        <asp:ImageButton ID="ibtnCalendar" runat="server" ImageAlign="Top" ImageUrl="~/Images/Calendar.bmp" OnClick="ibtnCalendar_Click" />
    </p>
    <p>
        <asp:Calendar ID="clnArrival" runat="server" Visible="False" 
            OnSelectionChanged="clnArrival_SelectionChanged" BackColor="White" 
            BorderColor="Black" DayNameFormat="Shortest" Font-Names="Times New Roman" 
            Font-Size="10pt" ForeColor="Black" Height="220px" NextPrevFormat="FullMonth" 
            TitleFormat="Month" Width="400px">
            <DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" 
                ForeColor="#333333" Height="10pt" />
            <DayStyle Width="14%" />
            <NextPrevStyle Font-Size="8pt" ForeColor="White" />
            <OtherMonthDayStyle ForeColor="#999999" />
            <SelectedDayStyle BackColor="#CC3333" ForeColor="White" />
            <SelectorStyle BackColor="#CCCCCC" Font-Bold="True" Font-Names="Verdana" 
                Font-Size="8pt" ForeColor="#333333" Width="1%" />
            <TitleStyle BackColor="Black" Font-Bold="True" Font-Size="13pt" 
                ForeColor="White" Height="14pt" />
            <TodayDayStyle BackColor="#CCCC99" />
        </asp:Calendar>
    </p>
    <p>
        Number of nights: &nbsp;
        <asp:TextBox ID="txtNights" runat="server" Width="45px"></asp:TextBox>
    </p>
    <p>
        Number of Adults: &nbsp;
        <asp:DropDownList ID="ddlAdults" runat="server" Width="50px"></asp:DropDownList>
        &nbsp;&nbsp;
        Number of Children: &nbsp;
        <asp:DropDownList ID="ddlChildren" runat="server" Width="50px"></asp:DropDownList>
    </p>
    <h3>Preferences</h3>
    <p>
        Room Type: &nbsp;
        <asp:RadioButton ID="rdoBusiness" runat="server" GroupName="Room" Text="Business" />
        <asp:RadioButton ID="rdoStandard" runat="server" GroupName="Room" Text="Standard" />
        <asp:RadioButton ID="rdoSuite" runat="server" GroupName="Room" Text="Suite" />
    </p>
    <p>
        Bed Type:
        &nbsp;
        <asp:RadioButton ID="rdoKing" runat="server" GroupName="Bed" Text="King" />
        &nbsp;
        <asp:RadioButton ID="rdoDouble" runat="server" GroupName="Bed" Text="Double" />
    </p>
    <p>
        <asp:CheckBox ID="chkSmoking" runat="server" Text="Smoking" />
    </p>

    <p>
        Special Requests
    </p>
    <p>
        <asp:TextBox ID="txtSpecialRequests" runat="server" Rows="4" TextMode="MultiLine" Width="250px"></asp:TextBox>
    </p>
    <h3>Contact Information</h3>
    <p>Name:
    </p>

    <p>
        <asp:TextBox ID="txtName" runat="server" Width="320px"></asp:TextBox>
    </p>
    <p>
        Email
    </p>
    <p>
        <asp:TextBox ID="txtEmail" runat="server" Width="320px"></asp:TextBox>
    </p>

    <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
         Width="80px" PostBackUrl="~/ConfirmationPage.aspx" />
    &nbsp;
    <asp:Button ID="btnClear" runat="server" Text="Clear" Width="80px" OnClick="btnClear_Click" />

    <p>
        <asp:Button ID="btnInstert" runat="server" Text="Insert" OnClick="btnInstert_Click" Width="80px" />
    </p>
    <p>
        <asp:Label ID="lblMessage" runat="server" Text="Label"></asp:Label>
    </p>
</asp:Content>
Andrew-Saarima
  • 270
  • 3
  • 9
  • I think you should store the values into some Session or you could use Querystring for small pieces of data and just use them on the other page instead of using `PreviousPage.FindControl`. – Dhrumil Dec 03 '15 at 05:53

2 Answers2

2

Using PreviousPage.FindControl() one can only access PreviousPage data when someone navigates from Page A —> Transfer Data —> Page B. If one request a page directly, then PreviousPage renders null reference.

Here, In your case once navigate to page Default2you only persist value at page load. After you click on submit your Default.aspx calls itself and PreviousPage loses it data. Also the way you are finding control is incorrect that way you will get NULLREFERENCE.

More Info

If there a master page in your web application then one cannot directly access previous page control just by by calling PreviousPage.FindControl() one have to first find ContentHolder control that resided in master page and then one can find child controls in child webpages.

Solution:-

Use IsPostBack property

how can I get the value of due_date in javascript

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string numNights = Convert.ToString(((TextBox)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("txtNights")).Text);
            string arrivalDate = Convert.ToString(((TextBox)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("txtArrivalDate")).Text);
            string numAdults = Convert.ToString(((DropDownList)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("ddlAdults")).SelectedValue);
            string numChildren = Convert.ToString(((DropDownList)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("ddlChildren")).SelectedValue);
            string roomTypeBusiness = "false";
            string roomTypeSuite = "false";
            string roomTypeStandard = "false";
            string bedTypeKing = "false";
            string bedTypeDouble = "false";
            string smokingOption = "false";

            if (Convert.ToBoolean(((RadioButton)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("rdoBusiness")).Checked"))
                roomTypeBusiness = "true";
            else if (Convert.ToBoolean(((RadioButton)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("rdoSuite")).Checked"))
                roomTypeSuite = "true";
            else
                roomTypeStandard = "true";

            if (Convert.ToBoolean(((RadioButton)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("rdoKing")).Checked))
                bedTypeKing = "true";
            else
                bedTypeDouble = "true";

            string specialRequests = Convert.ToString(((TextBox)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("txtSpecialRequests")).Text);
            string name = Convert.ToString(((TextBox)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("txtName")).Text);
            string email = Convert.ToString(((TextBox)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("txtEmail")).Text);

            /* Splitting data into newline */

            string info = String.Format("Arrival Date: {0}, Number of Nights: {1}, Number of Adults: {2}, Number of Children: {3}, Business Room: {4}, Suite Room: {5}, Standard Room: {6}, King Bed: {7}, Double Bed: {8}, Smoking: {9}, Special Requests: {10}, Name: {11}, E-mail: {12}",
            arrivalDate, numNights, numAdults, numChildren, roomTypeBusiness, roomTypeSuite, roomTypeStandard, bedTypeKing, bedTypeDouble, smokingOption, specialRequests, name, email);

            String[] tokens = info.Split(new[]{","}, StringSplitOptions.RemoveEmptyEntries);
            lblResults.Text = string.Join("<br />", tokens);        
            }
        }
    }
Suprabhat Biswal
  • 3,033
  • 1
  • 21
  • 29
  • When I try it like this I get "Error 1 'System.Web.UI.Control' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'System.Web.UI.Control' could be found (are you missing a using directive or an assembly reference?) " – Andrew-Saarima Dec 03 '15 at 06:33
  • When I run it this way I get "A first chance exception of type 'System.NullReferenceException' occurred in App_Web_tbejxdai.dll" It points it at string numNights = Convert.ToString(((TextBox)PreviousPage.FindControl("txtNights")).Text); – Andrew-Saarima Dec 03 '15 at 07:07
  • Are you using Master page in your web application ? – Suprabhat Biswal Dec 03 '15 at 07:14
  • Yes, I am. And I'll be the first to admit that I have no idea what I'm doing with it. This is for a class, and I basically blindly followed the teachers instructions for Master page. – Andrew-Saarima Dec 03 '15 at 07:16
  • 1
    I have updated my answer, If your `ContentPlaceHolder` control suppose to have Id as `ContentPlaceHolder1` then apply same else change name `ContentPlaceHolder1` with the ID. – Suprabhat Biswal Dec 03 '15 at 07:21
  • Yes, that did it! You are a life saver! Thanks! – Andrew-Saarima Dec 03 '15 at 07:25
  • Welcome, Pleasure helping you out. – Suprabhat Biswal Dec 03 '15 at 07:26
0

Your code is not correct. you have to use the controlId and then cast the control to correct type and then use the respective properties.Also for dropdown list you have to use SelectedItem.Text instead of SelectedValue.Text

Following is the modified code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
        protected void Page_Load(object sender, EventArgs e)
        {
            string numNights = Convert.ToString(((TextBox)PreviousPage.FindControl("txtNights")).Text);
            string arrivalDate = Convert.ToString(((TextBox)PreviousPage.FindControl("txtArrivalDate")).Text);

            string numAdults = Convert.ToString(((DropDownList)PreviousPage.FindControl("ddlAdults")).SelectedItem.Text);
            string numChildren = Convert.ToString(((DropDownList)PreviousPage.FindControl("ddlChildren")).SelectedItem.Text);
            string roomTypeBusiness = "false";
            string roomTypeSuite = "false";
            string roomTypeStandard = "false";
            string bedTypeKing = "false";
            string bedTypeDouble = "false";
            string smokingOption = "false";

            if (Convert.ToBoolean(((RadioButton)PreviousPage.FindControl("rdoBusiness")).Checked))
                roomTypeBusiness = "true";
            else if (Convert.ToBoolean(((RadioButton)PreviousPage.FindControl("rdoSuite")).Checked))
                roomTypeSuite = "true";
            else
                roomTypeStandard = "true";

            if (Convert.ToBoolean(((RadioButton)PreviousPage.FindControl("rdoKing")).Checked))
                bedTypeKing = "true";
            else
                bedTypeDouble = "true";

            string specialRequests = Convert.ToString(((TextBox)PreviousPage.FindControl("txtSpecialRequests")).Text);
            string name = Convert.ToString(((TextBox)PreviousPage.FindControl("txtName")).Text);
            string email = Convert.ToString(((TextBox)PreviousPage.FindControl("txtEmail")).Text);

            lblResults.Text = String.Format("Arrival Date: {0} \r\n Number of Nights: {1} \n Number of Adults: \r {2} Number of Children: {3} Business Room: {4} Suite Room: {5} Standard Room: {6} King Bed: {7} Double Bed: {8} Smoking: {9} Special Requests: {10} Name: {11} E-mail: {12}",
            arrivalDate, numNights, numAdults, numChildren, roomTypeBusiness, roomTypeSuite, roomTypeStandard, bedTypeKing, bedTypeDouble, smokingOption, specialRequests, name, email);
        }

}
Avinash Jain
  • 7,200
  • 2
  • 26
  • 40
  • When I try this way I get a "A first chance exception of type 'System.NullReferenceException' occurred in App_Web_stp3fzm4.dll" error – Andrew-Saarima Dec 03 '15 at 06:19
  • the PreviousPage property only have value when someone navigate from other page in your application to this page. And also make sure ViewState is enable for both pages – Avinash Jain Dec 03 '15 at 06:28
  • I run my ReservationForm (which I updated the original question to show) and I fill out all the info and click the submit button which has a PostBackUrl to my ConfirmationPage.aspx. Where do I enable ViewState? – Andrew-Saarima Dec 03 '15 at 06:36