简体   繁体   English

asp:Repeater的OnItemCommand不使用验证器

[英]OnItemCommand of asp:Repeater not working with validator

the function in OnItemCommand of the asp:Repeater is not working randomly. asp:Repeater OnItemCommand中的函数不是随机工作的。 It's a normal one coded like this. 这是一个正常的编码。

<asp:Repeater runat="server" ID="control01" OnItemCommand="control01_OnItemCommand">
    <ItemTemplate>
        <li><asp:LinkButton runat="server" ID="btn_button" CommandArgument='1'>Click</asp:LinkButton></li>        
    </ItemTemplate>
</asp:Repeater>     

After some time spent on debug I found it's related a input text box on page. 花了一些时间在调试上,我发现它与页面上的输入文本框相关。

<asp:TextBox ID="txt_input" runat="server"></asp:TextBox>

Only if it's correct value (validated) the function works well. 只有当它的值正确(有效)时,该功能才能正常工作。

If I remove <asp:RequiredFieldValidator> and <asp:RequiredFieldValidator> on the text box here 如果我在这里的文本框中删除<asp:RequiredFieldValidator><asp:RequiredFieldValidator>

<asp:RegularExpressionValidator ID="rvDecimal" ControlToValidate="txt_input" runat="server" ErrorMessage="Please enter a valid value" ValidationExpression="^(-)?\d+(\.\d\d)?$"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="rfd_input" runat="server" ControlToValidate="txt_input" Text="Please enter a valid value"></asp:RequiredFieldValidator>

OnItemCommand got fired and the function works well. OnItemCommand被解雇了,该功能运行良好。

The question is, how can I keep the validation and make the function in OnItemCommand working? 问题是,如何保持验证并使OnItemCommand的功能正常工作?

If you want the postback to occur even when a validation fails, you can disable the client validation: 如果您希望即使验证失败也会发生回发,您可以禁用客户端验证:

<asp:RegularExpressionValidator ID="rvDecimal" runat="server" EnableClientScript="false" ... />
<asp:RequiredFieldValidator ID="rfd_input" runat="server" EnableClientScript="false" ... />

If you want to see an indicator besides each field, showing that its data is not valid, then the validators should have a Text property. 如果要查看每个字段旁边的指示符,表明其数据无效,则验证程序应具有Text属性。

On the other hand, if you want a list of failed validations, you can add a ValidationSummary to the page and an ErrorMessage to each validator. 另一方面,如果需要失败的验证列表,可以向页面添加ValidationSummary,并为每个验证器添加ErrorMessage

Finally I've solved the problem by adding CausesValidation="false" in the control. 最后,我通过在控件中添加CausesValidation="false"来解决问题。

<asp:LinkButton runat="server" ID="btn_button" CausesValidation="false">

This would skip the validation the trigger the function in OnItemCommand 这将跳过验证触发OnItemCommand的函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在这种情况下中继器控制的onitemcommand不起作用? - Why onitemcommand of repeater control not working in this case? OnItemCommand函数不能与Listview中的ASP Linkbutton一起使用 - OnItemCommand function not working with asp Linkbutton in Listview 将非标准属性添加到R​​epeater中的asp:LinkBut​​ton会阻止OnItemCommand的运行 - Adding Non-Standard Attribute to asp:LinkButton inside Repeater prevents the run of OnItemCommand Repeater中的OnItemCommand可以重定向到新选项卡吗? - Can OnItemCommand in Repeater redirect to a new tab? 必填字段验证器不适用于中继器控件中的下拉列表 - Required field validator not working for dropdownlist in repeater control 分页在 asp 中继器中无法正常工作 - Paging not Working Correctly in asp Repeater 如何使用转发器的OnItemCommand触发文本框的事件,而不使用LinkBut​​ton? - How to fire event of textbox with OnItemCommand of a repeater , without using LinkButton? 与DataPager绑定时,Repeater Onitem命令不会触发。 为什么? - Repeater Onitemcommand won't fire when binded with DataPager. Why? 如何将一个中继器的控件访问到另一个中继器的OnItemCommand()方法中? - How do you access one repeater's control into another repeater's OnItemCommand() method? 中继器内的范围验证器 - Range validator inside repeater
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM