[英]How to place selected item from data bound drop down list in session state?
I want to store multiple pieces of data from two pages in session state and display them on a third page in separate labels. 我想在会话状态下存储两页中的多条数据,并在第三页中的单独标签中显示它们。 I can retrieve data from textboxes from the first page but my program then stops when I try to retrieve selected value from drop down list(connected to database) on second page from session state.
我可以从第一页的文本框中检索数据,但是当我尝试从会话状态从第二页的下拉列表(连接到数据库)中检索选定的值时,程序就会停止。
This is code behind from first page. 这是第一页后面的代码。 It works ok and I can display these elements on third page labels.
一切正常,我可以在第三页标签上显示这些元素。
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");
}
}
This is from the second page showing the elements I have in place and 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>
This is the code behind from second page. 这是第二页后面的代码。
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");
}
}
This is code behind from third page. 这是第三页后面的代码。 The line I get caught on is:
我发现的那条线是:
string package = (string)Session["choosePackage"];
I get the error: 我得到错误:
Object reference not set to an instance of an object.
你调用的对象是空的。
So I seem to be getting a null value from the drop down list or from the session variable of the drop down list. 因此,我似乎从下拉列表或下拉列表的会话变量中获取了空值。
There is a rookie mistake here somewhere no doubt. 毫无疑问,这里有个菜鸟错误。
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();
}
}
I believe the problem is that the SelectedIndexChanged
event doesn't fire when you set SelectedIndex
on Page_Load
. 我相信问题是在
Page_Load
上设置SelectedIndex
时,不会触发SelectedIndexChanged
事件。 I had a similar issue before, try adjusting your Page_Load
event... 我之前也遇到过类似的问题,请尝试调整您的
Page_Load
事件...
if (!IsPostBack)
{
ddlChoosepackage.DataBind();
ddlChoosepackage.SelectedIndex = 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.