C# 自定义验证器在FormView中工作吗?

C# 自定义验证器在FormView中工作吗?,c#,asp.net,formview,customvalidator,custom-validators,C#,Asp.net,Formview,Customvalidator,Custom Validators,我在谷歌上搜索,发现很多人都在为这个问题苦苦挣扎,但我仍然没有找到正确的答案 我有一个表单视图,需要检查语言代码是否重复,它必须在服务器端脚本上进行检查,因为它需要通过数据库进行检查 2011年5月4日更新,19.32 //我在这里添加了表单视图的属性,所以有人可能会指出是否有任何错误 <asp:FormView ID="fmvxLanguage" runat="server" EnableViewState="False" DefaultMode="Insert" Visibl

我在谷歌上搜索,发现很多人都在为这个问题苦苦挣扎,但我仍然没有找到正确的答案

我有一个表单视图,需要检查语言代码是否重复,它必须在服务器端脚本上进行检查,因为它需要通过数据库进行检查

2011年5月4日更新,19.32 //我在这里添加了表单视图的属性,所以有人可能会指出是否有任何错误

<asp:FormView ID="fmvxLanguage" runat="server" EnableViewState="False" DefaultMode="Insert"
    Visible="False" Width="95%" DataSourceID="odsLanguage" DataKeyNames="LanguageCode"
    CssClass="formViewAdd">
至于现在,我使用标签而不是自定义验证器,通过检查FormView_ItemInserting事件中的值是否有效,如果值无效,我只使用e.Cancel(FormViewInsertEventArgs)并使标签可见。但是,我仍然想知道自定义验证器是否在formview上工作,或者我是否做错了什么

<asp:FormView ID="fmvxLanguage" runat="server" EnableViewState="False" DefaultMode="Insert"
    Visible="False" Width="95%" DataSourceID="odsLanguage" DataKeyNames="LanguageCode"
    CssClass="formViewAdd">
多谢各位

以下代码与问题无关,但它可能对搜索此主题的人有用,并且可能存在相同的问题。我必须重复很多次,所以我为这个事件创建了可重用的类(使用标签作为验证器)


您需要设置按钮和CustomValidator的验证组

试试这个

<dxe:ASPxButton ID="btnAddNewLanguage" runat="server" CausesValidation="True" Image-Url="~/images/icon/Save-icon.png" CommandName="Insert" Text="Save" ValidationGroup="V> 
</dxe:ASPxButton> 



<asp:CustomValidator ID="cvdLanguageCodeNameDuplicate" runat="server"                          ControlToValidate="txtLanguageCode" CssClass="IconValidation"                          ErrorMessage="&lt;img src=&quot;/images/icon/validation-Icon.png&quot;/&gt;     Language code name is duplicated."                          onservervalidate="cvdLanguageCodeNameDuplicate_ServerValidate" ValidationGroup="V>    </asp:CustomValidator> 


我们团队的高级程序员刚刚发现,我们需要在CustomValidators中放入UpdateControlPanel,以便在服务器端验证完成且其有效状态为false时,将其显示在EditFormView上。对于客户端验证,它总是能正常工作。

我只是找到了更合适的解决方案,我们必须 调用Page.Validate()并在继续之前检查(Page.IsValid)。
如果分配了ValidationGroup,请致电Page.Validate(“groupNameHere”)

感谢您的回答,但它仍然不起作用。事实上,它在DevExpress GridView的EditForm模式下运行良好,没有设置任何验证组,只是在formview中不起作用。这看起来可能是您以前答案的编辑+扩展,而不是一个全新的答案。。。你应该考虑把它们组合起来。即使我不使用更新面板,它也能工作,所以我不认为我需要组合它,你需要使用面板来工作吗?
protected void cvdLanguageCodeNameDuplicate_ServerValidate(object source, ServerValidateEventArgs args)
    {

        if (LanguageHelper.HaveLanguageCode(args.Value))
        {
            args.IsValid = false;
        }
    }
public class clsFormViewDuplicationValidationSetter
{
    #region Property


    public FormView FormView { get; set; }

    public delegate bool DelDuplicationValidationNameOnly(string pStrName);
    public delegate bool DelDuplicationValidationNameAndId(string pStrName, int primaryId);

    public DelDuplicationValidationNameOnly DuplicationValidationNameOnly;
    public DelDuplicationValidationNameAndId DuplicationValidationDelegationNameAndId;


    public TextBox TextBoxNameToCheckForDuplication { get; set; }
    public Label LabelDuplicationValidationMessage { get; set; }

    #endregion

    #region Constructor

    /// <summary>
    /// Pattern For Simple Duplication ValidationName and Id
    /// </summary>
    /// <param name="pObjFormView">FormView</param>
    /// <param name="pObjTextBoxNameToCheckForDuplication">TextBoxName</param>
    /// <param name="pObjLabelDuplicationValidationMessage">Lable Showing Error Message</param>
    /// <param name="pObjDuplicationValidationNameAndId">Delegation for validation function (name and id)</param>
    public clsFormViewDuplicationValidationSetter(FormView pObjFormView, TextBox pObjTextBoxNameToCheckForDuplication, Label pObjLabelDuplicationValidationMessage, DelDuplicationValidationNameAndId pObjDuplicationValidationNameAndId)
    {
        this.FormView = pObjFormView;
        this.TextBoxNameToCheckForDuplication = pObjTextBoxNameToCheckForDuplication;
        this.LabelDuplicationValidationMessage = pObjLabelDuplicationValidationMessage;
        this.DuplicationValidationDelegationNameAndId = pObjDuplicationValidationNameAndId;
        FormView.ItemInserting += new FormViewInsertEventHandler(FormView_ItemInserting);
    }

    /// <summary>
    /// Pattern For Simple Duplication Validation Name 
    /// </summary>
    /// <param name="pObjFormView">FormView</param>
    /// <param name="pObjTextBoxNameToCheckForDuplication">TextBoxName</param>
    /// <param name="pObjLabelDuplicationValidationMessage">Lable Showing Error Message</param>
    /// <param name="pObjDuplicationValidationDelegation">Delegation for validation function (name)</param>
    public clsFormViewDuplicationValidationSetter(FormView pObjFormView, TextBox pObjTextBoxNameToCheckForDuplication, Label pObjLabelDuplicationValidationMessage, DelDuplicationValidationNameOnly pObjDuplicationValidationNameOnly)
    {
        this.FormView = pObjFormView;
        this.TextBoxNameToCheckForDuplication = pObjTextBoxNameToCheckForDuplication;
        this.LabelDuplicationValidationMessage = pObjLabelDuplicationValidationMessage;
        this.DuplicationValidationNameOnly = pObjDuplicationValidationNameOnly;
        FormView.ItemInserting += new FormViewInsertEventHandler(FormView_ItemInserting);
    }

    void FormView_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        string name = TextBoxNameToCheckForDuplication.Text;


        bool IsDuplicate; 

         // when adding, id always 0
        if (DuplicationValidationDelegationNameAndId != null)
            IsDuplicate = DuplicationValidationDelegationNameAndId(name, 0);
        else
            IsDuplicate = DuplicationValidationNameOnly(name); 

        if (IsDuplicate)
        {
            e.Cancel = true;
            FormView.Visible = true;
            LabelDuplicationValidationMessage.Visible = true;
        }
    }

    #endregion


}
    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox objtxtLanguageCode= (TextBox)fmvxLanguage.FindControl("txtLanguageCode");
        Label objFormViewLabelDuplicationValidationMessage = (Label)fmvxLanguage.FindControl("lblFormViewDuplicate");

        clsFormViewDuplicationValidationSetter objFormViewDuplicationValidationSetter = new clsFormViewDuplicationValidationSetter(fmvxLanguage,objtxtLanguageCode,objFormViewLabelDuplicationValidationMessage,LanguageHelper.HaveLanguageCode);
    }
<dxe:ASPxButton ID="btnAddNewLanguage" runat="server" CausesValidation="True" Image-Url="~/images/icon/Save-icon.png" CommandName="Insert" Text="Save" ValidationGroup="V> 
</dxe:ASPxButton> 



<asp:CustomValidator ID="cvdLanguageCodeNameDuplicate" runat="server"                          ControlToValidate="txtLanguageCode" CssClass="IconValidation"                          ErrorMessage="&lt;img src=&quot;/images/icon/validation-Icon.png&quot;/&gt;     Language code name is duplicated."                          onservervalidate="cvdLanguageCodeNameDuplicate_ServerValidate" ValidationGroup="V>    </asp:CustomValidator>