using framework_business;
using framework_library;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class controls_module_calendarRoom : System.Web.UI.UserControl
{
public int CalendarId
{
get
{
if (ViewState["calendarId"] == null)
return 0;
else
return (int)ViewState["calendarId"];
}
set
{
ViewState["calendarId"] = value;
}
}
#region methods
///
/// Reload Control
///
public void ReloadControl(int calId)
{
this.CalendarId = calId;
BindGrid();
BindSequence();
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "setLoadOptions", "if(typeof(setLoadOptions) == \"function\"){window.onload = setLoadOptions()};", true);
}
///
/// Bind Grid
///
private void BindGrid()
{
try
{
DataTable roomData = xData.GetTypedByCriteriaSpecificTable("recId", typeof(oCalendarRoom), "calendarId", this.CalendarId.ToString(), "sequence DESC");
rptCalendarRoom.DataSource = roomData;
rptCalendarRoom.DataBind();
}
catch (Exception ex)
{
exception.HandleException("CalendarRoom:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("Error.aspx", false);
}
}
///
/// Bind Sequence
///
private void BindSequence()
{
try
{
for (int i = 0; i < 20; i++)
{
//append sequence item
ddSequence.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
}
catch (Exception ex)
{
exception.HandleException("CalendarRoom:", MethodBase.GetCurrentMethod().Name, ex, 0);
Response.Redirect("/error", false);
}
}
private void TogglePanels(string panelName)
{
switch (panelName)
{
case "pnlForm":
pnlForm.Visible = true;
pnlGrid.Visible = false;
lblHeading.Visible = true;
break;
case "pnlGrid":
pnlForm.Visible = false;
pnlGrid.Visible = true;
lblHeading.Visible = false;
break;
}
//clear result
pnlResult.Visible = false;
pnlGridResult.Visible = false;
}
private void PopulateFormValues(oCalendarRoom _room)
{
try
{
txtRoom.Text = _room.name;
chkActive.Checked = _room.isActive;
ddSequence.ClearSelection();
if (ddSequence.Items.FindByValue(_room.sequence.ToString()) != null)
ddSequence.Items.FindByValue(_room.sequence.ToString()).Selected = true;
}
catch (Exception ex)
{
exception.HandleException("CalendarRoom:", MethodBase.GetCurrentMethod().Name, ex, Session["admin"]);
Response.Redirect("/error", false);
}
}
private void SaveFormValues(ref oCalendarRoom _room)
{
try
{
_room.name = txtRoom.Text;
_room.isActive = chkActive.Checked;
_room.sequence = int.Parse(ddSequence.SelectedItem.Value);
_room.calendarId = this.CalendarId;
_room.calendarEventTypes = String.Empty;
}
catch (Exception ex)
{
exception.HandleException("CalendarRoom:", MethodBase.GetCurrentMethod().Name, ex, Session["admin"]);
Response.Redirect("/error", false);
}
}
#endregion
#region events
///
/// Page Load Event
///
///
///
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!Page.IsPostBack)
{
//BindGrid();
//BindSequence();
}
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "setLoadOptions", "if(typeof(setLoadOptions) == \"function\"){window.onload = setLoadOptions()};", true);
}
catch (Exception ex)
{
exception.HandleException("CalendarRoom:", MethodBase.GetCurrentMethod().Name, ex, Session["admin"]);
Response.Redirect("/error", false);
}
}
///
/// New Location
///
///
///
protected void btnNewRoom_Click(object sender, EventArgs e)
{
try
{
utils.disposeSession("Room");
oCalendarRoom room = new oCalendarRoom();
PopulateFormValues(room);
TogglePanels("pnlForm");
}
catch (Exception ex)
{
exception.HandleException("CalendarRoom:", MethodBase.GetCurrentMethod().Name, ex, Session["admin"]);
Response.Redirect("/error", false);
}
}
///
/// Edit
///
///
///
protected void lnkEdit_Click(object sender, EventArgs e)
{
try
{
oCalendarRoom room = new oCalendarRoom();
ArrayList list = new ArrayList();
if (sender.GetType() == typeof(LinkButton))
{
int recId = int.Parse(((LinkButton)sender).CommandArgument);
if (recId > 0)
{
list = xData.GetTypedByCriteriaSpecific("recId", typeof(oCalendarRoom), "recId", recId.ToString());
//enumerate list
foreach (oCalendarRoom r in list)
{
//assign object
room = r;
//populate page form
PopulateFormValues(room);
Session["Room"] = room;
break;
}
TogglePanels("pnlForm");
}
}
}
catch (Exception ex)
{
exception.HandleException("CalendarRoom:", MethodBase.GetCurrentMethod().Name, ex, Session["admin"]);
Response.Redirect("/error", false);
}
}
///
/// Remove
///
///
///
protected void lnkRemove_Click(object sender, EventArgs e)
{
try
{
if (sender.GetType() == typeof(LinkButton))
{
int recId = int.Parse(((LinkButton)sender).CommandArgument);
//first check if there are any events linked to this room
ArrayList events = xData.GetTypedByCriteriaSpecific("recId", typeof(oCalendarEvent), "calendarRoomId", recId.ToString());
if (events != null && events.Count > 0)
{
pnlGridResult.Visible = true;
lblGridResult.Text = "the selected Location has Events linked to it and cannot be deleted.";
}
//Delete event type
else if (xData.DeleteTyped("recId", recId.ToString(), typeof(oCalendarRoom)))
{
//rebind pages grid
BindGrid();
pnlResult.Visible = true;
//display successful result
lblResult.Text = "the selected Location was deleted successfully.";
}
}
}
catch (Exception ex)
{
exception.HandleException("CalendarRoom:", MethodBase.GetCurrentMethod().Name, ex, Session["admin"]);
Response.Redirect("/error", false);
}
}
///
/// Save Location
///
///
///
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
oCalendarRoom room = new oCalendarRoom();
if (utils.verifySession("Room"))//update mode
{
room = (oCalendarRoom)Session["Room"];
//save form values
SaveFormValues(ref room);
if (xData.UpdateTyped("recId", room.recId.ToString(), typeof(oCalendarRoom), room))
{
Session["Room"] = room;
BindGrid();
pnlResult.Visible = true;
lblResult.Text = "your changes were updated successfully.";
}
}
else
{
if (!xData.VerifyExists("recId", typeof(oCalendarRoom), "name,calendarId", txtRoom.Text + "," + this.CalendarId))
{
//save form values
SaveFormValues(ref room);
room.recId = xData.SaveTyped("recId", typeof(oCalendarRoom), room);
if (room.recId > 0)
{
Session["Room"] = room;
BindGrid();
pnlResult.Visible = true;
lblResult.Text = "your Location was created successfully.";
}
}
else
{
pnlResult.Visible = true;
lblResult.Text = "A Location with this name already exists on the system";
}
}
}
catch (Exception ex)
{
exception.HandleException("CalendarRoom:", MethodBase.GetCurrentMethod().Name, ex, Session["admin"]);
Response.Redirect("/error", false);
}
}
///
/// Cancel
///
///
///
protected void btnCancel_Click(object sender, EventArgs e)
{
try
{
utils.disposeSession("Room");
BindGrid();
TogglePanels("pnlGrid");
}
catch (Exception ex)
{
exception.HandleException("CalendarRoom:", MethodBase.GetCurrentMethod().Name, ex, Session["admin"]);
Response.Redirect("/error", false);
}
}
#endregion
}