验证gridview中的已检查项目

时间:2012-12-21 04:06:52

标签: javascript asp.net validation gridview

我有一个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 + ");");
    }
}

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

2 个答案:

答案 0 :(得分:1)

 function GVal(chk, req) {

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

}

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

答案 1 :(得分:0)

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