Does custom validator works in FormView?

拜拜、爱过 提交于 2019-12-10 15:54:26

问题


I have search through google and found that a lot of people is struggling with this issue but i still not found the right answer.

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

I have a form view and need to check that if the language code is duplicate or not, it have to be check on server-side script since it needed to be check through database.

updated 4 may 2011, 19.32 // I add attribute of form view here, so someone may point out if there is anything wrong.

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

I Use insert command as Insert in button, Cause Validation is set to 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>

Custom Validator is set as above

When i click the button and try putting the breakpoint at serverValidate event, i didn't even reach there

protected void cvdLanguageCodeNameDuplicate_ServerValidate(object source, ServerValidateEventArgs args)
    {

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

As for now i use label instead of custom validator by checking if the value is valid or not in the event FormView_ItemInserting, if the value is not valid i just use e.Cancel (FormViewInsertEventArgs) and make the label visible. but still, i want to know if the custom validator is not works on formview or did i do something wrong.

Thank you.

The following code is not about the question, but it might be useful to someone search to this topic and might have same issue. i have to repeat this a lot of time so i make reusable class for this event ( using label as validator)

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


}

When using in 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);
    }

回答1:


You need to set the validation group of the button and CustomValidator .

try this

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



回答2:


Senior programmer in our team just found out that we need to put UpdateControlPanel in the CustomValidators in order to make it show on the EditFormView When Server-Side Validation has made and its valid state is false. For client side validation,its always work correctly.




回答3:


I just find out the more proper solution, we must Call Page.Validate() and checking if(Page.IsValid) before proceed. if the ValidationGroup assigned, Call Page.Validate("groupNameHere")



来源:https://stackoverflow.com/questions/5880359/does-custom-validator-works-in-formview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!