我想在会话状态下存储来自两个页面的多个数据,并将它们显示在单独标签的第三页上。我可以从第一页的文本框中检索数据,但是当我尝试从会话状态的第二页上的下拉列表(连接到数据库)中检索所选值时,我的程序就会停止。
这是第一页背后的代码。 它工作正常,我可以在第三页标签上显示这些元素。
public partial class booking : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnBookingContinue_Click(object sender, EventArgs e)
{
Session["firstName"] = tbxFirstName.Text;
Session["lastName"] = tbxLastName.Text;
Session["country"] = tbxCountry.Text;
Session["phone"] = tbxPhone.Text;
Session["email"] = tbxEmail.Text;
Server.Transfer("~/booking2.aspx");
}
}
这是显示我所拥有的元素和dataconnection的第二页。
<ul class="bookingForm">
<li>
<h2>Booking Form:</h2>
</li>
<li>
<asp:Label ID="lblChoosePackage" runat="server" Text="Choose Package:" CssClass="bookinglabel"></asp:Label>
<asp:DropDownList ID="ddlChoosepackage" runat="server" DataSourceID="ChoosePackageList" DataTextField="Title" DataValueField="Title" CssClass="bookingdropdown" OnSelectedIndexChanged="ddlChoosepackage_SelectedIndexChanged"></asp:DropDownList>
<asp:SqlDataSource ID="ChoosePackageList" runat="server" ConnectionString="<%$ ConnectionStrings:FlorianopoliSurfConnection %>" SelectCommand="SELECT [Title] FROM [Package] ORDER BY [PackageID]"></asp:SqlDataSource>
</li>
<li>
<asp:Label ID="lblArrivalDate" runat="server" Text="Arrival Date:" CssClass="bookinglabel"></asp:Label>
<asp:TextBox ID="tbxArrivaldate" runat="server" CssClass="bookingtextbox"></asp:TextBox>
</li>
<li>
<asp:Label ID="lblPeople" runat="server" Text="Number Of People:" CssClass="bookinglabel"></asp:Label>
<asp:TextBox ID="tbxPeople" runat="server" CssClass="bookingtextbox"></asp:TextBox>
</li>
<li>
<asp:Label ID="lblDeparture" runat="server" Text="Departure Location:" CssClass="bookinglabel"></asp:Label>
<asp:DropDownList ID="ddlDeparture" runat="server" CssClass="bookingdropdown">
<asp:ListItem Selected="True">Dublin</asp:ListItem>
<asp:ListItem>Cork</asp:ListItem>
<asp:ListItem>Shannon</asp:ListItem>
<asp:ListItem>Belfast</asp:ListItem>
</asp:DropDownList>
</li>
<li>
<asp:Label ID="lblCreditCard" runat="server" Text="Credit Card:" CssClass="bookinglabel"></asp:Label>
<asp:TextBox ID="tbxCreditCard" runat="server" CssClass="bookingtextbox"></asp:TextBox>
</li>
<li>
<asp:Button ID="btnContinueToSummary" runat="server" Text="Continue" PostBackUrl="~/confirmBooking.aspx" OnClick="btnContinueToSummary_Click" />
</li>
</ul>
这是第二页背后的代码。
public partial class booking2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlChoosepackage.SelectedIndex = 0;
}
}
protected void ddlChoosepackage_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlChoosepackage.SelectedIndex != 0)
{
Session["choosePackage"] = ddlChoosepackage.SelectedItem.Value;
}
}
protected void btnContinueToSummary_Click(object sender, EventArgs e)
{
//Session["choosePackage"] = ddlChoosepackage.SelectedItem;
Session["arrivalDate"] = tbxArrivaldate.Text;
Session["numOfPeople"] = tbxPeople.Text;
Session["departure"] = ddlDeparture.SelectedItem;
Session["creditCard"] = tbxCreditCard.Text;
Response.Redirect("~/confirmBooking.aspx");
}
}
这是第三页背后的代码。 我被抓住的路线是:
string package = (string)Session["choosePackage"];
我收到错误:
对象引用未设置为对象的实例。
所以我似乎从下拉列表或下拉列表的会话变量中获取空值。
毫无疑问,这里有一个新手的错误。
public partial class confirmBooking : System.Web.UI.Page
{
public void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["CurrencySelection"] != null)
{
HttpCookie cookie = Request.Cookies["CurrencySelection"];
ddlChooseCurrency.SelectedItem.Text = Server.HtmlEncode(cookie.Value);
}
string firstName = (string)Session["firstName"];
string lastName = (string)Session["lastName"];
string country = (string)Session["country"];
string phoneNumber = (string)Session["phone"];
string email = (string)Session["email"];
string package = (string)Session["choosePackage"];
string arrivalDate = (string)Session["arrivalDate"];
string numOfPeople = (string)Session["numOfPeople"];
string departure = (string)Session["departure"];
string creditCard = (string)Session["creditCard"];
lblSummaryNameDisplay.Text = string.Format("{0} {1}", firstName, lastName);
lblSummaryCountryDisplay.Text = string.Format("{0}", country);
lblSummaryPhoneDisplay.Text = string.Format("{0}", phoneNumber);
lblSummaryEmailDisplay.Text = string.Format("{0}", email);
lblSummaryPackageDisplay.Text = package.ToString();
lblSummaryArrivalDateDisplay.Text = arrivalDate.ToString();
lblSummaryPeopleDisplay.Text = numOfPeople.ToString();
lblSummaryDepartureDisplay.Text = departure.ToString();
lblSummaryCardDisplay.Text = creditCard.ToString();
}
}
答案 0 :(得分:1)
我认为问题是SelectedIndexChanged
在SelectedIndex
上设置Page_Load
时不会触发Page_Load
事件。我之前遇到过类似问题,请尝试调整 if (!IsPostBack)
{
ddlChoosepackage.DataBind();
ddlChoosepackage.SelectedIndex = 0;
}
事件......
Cannot resolve symbol 'R'