我正在尝试创建一个预订表的确认页面。但我遇到了问题,无法从之前的页面控件中获取值。我已将所有内容运行在服务器上,并将提交按钮的PostBackURL指向确认页面。一切都编译通过,但当我输入信息并点击提交后,唯一弹出的是我的布尔值,这些布尔值并不能真正反映其价值,因为我的if-else语句。我尝试了很多不同的类型转换/值抓取方法,而这种形式是我最接近的方式。任何帮助都将不胜感激。
此外,在String.format中如何换行?\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’re always treated like family</h2>
<p>
Arrival date:
<asp:TextBox ID="txtArrivalDate" runat="server" Width="75px">TextBoxHere</asp:TextBox>
<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:
<asp:TextBox ID="txtNights" runat="server" Width="45px"></asp:TextBox>
</p>
<p>
Number of Adults:
<asp:DropDownList ID="ddlAdults" runat="server" Width="50px"></asp:DropDownList>
Number of Children:
<asp:DropDownList ID="ddlChildren" runat="server" Width="50px"></asp:DropDownList>
</p>
<h3>Preferences</h3>
<p>
Room Type:
<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:
<asp:RadioButton ID="rdoKing" runat="server" GroupName="Bed" Text="King" />
<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" />
<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
。 - Dhrumil