加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 自定义验证器在FormView中是否有效?

发布时间:2020-12-15 21:17:42 所属栏目:百科 来源:网络整理
导读:我通过谷歌搜索,发现很多人都在努力解决这个问题,但我仍然没有找到正确的答案. http://i.stack.imgur.com/15jen.png 我有一个表单视图,需要检查语言代码是否重复,必须检查服务器端脚本,因为它需要通过数据库检查. 2011年5月4日更新,19.32 //我在这里添加了表
我通过谷歌搜索,发现很多人都在努力解决这个问题,但我仍然没有找到正确的答案.

http://i.stack.imgur.com/15jen.png

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

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">

//

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

我使用insert命令作为Insert in按钮,原因验证设置为true.

<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">    </asp:CustomValidator>

自定义验证器设置如上

当我点击按钮并尝试将断点放在serverValidate事件时,我甚至没有到达那里

protected void cvdLanguageCodeNameDuplicate_ServerValidate(object source,ServerValidateEventArgs args)
    {

        if (LanguageHelper.HaveLanguageCode(args.Value))
        {
            args.IsValid = false;
        }
    }

至于现在我通过在事件FormView_ItemInserting中检查值是否有效来使用label而不是自定义验证器,如果值无效,我只需使用e.Cancel(FormViewInsertEventArgs)并使标签可见.但仍然,我想知道自定义验证器是否不适用于formview或我做错了什么.

谢谢.

以下代码与此问题无关,但对于搜索此主题的人员可能会有相同的问题.我必须重复这个很多时间,所以我为这个事件制作了可重复使用的类(使用label作为验证器)

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,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


}

在Form Load中使用时

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);
    }

解决方法

您需要设置按钮和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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读