我通过谷歌搜索,发现很多人都在努力解决这个问题,但我仍然没有找到正确的答案。
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="<img src="/images/icon/validation-Icon.png"/> 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上是不起作用还是我做错了。
谢谢。
以下代码不是关于这个问题,但对于搜索此主题的人来说可能会有用,但可能会遇到同样的问题。我必须重复这个很多时间,所以我为这个事件制作了可重复使用的类(使用标签作为验证器)
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);
}
答案 0 :(得分:1)
您需要设置按钮和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="<img src="/images/icon/validation-Icon.png"/> Language code name is duplicated." onservervalidate="cvdLanguageCodeNameDuplicate_ServerValidate" ValidationGroup="V> </asp:CustomValidator>
答案 1 :(得分:0)
我们团队的高级程序员刚刚发现我们需要将UpdateControlPanel放在CustomValidators中,以便在服务器端验证发生时将其显示在EditFormView上,并且其有效状态为false。对于客户端验证,它始终正常工作。
答案 2 :(得分:0)
我发现更合适的解决方案,我们必须 在继续之前调用Page.Validate()并检查是否(Page.IsValid)。 如果分配了ValidationGroup,则调用Page.Validate(“groupNameHere”)