Does a custom validator work in FormView?

I have a google search and it has been found that many people are struggling with this problem, but I still have 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 duplicated or not, it should be checked on the server side of the script, because it needs to check the database.

updated May 4, 2011, 19.32 // I am adding a form view attribute here, so someone can indicate that something is 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 the insert command as the Insert button, Cause Validation is 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 installed as above

When I click the button and try to set a breakpoint on the serverValidate event, I did not even get there

 protected void cvdLanguageCodeNameDuplicate_ServerValidate(object source, ServerValidateEventArgs args) { if (LanguageHelper.HaveLanguageCode(args.Value)) { args.IsValid = false; } } 

Right now I am using a shortcut instead of a custom validator, checking if this value is valid or not in the FormView_ItemInserting event, if the value is not valid, I just use e.Cancel (FormViewInsertEventArgs) and make the shortcut visible. but still, I want to know if the custom validator is working in formview or something is wrong.

Thanks.

The following code does not address the issue , but may be useful if someone is looking for this topic and may have the same problem. I have to repeat this a lot of time, so I am making a reuse class for this event (using the label as a 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 used 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); } 
+4
source share
3 answers

I just find out a more correct solution, we have to Call the page. Validate () and check if (Page.IsValid) before proceeding. if a ValidationGroup is assigned, Call Page.Validate ("groupNameHere")

0
source

You need to set the button validation group and CustomValidator.

try it

 <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> 
+1
source

The senior programmer in our team just found out that we need to put the UpdateControlPanel in CustomValidators so that it appears in the EditFormView when the server-side validation has been confirmed, and its actual state is false. To check on the client side, it always works correctly.

0
source

All Articles