C# 从PreviousPage.FindControl获取值

C# 从PreviousPage.FindControl获取值,c#,asp.net,visual-studio-2013,findcontrol,C#,Asp.net,Visual Studio 2013,Findcontrol,我正在为预订表做确认页。我很难从以前的页面控件中获取值。我的一切都在服务器上运行,我的提交按钮的PostBackURL指向确认页面。一切都在编译,但当我输入信息并点击提交时,唯一弹出的是我的布尔值,因为我的if-else,它不能真正反映真正的价值。我已经尝试了一百万次不同的施法/抢值尝试,而这是我最近的一次。任何帮助都将不胜感激。 另外,如何获得String.format中的下一行,如\n\n似乎不起作用。 确认页面如下所示 ConfirmationPage.aspx.cs using Sys

我正在为预订表做确认页。我很难从以前的页面控件中获取值。我的一切都在服务器上运行,我的提交按钮的PostBackURL指向确认页面。一切都在编译,但当我输入信息并点击提交时,唯一弹出的是我的布尔值,因为我的if-else,它不能真正反映真正的价值。我已经尝试了一百万次不同的施法/抢值尝试,而这是我最近的一次。任何帮助都将不胜感激。 另外,如何获得String.format中的下一行,如\n\n似乎不起作用。 确认页面如下所示

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);
    }
}
上一页的aspx是

<%@ 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>

我的家远离家乡酒店
您在哪里;你总是像家人一样被对待

抵达日期:
文本框

夜数:

成人人数: 儿童人数:

偏好 房间类型:

床型:

特殊要求

联系方式 姓名:

电子邮件


使用
PreviousPage.FindControl()
只有当有人从
A页->传输数据->B页导航时,才能访问PreviousPage数据。若直接请求一个页面,则PreviousPage呈现空引用

在这里,在您的情况下,一旦导航到页面
Default2
,您只会在页面加载时保留值。在您点击提交您的
Default.aspx
后,它会自动调用自己的
PreviousPage
并丢失它的数据。此外,您查找控件的方式不正确,这样您将获得
NULLREFERENCE

更多信息

如果web应用程序中存在母版页,则不能仅通过调用
PreviousPage.FindControl()
直接访问上一页控件。必须首先找到母版页中的
ContentHolder
控件,然后才能在子网页中找到子控件

解决方案:-

使用属性

如何在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);        
            }
        }
    }
public部分类Default2:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
字符串numNights=Convert.ToString(((文本框)上一页.Master.FindControl(“contentplaceholder 1”).FindControl(“txtnayes”).Text);
string arrivalDate=Convert.ToString(((文本框)PreviousPage.Master.FindControl(“ContentPlaceholder 1”).FindControl(“txtArrivalDate”)).Text);
string numAdults=Convert.ToString(((DropDownList)PreviousPage.Master.FindControl(“contentplaceholder 1”).FindControl(“ddl”)).SelectedValue);
string numChildren=Convert.ToString(((DropDownList)PreviousPage.Master.FindControl(“contentplaceholder 1”).FindControl(“ddlChildren”)。SelectedValue);
字符串roomTypeBusiness=“false”;
字符串roomTypeSuite=“false”;
字符串roomTypeStandard=“false”;
字符串bedTypeKing=“false”;
字符串bedTypeDouble=“false”;
字符串冒烟选项=“false”;
if(Convert.ToBoolean(((RadioButton)PreviousPage.Master.FindControl(“ContentPlaceholder 1”).FindControl(“rdoBusiness”).Checked)
roomTypeBusiness=“true”;
else if(Convert.ToBoolean(((RadioButton)PreviousPage.Master.FindControl(“contentPlaceholder 1”).FindControl(“rdoSuite”).Checked)
roomTypeSuite=“true”;
其他的
roomTypeStandard=“true”;
if(Convert.ToBoolean(((单选按钮)上一页.Master.FindControl(“contentplaceholder 1”).FindControl(“rdoKing”)).Checked))
bedTypeKing=“true”;
其他的
bedTypeDouble=“true”;
string specialRequests=Convert.ToString(((文本框)上一页.Master.FindControl(“contentplaceholder 1”).FindControl(“txtsspecialrequests”)).Text);
字符串名称=Convert.ToString(((TextBox)PreviousPage.Master.FindControl(“contentplaceholder 1”).FindControl(“txtName”)).Text);
字符串email=Convert.ToString(((TextBox)PreviousPage.Master.FindControl(“contentplaceholder 1”).FindControl(“txtEmail”)).Text);
/*将数据拆分为换行符*/
string info=string.Format(“到达日期:{0},夜数:{1},成人人数:{2},儿童人数:{3},营业室:{4},套房:{5},标准间:{6},大床:{7},双人床:{8},吸烟:{9},特殊要求:{10},姓名:{11},电子邮件:{12}”,
到达日期、晚上、晚上、晚上、孩子、房间类型业务、房间类型套房、房间类型标准、床类型、床类型双人床、吸烟选项、特殊要求、姓名、电子邮件);
String[]tokens=info.Split(新[]{,“},StringSplitOptions.RemoveEmptyEntries);
lblResults.Text=string.Join(“
”,标记); } } }
您的代码不正确。您必须使用controlId,然后将控件强制转换为正确的类型,然后使用相应的属性。对于下拉列表,您还必须使用SelectedItem.Text而不是SelectedValue.Text

下面是修改后的代码

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);
        }

}

我认为您应该将这些值存储到某个会话中,或者您可以使用Querystring处理小块数据,然后在另一个页面上使用它们,而不是使用<