如何在Javascript中验证Devexpress Gridview中的Devexpress文本框

时间:2014-11-21 16:09:57

标签: asp.net-mvc-4 devexpress aspxgridview

我无法弄清楚为什么使用指定正则表达式的焦点输出验证在devex文本框中不起作用。当我从网格中使用文本框时,它开始按要求工作。请提出解决方案。

@Html.DevExpress().GridView(settings =>
{
    settings.Columns.Add(column =>
        {
            column.FieldName = "InYear";
            column.Caption = "In Year";
            column.Width = 100;
            column.ColumnType = MVCxGridViewColumnType.TextBox;
            column.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending; // Default
            column.SortIndex = 1;
            column.CellStyle.HorizontalAlign = HorizontalAlign.Left;
            var txtProperties = column.PropertiesEdit as TextBoxProperties;
            txtProperties.Width = Unit.Percentage(100);
            txtProperties.MaxLength = 4;
            txtProperties.DisplayFormatInEditMode = true;
            txtProperties.ValidationSettings.RequiredField.IsRequired = true;
            txtProperties.ValidationSettings.ValidateOnLeave = true;
            txtProperties.ValidationSettings.RegularExpression.ValidationExpression = @"\d{4}";
            txtProperties.ValidationSettings.RegularExpression.ErrorText = "Expected format is: YYYY";
            txtProperties.ClientSideEvents.ValueChanged = String.Format("function (s, e) {{ OnValueChanged(s, e, '{0}', {1}); }}", "InYear", "0");
            column.SetDataItemTemplateContent(c =>
            {
                if (!(bool)DataBinder.Eval(c.DataItem, "ReadOnly"))
                {
                    Html.DevExpress().TextBox(txtSettings =>
                    {

                        txtSettings.Name = "txtInYear_" + c.KeyValue.ToString();
                        txtSettings.Width = Unit.Percentage(100);
                        txtSettings.Properties.MaxLength = 4;
                        txtSettings.Properties.DisplayFormatInEditMode = true;
                        txtSettings.Properties.ValidationSettings.ValidateOnLeave = true;
                        txtSettings.Properties.ValidationSettings.ValidationGroup = c.KeyValue.ToString();
                        txtSettings.Properties.ValidationSettings.RegularExpression.ValidationExpression = @"[0-9]{4}";
                        txtSettings.Properties.ValidationSettings.RegularExpression.ErrorText = "Expected format is: YYYY";
                        txtSettings.Properties.ClientSideEvents.ValueChanged = String.Format("function (s, e) {{ OnValueChanged(s, e, '{0}', {1}); }}", c.Column.FieldName, c.KeyValue);
                    }).Bind(DataBinder.Eval(c.DataItem, c.Column.FieldName)).Render();


                }
                else
                    Html.DevExpress().Label(lblSettings =>
                    {
                        lblSettings.Name = "lblInYear_" + c.KeyValue.ToString();
                        lblSettings.Width = Unit.Percentage(100);
                    }).Bind(DataBinder.Eval(c.DataItem, c.Column.FieldName).ToString()).Render();
            });
        });
}).Bind(Model).GetHtml()

编辑:我正在使用gridview中的一个按钮来触发更新命令。如何仅触发EditRow的验证事件。请查看附图。

enter image description here 提前致谢

3 个答案:

答案 0 :(得分:0)

您何时需要验证TextBox?在浏览器模式下还是在编辑时?目前,编辑行时需要TextBox。尝试在模板中为TextBox设置IsRequired属性。

txtSettings.Properties.ValidationSettings.RequiredField.IsRequired = true;

它应该有用。

enter image description here

答案 1 :(得分:0)

答案 2 :(得分:0)

经过长时间的努力,我得到了以下答案:

  • 将验证组属性分配给嵌套控件,如下所示

    txtSettings.Properties.ValidationSettings.ValidationGroup = c.KeyValue.ToString();

  • 遍历客户端的密钥值并调用devexpress验证函数 ASPxClientEdit.ValidateEditorsInContainer ,如下所示, i 表示验证组名称

    var isValid =!ASPxClientEdit.ValidateEditorsInContainer(GridName.GetMainElement(),i)

我的方法可能不是最好的,但它对我有很大帮助,我认为这对其他有同样问题的人也有帮助。

谢谢......