using System; using System.Collections.Generic; using System.Data; using System.Configuration; using System.Web.Configuration; using System.Web.UI.WebControls; using PizzaModel; using PizzaService; using PizzaSystem; namespace PizzaPresentation { public partial class OrderForm : System.Web.UI.Page { private StudentService studentService; private int roomNumber; protected void Page_Load(object sender, EventArgs e) { // if room not there or service API not there, redirect to start page PizzaConfig pizzaSystem = (PizzaConfig)Application["PizzaSys"]; String roomNumberStr = (String)Session["room"]; if (pizzaSystem == null || roomNumberStr == null) { Response.Redirect("StudentStart.aspx", false); return; } studentService = pizzaSystem.StudentService; roomNumber = int.Parse(roomNumberStr); // page load on GET: set up pizza sizes, toppings lists if (!IsPostBack) { List allSizes = studentService.AllPizzaSizes(); RadioButtonList1.DataSource = allSizes; RadioButtonList1.DataTextField = "SizeName"; RadioButtonList1.DataValueField = "ID"; RadioButtonList1.DataBind(); ToppingsCheckBoxList.DataSource = studentService.AllToppings(); ToppingsCheckBoxList.DataTextField = "ToppingName"; ToppingsCheckBoxList.DataValueField = "ID"; ToppingsCheckBoxList.DataBind(); } } // This event happens even if autopostback is not turned on // it just happens when the form is submitted in that case // but we don't need to use it, since all the info is avail // to the submit event handler protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e) { // can look at these here, or at submit, more conveniently //String buttonText = RadioButtonList1.SelectedItem.Text; //sizeID = RadioButtonList1.SelectedItem.Value; } protected void ToppingsCheckBoxList_SelectedIndexChanged(object sender, EventArgs e) { //String boxText = ToppingsCheckBoxList.SelectedItem.Text; //String toppingID = ToppingsCheckBoxList.SelectedItem.Value; } public void SubmitButton_OnClick(object sender, EventArgs e) { String sizeID = null; // get all the user input from the various controls if (RadioButtonList1.SelectedItem != null) sizeID = RadioButtonList1.SelectedItem.Value; else { OrderResultLabel.Text = "Please choose a size"; return; // give user another chance to input a size } List toppingIDs = new List(); // find selected checkbox items, add those topping ids to list foreach (ListItem i in ToppingsCheckBoxList.Items) { if (i.Selected) toppingIDs.Add(i.Value); } try { int orderID = studentService.MakeOrder(roomNumber, sizeID, toppingIDs); Session["orderID"] = orderID; // remember orderID for thank you page // redirect to that page. (Alternatively, absorb that page in this one.) Response.Redirect("OrderThankYou.aspx", false); } catch (ApplicationException e1) { OrderResultLabel.Text = "order failed: " + e1.Message; } } } }