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="<img src="/images/icon/validation-Icon.png"/> 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); }