using System; using System.Data; using System.Collections; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using CMS.CMSHelper; using CMS.FormControls; using CMS.FormEngine; using CMS.GlobalHelper; using CMS.SettingsProvider; public partial class CMSFormControls_Classes_SelectAlternativeForm : FormEngineUserControl, ICallbackEventHandler { private string _callbackArg = string.Empty; #region "Properties" /// /// Gets or sets the enabled state of the control. /// public override bool Enabled { get { return base.Enabled; } set { base.Enabled = value; txtName.Enabled = value; btnSelect.Enabled = value; } } ///Gets or sets field value. public override object Value { get { return txtName.Text; } set { txtName.Text = (string)value; } } /// /// Gets ClientID of the textbox with transformation name. /// public override string ValueElementID { get { return txtName.ClientID; } } /// /// Gets or sets Class name. /// public string ClassName { get { return ValidationHelper.GetString(ViewState["className"], string.Empty); } set { ViewState["className"] = (object)value; } } #endregion #region "CallBackHandling" public void RaiseCallbackEvent(string eventArgument) { _callbackArg = eventArgument; } public string GetCallbackResult() { return Validate(_callbackArg, true); } #endregion protected void Page_Load(object sender, EventArgs e) { string argument = "document.getElementById('" + txtName.ClientID + "').value"; string clientCallback = "CheckAlternativeForm"; string CallbackRef = Page.ClientScript.GetCallbackEventReference(this, argument, clientCallback, "'" + lblStatus.ClientID + "'"); txtName.Attributes["onchange"] = String.Format("javascript:{0}", CallbackRef); btnSelect.Text = GetString("general.select"); btnSelect.OnClientClick = "SelectAltFormDialog_" + ClientID + "(); return false;"; btnClear.Text = GetString("general.clear"); btnClear.OnClientClick = string.Format("SetVal('{0}', ''); return false;", txtName.ClientID); } protected void Page_PreRender(object sender, System.EventArgs e) { string classNames = string.Empty; string javaScript = string.Empty; string classNameParam = string.Empty; if ((Form != null) && (Form.Data != null)) { object classNameObj = Form.Data.GetValue("ClassNames"); if(classNameObj!=null) { classNames = classNameObj.ToString(); } } string url = "~/CMSFormControls/Selectors/AlternativeFormSelection.aspx?lblElem=" + lblStatus.ClientID + "&txtElem=" + txtName.ClientID; // Preselect class if (!string.IsNullOrEmpty(ClassName)) { classNameParam = ClassName; } else if (!string.IsNullOrEmpty(classNames)) { string[] splitClassNames = classNames.Split(';'); classNameParam = splitClassNames[0]; } if (!string.IsNullOrEmpty(classNameParam)) { url += "&classname=" + classNameParam; } url += "&hash=" + QueryHelper.GetHash(url, false); javaScript = "function SelectAltFormDialog_" + ClientID + "(){modalDialog('" + ResolveUrl(url) + "','AltFormSelection', 400, 500); return false;}"; ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "SelectDialog_" + ClientID, ScriptHelper.GetScript(javaScript)); ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "SelectAlternativeForm", ScriptHelper.GetScript( "function SelectAltForm(formName,txtClientID,lblClientID){if((lblClientID != '') && (txtClientID != '')) { document.getElementById(txtClientID).value = formName;document.getElementById(lblClientID).innerHTML='';} return false;} " + "function CheckAlternativeForm(result, context){document.getElementById(context).innerHTML = result; return false; } " )); ScriptHelper.RegisterDialogScript(Page); } /// /// Returns true if user control is valid. /// public override bool IsValid() { string ValidationResult = Validate(txtName.Text, false); if (ValidationResult == string.Empty) { return true; } else { ValidationError = ValidationResult; return false; } } private string Validate(string value, bool allowPreselect) { if (!string.IsNullOrEmpty(value)) { // If alternative form name contains macro or is not full name, it is always valid if (ContextResolver.ContainsMacro(value) || !ValidationHelper.IsFullName(value)) { return string.Empty; } // Try to get alternative form object AlternativeFormInfo afi = AlternativeFormInfoProvider.GetAlternativeFormInfo(value); if (afi == null) { if (allowPreselect) { // Alternative form does not exist DataClassInfo di = DataClassInfoProvider.GetDataClass(value); if ((di == null) && (value != string.Empty)) { return GetString("altform.selectaltform.notexist").Replace("%%code%%", value); } else { return String.Empty; } } else { return GetString("altforms.selectaltform.formnotexist").Replace("%%code%%", value); } } } return string.Empty; } }