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 + ");");
    }
}

我做了类似的事情,但它显示了所有行的验证。

4

2 回答 2

1
 function GVal(chk, req) {

        ValidatorEnable(document.getElementById(req),document.getElementById(chk).checked);

}

您可以在此处找到更多详细信息 [ASP.NET 深度验证]:http: //msdn.microsoft.com/en-us/library/aa479045.aspx

于 2012-12-21T21:21:40.143 回答
0

您可以使用自定义验证器和 javascript/jQuery。这可以通过服务器端每行注入。这个SO post可能可以帮助你解决这个问题。

于 2012-12-21T08:16:36.720 回答