protected void AddUnitButton_Click(object sender, EventArgs e) { MessageUserControl.TryRun(() => { MessageUserControl.Visible = true; DropDownList siteNameDDL = AddSiteNameDDL; int siteId = int.Parse(siteNameDDL.SelectedValue); string unitName = AddUnitName.Text; string description = AddUnitDescription.Text; Utility utility = new Utility(); utility.checkValidString(unitName); utility.checkValidString(description); if (string.IsNullOrEmpty(unitName)) { MessageUserControl.ShowInfo("Warning", "Unit Name is required."); } else if (string.IsNullOrEmpty(description)) { MessageUserControl.ShowInfo("Warning", "Description is required."); } else { MessageUserControl.TryRun(() => { UnitController sysmgr = new UnitController(); sysmgr.Unit_Add(siteId, unitName, description, int.Parse(UserSiteId.Text)); ActiveUnitListView.DataSourceID = ActiveUnitListView_ODS.ID; AddUnitListView.DataBind(); ActiveUnitListView.DataBind(); // Clear fields after add AddClearButton_Click(sender, e); }, "Success", "New unit has been added."); } }); }
/// <summary> /// Button commands for inside the ListView. Allows the user to do things like Update, Add, Clear, Cancel etc. /// </summary> /// <param name="sender">Sender of the PageLoad IE: Button</param> /// <param name="e">Arguments submitted by the sender. IE: "Cancel"</param> protected void UnitListView_ItemCommand(object sender, ListViewCommandEventArgs e) { MessageUserControl.Visible = false; int unitId = 1; MessageUserControl.TryRun(() => { // If the user is attempting to do anything but Add or Cancel, collect the unitId from the ListView if (!e.CommandName.Equals("Cancel") && !e.CommandName.Equals("Add")) { unitId = int.Parse(e.CommandArgument.ToString()); } // Update the Unit on the selected row. Only one item can be updated at a time. if (e.CommandName.Equals("Change")) { MessageUserControl.TryRun(() => { int i = e.Item.DisplayIndex; DropDownList siteNameDDL = ActiveUnitListView.Items[i].FindControl("SiteNameDDL") as DropDownList; int siteId = int.Parse(siteNameDDL.SelectedValue); TextBox unitNameTextBox = ActiveUnitListView.Items[i].FindControl("UnitNameTextBox") as TextBox; string unitName = unitNameTextBox.Text; TextBox descriptionTextBox = ActiveUnitListView.Items[i].FindControl("DescriptionTextBox") as TextBox; string description = descriptionTextBox.Text; MessageUserControl.Visible = true; Utility utility = new Utility(); utility.checkValidString(unitName); utility.checkValidString(description); UnitController sysmgr = new UnitController(); sysmgr.Unit_Update(unitId, siteId, unitName, description); ActiveUnitListView.DataSourceID = ActiveUnitListView_ODS.ID; ActiveUnitListView.EditIndex = -1; ActiveUnitListView.DataBind(); AddUnitListView.DataBind(); DeactivatedUnitListView.DataBind(); ActiveSearchBox.Text = ""; MessageUserControl.Visible = true; ActiveSearchButton.Enabled = true; ActiveClearButton.Enabled = true; DataPager pager = ActiveUnitListView.FindControl("ActiveDataPager") as DataPager; pager.Visible = true; }, "Success", "Unit has been updated"); } // Deactivate the Unit on the selected row. else if (e.CommandName.Equals("Deactivate")) { MessageUserControl.TryRun(() => { MessageUserControl.Visible = true; UnitController sysmgr = new UnitController(); sysmgr.Unit_Deactivate(unitId); }, "Success", "Unit has been deactivated"); } }); ActiveUnitListView.DataSourceID = ActiveUnitListView_ODS.ID; DeactivatedUnitListView.DataSourceID = DeactivatedUnitListView_ODS.ID; AddSiteNameDDL.DataSourceID = ActiveSiteNameDDL_ODS.ID; ActiveUnitListView.DataBind(); AddUnitListView.DataBind(); DeactivatedUnitListView.DataBind(); AddSiteNameDDL.DataBind(); }