如何在gridview控件中进行验证?

时间:2013-12-28 10:13:46

标签: asp.net c#-4.0 gridview

 <tr>
 <asp:GridView ID="gvWH" runat="server" BorderColor="#BDBDBD" ShowFooter="True"
 CssClass="center" OnRowDataBound="gvWH_RowDataBound" 
 GridLines="Horizontal" OnRowDeleting="gvWH_RowDeleting"  
AutoGenerateColumns="False">
   <EmptyDataTemplate>
       No Records found
   </EmptyDataTemplate>
        <Columns>
            <asp:TemplateField HeaderText="Type">
                <HeaderStyle HorizontalAlign="Center" />
                     <ItemTemplate>
                     <asp:DropDownList ID="ddlbirdtype" runat="server" 
                         AutoPostBack="true">
                      </asp:DropDownList>
                <asp:RequiredFieldValidator ID="RFVbirdtype" runat="server" 
                      ControlToValidate="ddlbirdtype"  
                  ValidationGroup="GridView1" InitialValue="0" Display="None"
                 ErrorMessage="Please select Bird Type"></asp:RequiredFieldValidator>
              <asp:ValidatorCalloutExtender ID="VCEbirdtype" runat="Server" 
              TargetControlID="RFVbirdtype" EnableViewState="false" />
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Weight">
           <HeaderStyle HorizontalAlign="Center" />
             <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>
  </tr>
  <tr>
     <td align="center">
   <asp:Button ID="btnsave" runat="server" Text="Save" OnClick="btnSave_Click" OnClientClick="return validations1();" ValidationGroup="GridView1" CausesValidation="true"
     Style="height: 26px" />
     </td>
  </tr>

当我点击外部“保存”按钮时,它会显示验证..请帮助我

1 个答案:

答案 0 :(得分:0)

我认为 InitialValue =“0”属性只会让您无法获得验证,请删除它。如果仍然需要这意味着你必须指定dropdownList的First Item,否则只需删除它。

显示设置为动态 试试这个

<tr>
 <asp:GridView ID="gvWH" runat="server" BorderColor="#BDBDBD" ShowFooter="True"
 CssClass="center" OnRowDataBound="gvWH_RowDataBound" 
 GridLines="Horizontal" OnRowDeleting="gvWH_RowDeleting"  
AutoGenerateColumns="False">
   <EmptyDataTemplate>
       No Records found
   </EmptyDataTemplate>
        <Columns>
            <asp:TemplateField HeaderText="Type">
                <HeaderStyle HorizontalAlign="Center" />
                     <ItemTemplate>
                     <asp:DropDownList ID="ddlbirdtype" runat="server" 
                         AutoPostBack="true">
                      </asp:DropDownList>
                <asp:RequiredFieldValidator ID="RFVbirdtype" runat="server" 
                      ControlToValidate="ddlbirdtype"  
                  ValidationGroup="GridView1" Display="Dynamic"
                 ErrorMessage="Please select Bird Type"></asp:RequiredFieldValidator>
              <asp:ValidatorCalloutExtender ID="VCEbirdtype" runat="Server" 
              TargetControlID="RFVbirdtype" EnableViewState="false" />
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Weight">
           <HeaderStyle HorizontalAlign="Center" />
             <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>
  </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>