using System; using PizzaSystem; using PizzaService; using PizzaModel; namespace PizzaPresentation { public partial class AdminChangeOptions : System.Web.UI.Page { private StudentService studentService; private AdminService adminService; protected void Page_Load(object sender, EventArgs e) { // check if app has yet run, set up service API if (Application["PizzaSys"] == null) { PizzaConfig pizzaService = new PizzaConfig(); Application["PizzaSys"] = pizzaService; } PizzaConfig pizzaSystem = (PizzaConfig)Application["PizzaSys"]; studentService = pizzaSystem.StudentService; adminService = pizzaSystem.AdminService; if (!IsPostBack) { // page load on GET: show current toppings and sizes ToppingsGridView.DataSource = studentService.AllToppings(); ToppingsGridView.DataBind(); PizzaSizeGridView.DataSource = studentService.AllPizzaSizes(); PizzaSizeGridView.DataBind(); } DeleteToppingResultLabel.Text = ""; AddToppingResultLabel.Text = ""; DeleteSizeResultLabel.Text = ""; AddSizeResultLabel.Text = ""; } protected void ToppingsGridView_SelectedIndexChanged(object sender, EventArgs e) { } protected void DeleteToppingButton_OnClick(object sender, EventArgs e) { try { int index = ToppingsGridView.SelectedIndex; if (index < 0) { DeleteToppingResultLabel.Text = "Nothing selected"; return; } String toppingID = (String)ToppingsGridView.SelectedRow.Cells[1].Text; adminService.DeleteTopping(toppingID); DeleteToppingResultLabel.Text = "Success"; ToppingsGridView.SelectedIndex = -1; // unselect row // redo grid ToppingsGridView.DataSource = studentService.AllToppings(); ToppingsGridView.DataBind(); } catch (ApplicationException e1) { DeleteToppingResultLabel.Text = e1.Message; } } protected void PizzaSizeGridView_SelectedIndexChanged(object sender, EventArgs e) { } // This event is fired in postback handling of user button-click // or (spuriously) if the browser sends a postback when the // user does a CR in a textbox. To prevent this, mark the // textboxes with "auto-postback" to get some Javascript // written to cause the textbox itself to do the postback. protected void DeleteSizeButton_OnClick(object sender, EventArgs e) { try { int index = PizzaSizeGridView.SelectedIndex; if (index < 0) { DeleteSizeResultLabel.Text = "Nothing selected"; return; } String sizeID = (String)PizzaSizeGridView.SelectedRow.Cells[1].Text; adminService.DeletePizzaSize(sizeID); DeleteSizeResultLabel.Text = "Success"; PizzaSizeGridView.SelectedIndex = -1; // unselect row // redo grid PizzaSizeGridView.DataSource = studentService.AllPizzaSizes(); PizzaSizeGridView.DataBind(); } catch (ApplicationException e1) { DeleteSizeResultLabel.Text = e1.Message; } } // This event fires on the postback following any user entry in // textbox, regardless of how the postback itself is caused, // i.e., by in text box or add-topping button click. // So this is a good spot to do the needed insert protected void AddSizeTextBox_TextChanged(object sender, EventArgs e) { try { String newName = AddSizeTextBox.Text; adminService.AddPizzaSize(newName); AddSizeTextBox.Text = ""; AddSizeResultLabel.Text = "Success"; PizzaSizeGridView.SelectedIndex = -1; // unselect row // redo grid PizzaSizeGridView.DataSource = studentService.AllPizzaSizes(); PizzaSizeGridView.DataBind(); } catch (ApplicationException e1) { AddSizeResultLabel.Text = e1.Message; } } protected void AddToppingTextBox_TextChanged(object sender, EventArgs e) { try { String newName = AddToppingTextBox.Text; adminService.AddTopping(newName); AddToppingTextBox.Text = ""; AddToppingResultLabel.Text = "Success"; ToppingsGridView.SelectedIndex = -1; // unselect row // redo grid ToppingsGridView.DataSource = studentService.AllToppings(); ToppingsGridView.DataBind(); } catch (ApplicationException e1) { AddToppingResultLabel.Text = e1.Message; } } protected void AddToppingButton_OnClick(object sender, EventArgs e) { } protected void AddSizeButton_OnClick(object sender, EventArgs e) { } protected void ChangesDoneButton_OnClick(object sender, EventArgs e) { Response.Redirect("AdminMenu.aspx", false); } } }