[英]custom validator of checkbox in gridview only mark one row invalid
I have a gridview with 1 to 4 item checkboxes plus a checkbox to select "none" in each row. 我有一个带有1到4个项目复选框的gridview以及一个在每一行中选择“无”的复选框。
On submit I am trying to return an error to a row if "none" and any other item is selected in a row. 提交时,如果连续选择“无”和任何其他项目,我将尝试将错误返回到一行。 I am using a custom validator which is working for the first invalid row, but does not error subsequent invalid rows. 我正在使用一个自定义验证器,该验证器适用于第一个无效行,但不会使随后的无效行出错。 When I correct the first invalid row and re-submit the validation method runs again and invalid rows are found, but the page is returned as valid. 当我更正第一个无效行并重新提交时,验证方法将再次运行并找到无效行,但该页面将作为有效返回。
How can I get the validator to error all invalid rows, or how can I set the page as invalid when an invalid row is found? 如何让验证器对所有无效行进行错误处理,或者当发现无效行时如何将页面设置为无效?
Update - I found that this issue only happens when the last row is invalid. 更新-我发现仅当最后一行无效时才会发生此问题。 If the last row is invalid the Page.IsValid flag is not set to false. 如果最后一行无效,则Page.IsValid标志不会设置为false。 I also found that validation, noneChecked, runs once for each row; 我还发现验证,noneChecked,每行运行一次; so if there are four rows it validates each row four times. 因此,如果有四行,它将对每一行进行四次验证。
<asp:TemplateField HeaderText="Items">
<ItemTemplate>
<span class="qdsdata">
<asp:PlaceHolder ID="PlaceHolder12" runat="server" Visible='<%# !DBNull.Value.Equals(Eval("records"))%>'>
<asp:CheckBox ForeColor="#FF5050" runat="server" ID="cbr" />
Records<br />
</asp:PlaceHolder>
<asp:PlaceHolder ID="PlaceHolder7" runat="server" Visible='<%# !DBNull.Value.Equals(Eval("xray"))%>'>
<asp:CheckBox ForeColor="#FF5050" runat="server" ID="cbx" />
X-Rays<br />
</asp:PlaceHolder>
<asp:PlaceHolder ID="PlaceHolder8" runat="server" Visible='<%# !DBNull.Value.Equals(Eval("path"))%>'>
<asp:CheckBox ForeColor="#FF5050" runat="server" ID="cbp" />
Pathology<br />
</asp:PlaceHolder>
<asp:PlaceHolder ID="PlaceHolder9" runat="server" Visible='<%# !DBNull.Value.Equals(Eval("echo"))%>'>
<asp:CheckBox ForeColor="#FF5050" runat="server" ID="cbe" />
Echocardiogram<br />
</asp:PlaceHolder>
<hr style="width: 60%;" />
<asp:CheckBox ForeColor="#FF5050" runat="server" ID="cbnone" ValidationGroup="valgroup1" />
None<br />
<asp:CustomValidator ID="vcbnone" runat="server" SetFocusOnError="true" OnServerValidate="noneChecked"
ValidationGroup="valgroup1" CssClass="r" ErrorMessage="Uncheck None"></asp:CustomValidator>
</span>
</ItemTemplate>
</asp:TemplateField>
Code behind 后面的代码
public void noneChecked(object source, ServerValidateEventArgs args)
{
foreach (GridViewRow gvrow in Location.Rows)
{
bool cbnone = ((CheckBox)gvrow.FindControl("cbnone")).Checked;
bool cbr = ((CheckBox)gvrow.FindControl("cbr")).Checked;
bool cbx = ((CheckBox)gvrow.FindControl("cbx")).Checked;
bool cbp = ((CheckBox)gvrow.FindControl("cbp")).Checked;
bool cbe = ((CheckBox)gvrow.FindControl("cbe")).Checked;
if ((cbr || cbx || cbp || cbe) && cbnone)
{
((CustomValidator)gvrow.FindControl("vcbnone")).IsValid = false;
}
}
}
I found the issue. 我发现了问题。 I was looping through the gridrows each time the OnServerValidation noneChecked was called in the CustomValidator and it is called for each row in the grid, so if there are 4 rows in the grid it would loop 16 times. 每次在CustomValidator中调用OnServerValidation noneChecked时,我都会遍历网格行,并为网格中的每一行调用它,因此,如果网格中有4行,它将循环16次。 I corrected this and the validation now works perfect. 我已对此进行更正,现在验证可以正常进行了。
New correct code behind: 后面的新正确代码:
public void noneChecked(object source, ServerValidateEventArgs args)
{
GridViewRow gvrow = (GridViewRow)(source as Control).Parent.Parent;
bool cbnone = ((CheckBox)gvrow.FindControl("cbnone")).Checked;
bool cbr = ((CheckBox)gvrow.FindControl("cbr")).Checked;
bool cbx = ((CheckBox)gvrow.FindControl("cbx")).Checked;
bool cbp = ((CheckBox)gvrow.FindControl("cbp")).Checked;
bool cbe = ((CheckBox)gvrow.FindControl("cbe")).Checked;
if ((cbr || cbx || cbp || cbe) && cbnone)
{
((CustomValidator)gvrow.FindControl("vcbnone")).IsValid = false;
args.IsValid = false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.