neetha mathew

neetha mathew

  • 2k
  • 153
  • 8.9k

Grid View Validation in asp.net for 2 grid View

Jan 27 2022 11:06 AM

In my code, I have two Gridview. One is for adding Education Details and the other for adding Experience details. I want validation to both grid. I used the required field for all the textbox in the grid but it validate the below grid also. In button click add I want to add a javascript for the validation.

<asp:GridView runat="server" ID="gvDetails" CssClass="mydatagrid" HeaderStyle-CssClass="Grdheader" ShowFooter="true" AllowPaging="true" PageSize="10" AutoGenerateColumns="false" ShowHeaderWhenEmpty="true" OnRowDeleting="gvDetails_RowDeleting" OnSelectedIndexChanged="gvDetails_SelectedIndexChanged">

<HeaderStyle CssClass="headerstyle" />
<Columns>
<asp:BoundField DataField="rowid" HeaderText="No" ReadOnly="true" />
<asp:TemplateField HeaderText="Qualification">
<ItemTemplate>
<asp:TextBox ID="txtqualification"    runat="server"  Height="60px" Width="100%" BorderStyle="None" />
    <asp:RequiredFieldValidator ID="rfvQualification" ControlToValidate="txtqualification" runat="server"
                    ErrorMessage="Required" ForeColor="Red"
                    ValidationGroup ="Group_"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Specialization">
<ItemTemplate>
<asp:TextBox ID="txtspecialization" runat="server"  Height="60px" Width="100%" BorderStyle="None" />
        <asp:RequiredFieldValidator ID="rfvSpec" ControlToValidate="txtspecialization" runat="server"
                    ErrorMessage="Required" ForeColor="Red"
                    ValidationGroup ="Group_" ></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
    <asp:TemplateField HeaderText="Board/University"> 
        <ItemTemplate>
        <asp:TextBox id="txtboard" runat="server" Height="60px" Width="100%" BorderStyle="None" />
         <asp:RequiredFieldValidator ID="rfvBoard" ControlToValidate="txtboard" runat="server"
                    ErrorMessage="Required" ForeColor="Red"
                    ValidationGroup ="Group_"></asp:RequiredFieldValidator>  
        </ItemTemplate>
        </asp:TemplateField>
     <asp:TemplateField HeaderText="Year"> 
        <ItemTemplate>
        <asp:TextBox id="txtyear" runat="server"   onkeypress="CheckNumeric(event);"  Height="60px" Width="100%" BorderStyle="None" />
                 <asp:RequiredFieldValidator ID="rfvYear" ControlToValidate="txtyear" runat="server"
                    ErrorMessage="Required" ForeColor="Red"
                    ValidationGroup ="Group_"></asp:RequiredFieldValidator>
        </ItemTemplate>
        </asp:TemplateField>
     <asp:TemplateField HeaderText="Percentage"> 
        <ItemTemplate>
        <asp:TextBox id="txtpercentage"  runat="server" onkeypress="CheckNumeric(event);" Height="60px" Width="100%" BorderStyle="None" />
         <asp:RequiredFieldValidator ID="rfvPerc" ControlToValidate="txtpercentage" runat="server"
                    ErrorMessage="Required" ForeColor="Red"
                    ValidationGroup ="Group_"></asp:RequiredFieldValidator>
        </ItemTemplate>
         <FooterTemplate>
    <asp:Button ID="Button1" runat="server" Text="Add"  ValidationGroup ="Group_" OnClick="gvDetails_SelectedIndexChanged" CssClass="grdadd"/>
</FooterTemplate>
        </asp:TemplateField>
<asp:CommandField ShowDeleteButton="true" DeleteText="X" ControlStyle-CssClass="grdDelete"/>
</Columns>                
    <footerstyle  CssClass="footerGrd"/>
</asp:GridView>
  <asp:GridView runat="server" ID="gvExperience" CssClass="mydatagrid" HeaderStyle-CssClass="Grdheader" ShowFooter="true" AllowPaging="true" PageSize="10" AutoGenerateColumns="false" ShowHeaderWhenEmpty="true" OnRowDeleting="gvExperience_RowDeleting" OnSelectedIndexChanged="gvExperience_SelectedIndexChanged">

<HeaderStyle CssClass="headerstyle" />
<Columns>
<asp:BoundField DataField="rowidE" HeaderText="No" ReadOnly="true" />
<asp:TemplateField HeaderText="Name of Organisation">
<ItemTemplate>
<asp:TextBox ID="txtOrganisation"   runat="server" Height="60px" Width="100%" BorderStyle="None" />
 <asp:RequiredFieldValidator ID="rfvorg" ControlToValidate="txtOrganisation" runat="server"
                    ErrorMessage="Required" ForeColor="Red"
                    ValidationGroup ="Group1_"></asp:RequiredFieldValidator>  
            
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Position held">
<ItemTemplate>
<asp:TextBox ID="txtposition"  runat="server" Height="60px" Width="100%" BorderStyle="None" />
<asp:RequiredFieldValidator ID="rfvpos" ControlToValidate="txtposition" runat="server"
                    ErrorMessage="Required" ForeColor="Red"
                    ValidationGroup ="Group1_"></asp:RequiredFieldValidator>
</ItemTemplate> 
</asp:TemplateField>
    <asp:TemplateField HeaderText="Nature of duty"> 
        <ItemTemplate>
            <asp:TextBox ID="txtNature"  runat="server" Height="60px" Width="100%" BorderStyle="None" />
           <asp:RequiredFieldValidator ID="rfvnature" ControlToValidate="txtNature" runat="server"
                    ErrorMessage="Required" ForeColor="Red"
                    ValidationGroup ="Group1_"></asp:RequiredFieldValidator>
            </ItemTemplate>
        </asp:TemplateField>
     <asp:TemplateField HeaderText="Duration From"> 
        <ItemTemplate>
        <asp:TextBox id="txtDuration"  runat="server" Height="60px" Width="100%" BorderStyle="None" />
          <asp:RequiredFieldValidator ID="rfduratin" ControlToValidate="txtDuration" runat="server"
                    ErrorMessage="Required" ForeColor="Red"
                    ValidationGroup ="Group1_"></asp:RequiredFieldValidator>
            </ItemTemplate>
        </asp:TemplateField>
     <asp:TemplateField HeaderText="Duration
To"> 
        <ItemTemplate>
        <asp:TextBox id="txtTo"   runat="server" Height="60px" Width="100%" BorderStyle="None" />
                   <asp:RequiredFieldValidator ID="rfTo" ControlToValidate="txtTo" runat="server"
                    ErrorMessage="Required" ForeColor="Red"
                    ValidationGroup ="Group1_"></asp:RequiredFieldValidator>  
            </ItemTemplate>
         <FooterTemplate>
    <asp:Button ID="Button2" runat="server" ValidationGroup ="Group1_" Text="Add" OnClick="Button2_Click"  CssClass="grdadd"/>
</FooterTemplate>
        </asp:TemplateField>
<asp:CommandField ShowDeleteButton="true" DeleteText="X" ControlStyle-CssClass="grdDelete"/>
</Columns>           
    <footerstyle  CssClass="footerGrd"/>       
</asp:GridView>

 


Answers (1)