1行に2つのテキストボックスと1つのチェックボックスがあるGridviewがあります。チェックボックスがオンになっている場合は、両方のテキストボックスに必須のフィールド検証が必要です。それ以外の場合は、検証が必要ありません。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CssClass="grvStyle"
Width="100%" OnRowDataBound="GridView1_RowDataBound">
<EmptyDataTemplate>
<asp:Label ID="lblnorow" runat="server" Text="Record not Found"></asp:Label>
</EmptyDataTemplate>
<Columns>
<asp:TemplateField HeaderText="SrNo." ItemStyle-Width="30px" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblsrno" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Dates" HeaderText="Dates">
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Registration Closing Date">
<ItemTemplate>
<asp:TextBox ID="txtCloseDate" runat="server" CssClass="datepicker"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ValidationGroup="VGrpSelect"
ControlToValidate="txtCloseDate" ErrorMessage="Registration Closing Date is required"
Display="none" SetFocusOnError="True">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtCloseDate"
Display="None" ErrorMessage="Invalid Registration Closing Date Format" SetFocusOnError="True"
ValidationGroup="VGrpSelect" ValidationExpression="([1-9]|0[1-9]|[12][0-9]|3[01])[- /.]([1-9]|0[1-9]|1[012])[- /.](19|20|30)\d\d">
</asp:RegularExpressionValidator>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="No of Pax">
<ItemTemplate>
<asp:TextBox ID="txtNoOfPax" runat="server" Width="50"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ValidationGroup="VGrpSelect"
ControlToValidate="txtNoOfPax" ErrorMessage="No of Pax is required"
Display="none" SetFocusOnError="True">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ValidationGroup="VGrpSelect"
SetFocusOnError="True" Display="None" ErrorMessage="No of Pax must have Numeric value"
ControlToValidate="txtNoOfPax" ValidationExpression="\d*">
</asp:RegularExpressionValidator>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-HorizontalAlign="Center" HeaderStyle-Width="60px"
ItemStyle-HorizontalAlign="Center">
<HeaderTemplate>
Select<asp:CheckBox ID="chkHeaderSelect" runat="server" onclick="SetCheckBox(this)" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" onclick="CheckCheckBox()" />
<asp:HiddenField ID="hidf" runat="server" Value='<%# Eval("Dates") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle CssClass="data-row" />
<AlternatingRowStyle CssClass="alt-data-row" />
<PagerStyle HorizontalAlign="Right" ForeColor="Black" />
</asp:GridView>
<script type="text/javascript">
function GVal(chk, req) {
if (chk.Checked == true) {
req.Enabled = true;
}
else {
req.Enabled = false;
}
}
</script>
gridviewのrowdataboundイベントで
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblSrNo = (Label)e.Row.FindControl("lblSrNo");
int srno = e.Row.DataItemIndex + 1;
lblSrNo.Text = srno.ToString();
CheckBox chkSelect = (CheckBox)e.Row.FindControl("chkSelect");
TextBox txtCloseDate = (TextBox)e.Row.FindControl("txtCloseDate");
RequiredFieldValidator required = (RequiredFieldValidator)e.Row.FindControl("RequiredFieldValidator10");
chkSelect.Attributes.Add("onclick", "javascript:GVal(" + chkSelect.ClientID + "," + required.ClientID + ");");
}
}
私はこのようなことをしましたが、それはすべての行の検証を示しています。