2015-12-03 64 views
3

我正在嘗試爲預訂表單做出確認頁面。而且我無法從我以前的頁面控件中獲取價值。我有一切在服務器上運行,我有我的提交按鈕的PostBackURL指向確認頁面。一切都在編譯,但是當我放入信息並提交時,彈出的唯一信息就是我的布爾值,因爲我的if-elses沒有真正反映真實值。我已經嘗試過一百萬次不同的廣告/價值攫取嘗試,而這種目前的形式是我最接近的。任何幫助將不勝感激。 另外,如何獲取String.format中的下一行(如\ n)? \ n似乎不起作用。 確認頁面看起來像這樣從PreviousPage獲取值。找到控件

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> 
+0

我認爲你應該將值存儲到某個Session中,或者你可以使用Querystring來處理小塊數據,並在另一個頁面上使用它們而不是使用'PreviousPage.FindControl'。 –

回答

1

使用PreviousPage.FindControl()一個只能當有人從Page A —> Transfer Data —> Page B導航訪問|上一頁數據。如果直接請求一個頁面,則PreviousPage呈現空引用。

在這裏,在你的情況下,一旦導航到頁面Default2你只在頁面加載時保持價值。當您點擊提交您的Default.aspx調用本身和PreviousPage丟失數據。同樣,你找到控制的方式是不正確的,你會得到NULLREFERENCE

更多信息

如果在你的web應用程序中的母版頁,然後一個不能直接只需通過調用PreviousPage.FindControl()一個首先要找到ContentHolder控制,在母版頁,然後居住訪問以前的頁面控制人們可以在兒童網頁中找到兒童控制。

解決方案: -

使用IsPostBack財產

我怎樣才能DUE_DATE的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);   
      } 
     } 
    } 
+0

當我嘗試這樣我得到「錯誤'System.Web.UI.Control'不包含'文本'的定義和沒有擴展方法'文本'接受類型'System.Web第一個參數。 UI.Control'可以找到(你是否缺少使用指令或程序集引用?)「 –

+0

@OptimusOmega:代碼編輯請嘗試它。 – Prabhat

+0

當我以這種方式運行時,我得到「App_Web_tbejxdai.dll中發生類型'System.NullReferenceException'的第一次機會異常」它指向 字符串numNights = Convert.ToString(((TextBox)PreviousPage.FindControl(「txtNights」 ))。文本); –

0

您的代碼是不正確的。您必須使用控件ID,然後投了控制正確的類型,然後使用相應的properties.Also下拉列表中,你必須使用的,而不是SelectedValue.Text

以下SelectedItem.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); 
     } 

} 
+0

當我嘗試這種方式時,我得到「在App_Web_stp3fzm4.dll中發生類型'System.NullReferenceException'的第一次機會異常」錯誤 –

+0

當有人從您的應用程序中的其他頁面導航到此頁面時,PreviousPage屬性只有值。並且確保ViewState對這兩個頁面都是啓用的 –

+0

我運行我的ReservationForm(我更新了原始問題以顯示),然後填寫所有信息並單擊具有PostBackUrl的提交按鈕到我的ConfirmationPage.aspx。我在哪裏啓用ViewState? –