using framework_business; using framework_library; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class controls_admin_faq : System.Web.UI.UserControl { #region methods private void BindGrid() { try { ArrayList list = xData.GetTypedCollection("recId", typeof(oFAQ), "sequence"); rptFaq.DataSource = list; rptFaq.DataBind(); } catch (Exception ex) { exception.HandleException("faq:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } 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("faq:", MethodBase.GetCurrentMethod().Name, ex, 0); Response.Redirect("/error", false); } } private void TogglePanels(string panelName) { switch (panelName) { case "pnlForm": pnlForm.Visible = true; pnlGrid.Visible = false; break; case "pnlGrid": pnlForm.Visible = false; pnlGrid.Visible = true; break; } //clear result pnlResult.Visible = false; } private void PopulateFormValues(oFAQ faq) { try { txtQuestion.Text = faq.question; txtAnswer.Text = faq.answer; chkActive.Checked = faq.isActive; ddSequence.ClearSelection(); if (ddSequence.Items.FindByValue(faq.sequence.ToString()) != null) ddSequence.Items.FindByValue(faq.sequence.ToString()).Selected = true; } catch (Exception ex) { exception.HandleException("faq:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } private void SaveFormValues(ref oFAQ faq) { try { faq.answer = txtAnswer.Text; faq.isActive = chkActive.Checked; faq.question = txtQuestion.Text; faq.sequence = int.Parse(ddSequence.SelectedItem.Value); } catch (Exception ex) { exception.HandleException("faq:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } #endregion #region events protected void Page_Load(object sender, EventArgs e) { try { if (!Page.IsPostBack) { BindGrid(); BindSequence(); } } catch (Exception ex) { exception.HandleException("faq:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } protected void btnNewQuestion_Click(object sender, EventArgs e) { try { utils.disposeSession("FAQ"); oFAQ faq = new oFAQ(); PopulateFormValues(faq); TogglePanels("pnlForm"); } catch (Exception ex) { exception.HandleException("faq:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } protected void lnkEdit_Click(object sender, EventArgs e) { try { oFAQ faq = new oFAQ(); ArrayList list = new ArrayList(); if (sender.GetType() == typeof(LinkButton)) { int recId = int.Parse(((LinkButton)sender).CommandArgument); if (recId > 0) { //get cat list = xData.GetTypedByCriteriaSpecific("recId", typeof(oFAQ), "recId", recId.ToString()); //enumerate list foreach (oFAQ f in list) { //assign object faq = f; //populate page form PopulateFormValues(faq); Session["FAQ"] = faq; break; } TogglePanels("pnlForm"); } } } catch (Exception ex) { exception.HandleException("faq:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } protected void lnkRemove_Click(object sender, EventArgs e) { try { if (sender.GetType() == typeof(LinkButton)) { int recId = int.Parse(((LinkButton)sender).CommandArgument); //Delete faq if (xData.DeleteTyped("recId", recId.ToString(), typeof(oFAQ))) { //rebind pages grid BindGrid(); pnlResult.Visible = true; //display successful result lblResult.Text = "the selected FAQ was deleted successfully."; } } } catch (Exception ex) { exception.HandleException("faq:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } protected void btnCancel_Click(object sender, EventArgs e) { try { utils.disposeSession("FAQ"); BindGrid(); TogglePanels("pnlGrid"); } catch (Exception ex) { exception.HandleException("faq:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } protected void btnSave_Click(object sender, EventArgs e) { try { oFAQ faq = new oFAQ(); if (utils.verifySession("FAQ"))//update mode { faq = (oFAQ)Session["FAQ"]; //save form values SaveFormValues(ref faq); if (xData.UpdateTyped("recId", faq.recId.ToString(), typeof(oFAQ), faq)) { Session["FAQ"] = faq; BindGrid(); pnlResult.Visible = true; lblResult.Text = "your changes were updated successfully."; } } else { if (!xData.VerifyExists("recId", typeof(oFAQ), "Question", txtQuestion.Text)) { //save form values SaveFormValues(ref faq); faq.recId = xData.SaveTyped("recId", typeof(oFAQ), faq); if (faq.recId > 0) { Session["FAQ"] = faq; BindGrid(); pnlResult.Visible = true; lblResult.Text = "your FAQ was created successfully."; } } else { pnlResult.Visible = true; lblResult.Text = "A FAQ with this question already exists on the system"; } } } catch (Exception ex) { exception.HandleException("faq:", MethodBase.GetCurrentMethod().Name, ex, Session["user"]); Response.Redirect("/error", false); } } #endregion }