Ejemplo n.º 1
0
        /// <summary>
        /// Does the section generates any client side validation script ?
        /// </summary>
        /// <returns></returns>
        public override bool GeneratesClientSideScript()
        {
            bool flag = false;

            if (base.EnableClientSideValidation)
            {
                for (int i = 0; i < this.ChildQuestions.Count; i++)
                {
                    foreach (AnswerItem item in this.ChildQuestions[i].Answers)
                    {
                        if (item is IClientScriptValidator)
                        {
                            IClientScriptValidator validator = (IClientScriptValidator)item;
                            if ((validator.EnableValidation && (validator.JavascriptFunctionName != null)) && (validator.JavascriptFunctionName.Length != 0))
                            {
                                flag = true;
                                break;
                            }
                        }
                    }
                    if (flag)
                    {
                        return(flag);
                    }
                }
            }
            return(flag);
        }
 /// <summary>
 /// Parse the answer collection and generates
 /// the question client side validation script for the answers / fields
 /// of sections that require it.
 /// </summary>
 protected override string GenerateClientSideValidationCode()
 {
     if (base.EnableClientSideValidation)
     {
         bool          flag    = false;
         StringBuilder builder = new StringBuilder();
         builder.Append(string.Format("<script type=\"text/javascript\" language=\"javascript\"><!--{0}function {1}{2}(){{/*alert('start validation');*/", Environment.NewLine, GlobalConfig.QuestionValidationFunction, this.UniqueID.Replace(":", "_")));
         foreach (AnswerItem item in this.Answers)
         {
             if (item is IClientScriptValidator)
             {
                 IClientScriptValidator validator = (IClientScriptValidator)item;
                 if ((validator.EnableValidation && (validator.JavascriptFunctionName != null)) && (validator.JavascriptFunctionName.Length != 0))
                 {
                     builder.Append(string.Format("if ((document.getElementsByName('{1}').item(0) != null  && !{0}(document.getElementsByName('{1}').item(0))) || (document.getElementById('{1}') != null  && !{0}(document.getElementById('{1}'))) ){{alert('{2} : {3}');return false;}}", new object[] { validator.JavascriptFunctionName, validator.GetControlIdToValidate(), item.Text, validator.JavascriptErrorMessage }));
                     flag = true;
                 }
             }
         }
         builder.Append("return true;}//--></script>");
         if (((this.Page != null) && flag) && (builder != null))
         {
             return(builder.ToString());
         }
     }
     return(null);
 }
 /// <summary>
 /// Does the section generates any client side validation script ?
 /// </summary>
 /// <returns></returns>
 public override bool GeneratesClientSideScript()
 {
     if (base.EnableClientSideValidation)
     {
         foreach (AnswerItem item in this.Answers)
         {
             if (item is IClientScriptValidator)
             {
                 IClientScriptValidator validator = (IClientScriptValidator)item;
                 if ((validator.EnableValidation && (validator.JavascriptFunctionName != null)) && (validator.JavascriptFunctionName.Length != 0))
                 {
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
        /// <summary>
        /// Creates a new answer item instance from the db answer data
        /// </summary>
        /// <param name="answer">The data source to create the instance from</param>
        /// <param name="question">The question container</param>
        /// <param name="defaultSelectionMode">The default selection mode for the
        /// item (Radio, checkbox etc...)</param>
        /// <param name="parentControlUniqueID">
        /// Unique ID required to identify global selection groups
        /// like radiobutton groups
        /// </param>
        /// <param name="showAnswerText">
        /// Assigns the text to the answeritem control ?
        /// </param>
        /// <param name="enableDefaults">
        /// Does the answer set the user default values of fields to the answer web controls
        /// </param>
        public static AnswerItem Create(AnswerData.AnswersRow answer, QuestionItem question, Section section, AnswerSelectionMode defaultSelectionMode, Style answerStyle, ControlRenderMode renderMode, string languageCode, string parentControlUniqueID, bool showAnswerText, VoterAnswersData.VotersAnswersDataTable voterAnswersState, bool enableDefaults)
        {
            AnswerItem item = null;

            try
            {
                if (answer.TypeAssembly != null)
                {
                    item                  = (AnswerItem)Assembly.Load(answer.TypeAssembly).CreateInstance(answer.TypeNameSpace);
                    item.AnswerId         = answer.AnswerId;
                    item.ImageUrl         = answer.IsImageURLNull() ? null : answer.ImageURL;
                    item.ID               = (section == null) ? (GlobalConfig.AnswerItemName + answer.AnswerId) : string.Concat(new object[] { GlobalConfig.AnswerItemName, answer.AnswerId, GlobalConfig.AnswerSectionName, section.SectionUid });
                    item.AnswerStyle      = answerStyle;
                    item.Question         = question;
                    item.QuestionId       = answer.QuestionId;
                    item.RenderMode       = renderMode;
                    item.Text             = new PipeManager().PipeValuesInText(answer.QuestionId, answer.AnswerText, voterAnswersState, languageCode);
                    item.ShowAnswerText   = showAnswerText;
                    item.SectionContainer = section;
                    item.LanguageCode     = languageCode;
                }
            }
            catch (FileNotFoundException)
            {
                item          = new AnswerTextItem();
                item.AnswerId = -1;
                item.Text     = string.Format(ResourceManager.GetString("AnswerTypeAssemblyNotFoundMessage"), answer.TypeAssembly);
                return(item);
            }
            catch (NullReferenceException)
            {
                item          = new AnswerTextItem();
                item.AnswerId = -1;
                item.Text     = string.Format(ResourceManager.GetString("AnswerTypeNotFoundMessage"), answer.TypeNameSpace, answer.TypeAssembly);
                return(item);
            }
            catch (InvalidCastException)
            {
                item          = new AnswerTextItem();
                item.AnswerId = -1;
                item.Text     = string.Format(ResourceManager.GetString("AnswerTypeInvalidMessage"), answer.TypeNameSpace, answer.TypeAssembly);
                return(item);
            }
            if (item is IClientScriptValidator)
            {
                IClientScriptValidator validator = (IClientScriptValidator)item;
                validator.JavascriptCode         = answer.JavascriptCode;
                validator.JavascriptFunctionName = answer.JavascriptFunctionName;
                if (answer.JavascriptErrorMessage != null)
                {
                    validator.JavascriptErrorMessage = (ResourceManager.GetString(answer.JavascriptErrorMessage, languageCode) == null) ? answer.JavascriptErrorMessage : ResourceManager.GetString(answer.JavascriptErrorMessage, languageCode);
                }
                else
                {
                    validator.JavascriptErrorMessage = null;
                }
                validator.EnableValidation = true;
            }
            if (item is IMandatoryAnswer)
            {
                ((IMandatoryAnswer)item).Mandatory = answer.Mandatory;
            }
            if ((item is IRegExValidator) && (answer.RegExpression != null))
            {
                ((IRegExValidator)item).RegExpression             = answer.RegExpression;
                ((IRegExValidator)item).RegExpressionErrorMessage = answer.RegExMessage;
            }
            AnswerSelectionItem item2 = item as AnswerSelectionItem;

            if (item2 != null)
            {
                item2.UniqueGroupId = parentControlUniqueID;
                item2.AnswerId      = answer.AnswerId;
                item2.TypeMode      = (AnswerTypeMode)answer.TypeMode;
                item2.SelectionMode = defaultSelectionMode;
                bool flag = (section == null) ? IsUserSelected(answer.AnswerId, 0, voterAnswersState) : IsUserSelected(answer.AnswerId, section.SectionNumber, voterAnswersState);
                if (flag)
                {
                    item2.Selected = flag;
                }
                else
                {
                    item2.Selected = enableDefaults ? answer.Selected : false;
                }
            }
            if (item is IFieldItem)
            {
                IFieldItem item3 = (IFieldItem)item;
                item3.FieldHeight = answer.FieldHeight;
                item3.FieldWidth  = answer.FieldWidth;
                item3.FieldLength = answer.FieldLength;
            }
            else if (item is AnswerDataSourceItem)
            {
                AnswerDataSourceItem item4 = (AnswerDataSourceItem)item;
                item4.QuestionId = answer.QuestionId;
                item4.AnswerId   = answer.AnswerId;
                if (item4 is AnswerXmlItem)
                {
                    item4.DataSource = answer.XmlDatasource;
                }
                else if (answer.DataSource != null)
                {
                    item4.DataSource = ParseDefaultAnswerText(answer.QuestionId, answer.DataSource, voterAnswersState, languageCode);
                }
                item4.ImageUrl = answer.IsImageURLNull() ? null : answer.ImageURL;
            }
            string str = (section == null) ? GetUserText(answer.AnswerId, 0, voterAnswersState) : GetUserText(answer.AnswerId, section.SectionNumber, voterAnswersState);

            if (str != null)
            {
                item.DefaultText = str;
            }
            else if ((enableDefaults && !answer.IsDefaultTextNull()) && (answer.DefaultText.Length > 0))
            {
                item.DefaultText = ParseDefaultAnswerText(answer.QuestionId, answer.DefaultText, voterAnswersState, languageCode);
            }
            if (item is ExtendedAnswerItem)
            {
                ((ExtendedAnswerItem)item).RestoreProperties();
            }
            return(item);
        }