如何在asp.net中对gridview下拉列表和文本框进行验证?

时间:2013-12-30 05:10:33

标签: c# asp.net gridview

我试过这个,但它不起作用:

 function validationsBirdstra() {
 var grid = document.getElementById('<%=GVBirdstransfer.ClientID%>');
        var theTextBoxvalue = grid.rows[0].cells[1].children[0].value;
        if (theTextBoxvalue == "") {
            alert('enter Type please');
            return false;
        }
 }

我的gridview是

    <table style="clear: both; float: none; margin-right: auto; margin-left: auto; font:  13px Verdana;">
            <tbody>
                <tr>
                    <td>      
   <asp:GridView ID="GVBirdstransfer" runat="server" BorderColor="#BDBDBD"    ShowFooter="True"
                            CssClass="center"  OnRowDataBound="GVBirdstransfer_RowDataBound" GridLines="Horizontal"
                            OnRowDeleting="GVBirdstransfer_RowDeleting" AutoGenerateColumns="False">
                            <EmptyDataTemplate>
                                No Records found</EmptyDataTemplate>
                            <Columns>
                                <asp:TemplateField ItemStyle-Width="20px" HeaderText="SNo">
                                    <ItemTemplate>
                                        <%#Container.DataItemIndex+1 %>
                                    </ItemTemplate>
                                    <ItemStyle Width="20px" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Type">
                                    <HeaderStyle HorizontalAlign="Center" />
                                    <ItemTemplate>
                                        <asp:DropDownList ID="ddltype" runat="server"  AutoPostBack="true" OnSelectedIndexChanged="ddltype_SelectedIndexChanged">
                                        </asp:DropDownList>
                                        <asp:RequiredFieldValidator ID="RFVbirdtype"  runat="server" ControlToValidate="ddltype"
                                            ValidationGroup="GridView1"  InitialValue="Select Me" Display="Dynamic" ErrorMessage="Please select Type"></asp:RequiredFieldValidator>
                                        <asp:ValidatorCalloutExtender ID="VCEbirdtype" runat="Server" TargetControlID="RFVbirdtype"
                                            EnableViewState="false" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="To Branch">
                                    <HeaderStyle HorizontalAlign="Center" />
                                    <ItemTemplate>
                                        <asp:DropDownList ID="ddltobranch" runat="server" AutoPostBack="true">
                                            <asp:ListItem Value="0" Text="--Select--"></asp:ListItem>
                                        </asp:DropDownList>
                                        <%--<asp:RequiredFieldValidator ID="RFVfrmbranch" runat="server" ControlToValidate="ddltobranch"
                                            ValidationGroup="GridView1" InitialValue="0" Display="None" ErrorMessage="Please select Branch "></asp:RequiredFieldValidator>
                                        <asp:ValidatorCalloutExtender ID="VCEfrmbranch" runat="Server" TargetControlID="RFVfrmbranch"
                                            EnableViewState="false" />--%>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Birds">
                                    <HeaderStyle HorizontalAlign="Center" />
                                    <ItemTemplate>
                                        <asp:TextBox ID="txtbirds" Width="70px" MaxLength="15" runat="server" onkeypress="return validatenumerics(event)"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="txtbirds"
                                            ValidationGroup="GridView1" Display="Static" Text="*"></asp:RequiredFieldValidator>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Mortality">
                                    <HeaderStyle HorizontalAlign="Center" />
                                    <ItemTemplate>
                                        <asp:TextBox ID="txtmortality" Width="70px" MaxLength="15" runat="server" onkeypress="return validatenumerics(event)"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="rfv1" runat="server" ControlToValidate="txtmortality"
                                            ValidationGroup="GridView1" Display="Static" Text="*"></asp:RequiredFieldValidator>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Weight">
                                    <HeaderStyle HorizontalAlign="Center" />
                                    <ItemTemplate>
                                        <asp:TextBox ID="txtweight" Width="70px" MaxLength="15" runat="server" onkeypress="return validatenumerics(event)"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="rfv2" runat="server" ControlToValidate="txtweight"
                                            ValidationGroup="GridView1" Display="Static" Text="*"></asp:RequiredFieldValidator>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="GRNo">
                                    <HeaderStyle HorizontalAlign="Center" />
                                    <ItemTemplate>
                                        <asp:TextBox ID="txtgrno" Width="70px" MaxLength="15" runat="server" AutoPostBack="true"
                                            onkeypress="return validatenumerics(event)"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="rfv3" runat="server" ControlToValidate="txtgrno"
                                            ValidationGroup="GridView1" Display="Static" Text="*"></asp:RequiredFieldValidator>
                                    </ItemTemplate>
                                    <FooterStyle HorizontalAlign="Right" />
                                    <FooterTemplate>
                                        <asp:Button ID="BtnAdd" runat="server" Text="Add" OnClick="BtnAdd_Click" ValidationGroup="GridView1"
                                            CausesValidation="true" />
                                    </FooterTemplate>
                                    <ItemStyle Width="100px" />
                                </asp:TemplateField>
                                <asp:CommandField ShowDeleteButton="True" />
                            </Columns>
                        </asp:GridView>
                    </td>
                </tr>
                <%--  <tr>
                    <td align="center" style="padding-right: 10px">
                        <b>Total Weight:</b>
                        <asp:TextBox ID="txtTotal1" runat="server" Width="80px" />
                    </td>
                </tr>--%>
                <tr>
                    <td align="center">
                        <asp:Button ID="btnsave" runat="server" Text="Save" OnClick="btnSave_Click" OnClientClick="return validationsBirdstra();"
                            ValidationGroup="GridView1" CausesValidation="true" Style="height: 26px" />
                    </td>
                </tr>
            </tbody>
        </table>

我创建了gridview。我有一些下拉列表和一些文本框。现在我想对那些进行验证。

2 个答案:

答案 0 :(得分:0)

如果您使用的是RequiredFieldValidator,那么为什么要使用Javascript。只需在按钮中删除OnClientClick并尝试,它将触发。

在RequiredFieldValidator中将显示设置为动态

试试这个

<table style="clear: both; float: none; margin-right: auto; margin-left: auto; font:  13px Verdana;">
            <tbody>
                <tr>
                    <td>      
   <asp:GridView ID="GVBirdstransfer" runat="server" BorderColor="#BDBDBD"    ShowFooter="True"
                            CssClass="center"  OnRowDataBound="GVBirdstransfer_RowDataBound" GridLines="Horizontal"
                            OnRowDeleting="GVBirdstransfer_RowDeleting" AutoGenerateColumns="False">
                            <EmptyDataTemplate>
                                No Records found</EmptyDataTemplate>
                            <Columns>
                                <asp:TemplateField ItemStyle-Width="20px" HeaderText="SNo">
                                    <ItemTemplate>
                                        <%#Container.DataItemIndex+1 %>
                                    </ItemTemplate>
                                    <ItemStyle Width="20px" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Type">
                                    <HeaderStyle HorizontalAlign="Center" />
                                    <ItemTemplate>
                                        <asp:DropDownList ID="ddltype" runat="server"  AutoPostBack="true" OnSelectedIndexChanged="ddltype_SelectedIndexChanged">
                                        </asp:DropDownList>
                                        <asp:RequiredFieldValidator ID="RFVbirdtype"  runat="server" ControlToValidate="ddltype"
                                            ValidationGroup="GridView1"  InitialValue="Select Me" Display="Dynamic" ErrorMessage="Please select Type"></asp:RequiredFieldValidator>
                                        <asp:ValidatorCalloutExtender ID="VCEbirdtype" runat="Server" TargetControlID="RFVbirdtype"
                                            EnableViewState="false" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="To Branch">
                                    <HeaderStyle HorizontalAlign="Center" />
                                    <ItemTemplate>
                                        <asp:DropDownList ID="ddltobranch" runat="server" AutoPostBack="true">
                                            <asp:ListItem Value="0" Text="--Select--"></asp:ListItem>
                                        </asp:DropDownList>
                                        <%--<asp:RequiredFieldValidator ID="RFVfrmbranch" runat="server" ControlToValidate="ddltobranch"
                                            ValidationGroup="GridView1" InitialValue="0" Display="None" ErrorMessage="Please select Branch "></asp:RequiredFieldValidator>
                                        <asp:ValidatorCalloutExtender ID="VCEfrmbranch" runat="Server" TargetControlID="RFVfrmbranch"
                                            EnableViewState="false" />--%>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Birds">
                                    <HeaderStyle HorizontalAlign="Center" />
                                    <ItemTemplate>
                                        <asp:TextBox ID="txtbirds" Width="70px" MaxLength="15" runat="server" onkeypress="return validatenumerics(event)"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="txtbirds"
                                            ValidationGroup="GridView1" Display="Dynamic" Text="*"></asp:RequiredFieldValidator>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Mortality">
                                    <HeaderStyle HorizontalAlign="Center" />
                                    <ItemTemplate>
                                        <asp:TextBox ID="txtmortality" Width="70px" MaxLength="15" runat="server" onkeypress="return validatenumerics(event)"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="rfv1" runat="server" ControlToValidate="txtmortality"
                                            ValidationGroup="GridView1" Display="Dynamic" Text="*"></asp:RequiredFieldValidator>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Weight">
                                    <HeaderStyle HorizontalAlign="Center" />
                                    <ItemTemplate>
                                        <asp:TextBox ID="txtweight" Width="70px" MaxLength="15" runat="server" onkeypress="return validatenumerics(event)"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="rfv2" runat="server" ControlToValidate="txtweight"
                                            ValidationGroup="GridView1" Display="Dynamic" Text="*"></asp:RequiredFieldValidator>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="GRNo">
                                    <HeaderStyle HorizontalAlign="Center" />
                                    <ItemTemplate>
                                        <asp:TextBox ID="txtgrno" Width="70px" MaxLength="15" runat="server" AutoPostBack="true"
                                            onkeypress="return validatenumerics(event)"></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="rfv3" runat="server" ControlToValidate="txtgrno"
                                            ValidationGroup="GridView1" Display="Dynamic" Text="*"></asp:RequiredFieldValidator>
                                    </ItemTemplate>
                                    <FooterStyle HorizontalAlign="Right" />
                                    <FooterTemplate>
                                        <asp:Button ID="BtnAdd" runat="server" Text="Add" OnClick="BtnAdd_Click" ValidationGroup="GridView1"
                                            CausesValidation="true" />
                                    </FooterTemplate>
                                    <ItemStyle Width="100px" />
                                </asp:TemplateField>
                                <asp:CommandField ShowDeleteButton="True" />
                            </Columns>
                        </asp:GridView>
                    </td>
                </tr>
                <%--  <tr>
                    <td align="center" style="padding-right: 10px">
                        <b>Total Weight:</b>
                        <asp:TextBox ID="txtTotal1" runat="server" Width="80px" />
                    </td>
                </tr>--%>
                <tr>
                    <td align="center">
                        <asp:Button ID="btnsave" runat="server" Text="Save" OnClick="btnSave_Click" ValidationGroup="GridView1" CausesValidation="true" Style="height: 26px" />
                    </td>
                </tr>
            </tbody>
        </table>

答案 1 :(得分:0)

浏览网页后,打开页面源并查找网格视图。
您将获得客户端ID。现在用您的客户端ID替换<%=GVBirdstransfer.ClientID%>并检查