private void LoadPaymentMethodSettings()
        {
            List <PaymentMethodTempSetting> paymentMethodTempSettings = ViewState["PaymentMethodSettings"] as List <PaymentMethodTempSetting>;

            if (paymentMethodTempSettings == null)
            {
                paymentMethodTempSettings = paymentMethod.Settings.Where(s => s.LanguageId == null).Select(s => new PaymentMethodTempSetting()
                {
                    Id = s.Id, Key = s.Key, Value = s.Value, UmbracoLanguageId = s.LanguageId
                }).ToList();
                ViewState["PaymentMethodSettings"] = paymentMethodTempSettings;
            }

            ListView lvCommonSettings = CurrentTabView.FindControl <ListView>("LvSettings");

            lvCommonSettings.DataSource = paymentMethodTempSettings;
            lvCommonSettings.DataBind();

            foreach (Language language in umbracoLanguages)
            {
                List <PaymentMethodTempSetting> paymentMethodLanguageTempSettings = ViewState["PaymentMethodSettings" + language.id] as List <PaymentMethodTempSetting>;
                if (paymentMethodLanguageTempSettings == null)
                {
                    paymentMethodLanguageTempSettings = paymentMethod.Settings.Where(s => s.LanguageId == language.id).Select(s => new PaymentMethodTempSetting()
                    {
                        Id = s.Id, Key = s.Key, Value = s.Value, UmbracoLanguageId = s.LanguageId
                    }).ToList();
                    ViewState["PaymentMethodSettings" + language.id] = paymentMethodLanguageTempSettings;
                }

                ListView lvSettings = CurrentTabView.FindControl <ListView>("LvSettings" + language.id);
                lvSettings.DataSource = paymentMethodLanguageTempSettings;
                lvSettings.DataBind();
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            if (!IsPostBack)
            {
                TemplateFileSelectorControl.LoadTemplateFiles();

                TxtName.Text  = emailTemplate.Name;
                TxtAlias.Text = emailTemplate.Alias;
                ChkShouldClientReceiveEmail.Checked = emailTemplate.SendEmailToCustomer;

                EmailTemplateSettings defaultSettings = emailTemplate.Settings.SingleOrDefault(s => s.LanguageId == null);
                if (defaultSettings != null)
                {
                    TxtSubject.Text       = defaultSettings.Subject;
                    TxtSenderName.Text    = defaultSettings.SenderName;
                    TxtSenderAddress.Text = defaultSettings.SenderAddress;
                    TxtToAddresses.Text   = string.Join(";", defaultSettings.ToAddresses);
                    TxtCCAddresses.Text   = string.Join(";", defaultSettings.CcAddresses);
                    TxtBCCAddresses.Text  = string.Join(";", defaultSettings.BccAddresses);

                    TemplateFileSelectorControl.Items.TrySelectByValue(defaultSettings.TemplateFile);
                }

                foreach (Language language in umbracoLanguages)
                {
                    TemplateFileSelector templateFileSelectorControl = CurrentTabView.FindControl <TemplateFileSelector>("TemplateFileSelectorControl" + language.id);
                    templateFileSelectorControl.LoadTemplateFiles();

                    EmailTemplateSettings emailTemplateSettings = emailTemplate.Settings.SingleOrDefault(s => s.LanguageId == language.id);
                    if (emailTemplateSettings != null)
                    {
                        TextBox txtSubject       = CurrentTabView.FindControl <TextBox>("TxtSubject" + language.id);
                        TextBox txtSenderName    = CurrentTabView.FindControl <TextBox>("TxtSenderName" + language.id);
                        TextBox txtSenderAddress = CurrentTabView.FindControl <TextBox>("TxtSenderAddress" + language.id);
                        TextBox txtToAddresses   = CurrentTabView.FindControl <TextBox>("TxtToAddresses" + language.id);
                        TextBox txtCCAddresses   = CurrentTabView.FindControl <TextBox>("TxtCCAddresses" + language.id);
                        TextBox txtBCCAddresses  = CurrentTabView.FindControl <TextBox>("TxtBCCAddresses" + language.id);

                        txtSubject.Text       = emailTemplateSettings.Subject;
                        txtSenderName.Text    = emailTemplateSettings.SenderName;
                        txtSenderAddress.Text = emailTemplateSettings.SenderAddress;
                        txtToAddresses.Text   = string.Join(";", emailTemplateSettings.ToAddresses);
                        txtCCAddresses.Text   = string.Join(";", emailTemplateSettings.CcAddresses);
                        txtBCCAddresses.Text  = string.Join(";", emailTemplateSettings.BccAddresses);

                        foreach (ListItem item in templateFileSelectorControl.Items)
                        {
                            item.Selected = item.Value == emailTemplateSettings.TemplateFile;
                            if (item.Selected)
                            {
                                break;
                            }
                        }
                    }
                }
            }

            base.OnLoad(e);
        }
        protected void btnAddSetting_Click(object sender, EventArgs e)
        {
            string strLanguageId = (sender as Button).CommandArgument;

            int?     languageId = strLanguageId.TryParse <int>();
            ListView lvSettings = CurrentTabView.FindControl <ListView>("LvSettings" + strLanguageId);

            //Save all temp entries so user dont lose data
            SaveTempSettings();

            List <PaymentMethodTempSetting> paymentMethodSettings = (ViewState[("PaymentMethodSettings" + strLanguageId)] as List <PaymentMethodTempSetting>);

            paymentMethodSettings.Add(new PaymentMethodTempSetting()
            {
                UmbracoLanguageId = languageId
            });

            lvSettings.DataSource = paymentMethodSettings;
            lvSettings.DataBind();
        }
        private List <PaymentMethodTempSetting> GetTempSettings(int?languageId)
        {
            string strLanguageId = languageId != null?languageId.Value.ToString() : string.Empty;

            List <PaymentMethodTempSetting> settings = new List <PaymentMethodTempSetting>();

            ListView lvSettings = CurrentTabView.FindControl <ListView>("LvSettings" + strLanguageId);

            foreach (ListViewDataItem dataItem in lvSettings.Items)
            {
                HiddenField hdfId    = dataItem.FindControl <HiddenField>("HdfId");
                TextBox     txtKey   = dataItem.FindControl <TextBox>("TxtKey");
                TextBox     txtValue = dataItem.FindControl <TextBox>("TxtValue");

                settings.Add(new PaymentMethodTempSetting()
                {
                    Id = long.Parse(hdfId.Value), UmbracoLanguageId = languageId, Key = txtKey.Text, Value = txtValue.Text
                });
            }

            return(settings);
        }
        private void SaveButton_Clicked(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                emailTemplate.Name  = TxtName.Text;
                emailTemplate.Alias = TxtAlias.Text;
                emailTemplate.SendEmailToCustomer = ChkShouldClientReceiveEmail.Checked;

                string subject       = TxtSubject.Text;
                string senderName    = TxtSenderName.Text;
                string senderAddress = TxtSenderAddress.Text;
                string toAddresses   = TxtToAddresses.Text;
                string ccAddresses   = TxtCCAddresses.Text;
                string bccAddresses  = TxtBCCAddresses.Text;
                string templateFile  = TemplateFileSelectorControl.SelectedValue;

                AddOrUpdateEmailTemplateSettings(emailTemplate.Settings.SingleOrDefault(s => s.LanguageId == null), subject, senderName, senderAddress, toAddresses, ccAddresses, bccAddresses, templateFile);

                foreach (Language language in umbracoLanguages)
                {
                    subject       = CurrentTabView.FindControl <TextBox>("TxtSubject" + language.id).Text;
                    senderName    = CurrentTabView.FindControl <TextBox>("TxtSenderName" + language.id).Text;
                    senderAddress = CurrentTabView.FindControl <TextBox>("TxtSenderAddress" + language.id).Text;
                    toAddresses   = CurrentTabView.FindControl <TextBox>("TxtToAddresses" + language.id).Text;
                    ccAddresses   = CurrentTabView.FindControl <TextBox>("TxtCCAddresses" + language.id).Text;
                    bccAddresses  = CurrentTabView.FindControl <TextBox>("TxtBCCAddresses" + language.id).Text;
                    templateFile  = CurrentTabView.FindControl <TemplateFileSelector>("TemplateFileSelectorControl" + language.id).SelectedValue;

                    AddOrUpdateEmailTemplateSettings(emailTemplate.Settings.SingleOrDefault(s => s.LanguageId == language.id), subject, senderName, senderAddress, toAddresses, ccAddresses, bccAddresses, templateFile, language.id);
                }

                emailTemplate.Save();

                ClientTools.ShowSpeechBubble(SpeechBubbleIcon.Save, CommonTerms.EmailTemplateSaved, string.Empty);
            }
        }