/// <summary> /// Adds the selected character to /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void BtnAddMember_Click(object sender, EventArgs e) { // Check if characer belongs to the DM User dm = validUsers[DDDMUsers.SelectedIndex]; User user = validUsers[DDCampaignUsers.SelectedIndex]; if (dm.ID == user.ID) { LblInvalidCharacter.Text = "The DM cannot have a character in the party."; return; } // Catch any errors that occur when retrieving character LblInvalidCharacter.Text = ""; Character character = null; try { character = userCharacters[DDCampaignCharacters.SelectedIndex]; } catch (ArgumentOutOfRangeException ex) { LblInvalidCharacter.Text = "That character cannot be added to the party."; return; } // Check if user already has character in party foreach (User selectedUsers in selectedUsers) { if (selectedUsers.ID == user.ID) { LblInvalidCharacter.Text = "That user already has a character in the party."; return; } } // Update Session if (Session["selectedCharacters"] != null && Session["selectedUsers"] != null) { selectedCharacters = (List <Character>)Session["selectedCharacters"]; selectedUsers = (List <User>)Session["selectedUsers"]; } // Ensure character is not already added foreach (Character selectedCharacter in selectedCharacters) { if (selectedCharacter.Equals(character)) { LblInvalidCharacter.Text = "That character is already in the party."; return; } } // If valid, add user and character selectedUsers.Add(user); Session["selectedUsers"] = selectedUsers; selectedCharacters.Add(character); Session["selectedCharacters"] = selectedCharacters; LBPartyMembers.DataSource = selectedCharacters; LBPartyMembers.DataBind(); }
/// <summary> /// Removes a selected character from the party /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void BtnRemoveCharacter_Click(object sender, EventArgs e) { Character removeCharacter = null; try { removeCharacter = selectedCharacters[LBPartyMembers.SelectedIndex]; } catch (ArgumentOutOfRangeException ex) { LblCannotRemove.Text = "Please select a character from the list to remove."; return; } selectedCharacters.Remove(removeCharacter); Session["selectedCharacters"] = selectedCharacters; LBPartyMembers.DataSource = selectedCharacters; LBPartyMembers.DataBind(); }