Home
Manage Your Code
Snippet: How to validate field inside a gridview and show errors in message box. (ASP.NET)
Title: How to validate field inside a gridview and show errors in message box. Language: ASP.NET
Description: hi i have gridview ,i want to validate all the field inside gridview my grid view as Views: 1093
Author: abcd123 abcd123 Date Added: 8/20/2012
Copy Code  
  <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                                                                                  CssClass="GridStyle"
                                                                                    DataKeyNames="ID" DataSourceID="SqlDataSource1" Font-Names="Tahoma" Font-Size="11px"
                                                                                    HeaderStyle-CssClass="HeaderStyle" HorizontalAlign="Left" OnDataBound="gvApplication_DataBound"
                                                                                    OnPageIndexChanging="gvApplication_PageIndexChanging" OnRowCancelingEdit="gvApplication_RowCancelingEdit"
                                                                                    OnRowCommand="gvApplication_RowCommand" OnRowCreated="gvApplication_RowCreated"
                                                                                    OnRowDataBound="gvApplication_RowDataBound" OnRowDeleted="gvApplication_RowDeleted"
                                                                                    OnRowEditing="gvApplication_RowEditing" OnRowUpdated="gvApplication_RowUpdated"
                                                                                    OnRowUpdating="gvApplication_RowUpdating" Width="100%">
                                                                                    <%--<RowStyle BorderColor="Silver" BorderStyle="Solid" BorderWidth="1px" ForeColor="Blue"
                                                            Wrap="True" />--%>
                                                                                    <Columns>
                                                                                     
                                                                                             <asp:CommandField 
                       ShowEditButton="True" CausesValidation="true" ValidationGroup="ValGrpEdit"></asp:CommandField>
                                                                                             
                                                                                      
                                                                                        <asp:TemplateField ShowHeader="False">
                                                                                            <ItemTemplate>
                                                                                                <asp:LinkButton ID="lnkDelete" runat="server" CausesValidation="False" 
                                                                                                    CommandName="Delete" Text="Delete"></asp:LinkButton>
                                                                                            </ItemTemplate>
                                                                                        </asp:TemplateField>
                                                                                
                                                                                        <asp:TemplateField HeaderText="Name" SortExpression="Name">
                                                                                            <EditItemTemplate>
                                                                                                <asp:TextBox ID="Name1" runat="server" CssClass="text" Text='<%# Bind("Name") %>'
                                                                                                    ValidationGroup="ValGrpEdit"></asp:TextBox>
                                                                                                <asp:RequiredFieldValidator ID="RequiredName" runat="server" ControlToValidate="Name1"
                                                                                                    ErrorMessage="Please enter a valid Name."  EnableClientScript="true" ValidationGroup="ValGrpEdit" Width="16px">*</asp:RequiredFieldValidator>
                                                                                            </EditItemTemplate>
                                                                                            <ItemTemplate>
                                                                                                <asp:Label ID="Name2" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                                                                                            </ItemTemplate>
                                                                                            <ItemStyle ForeColor="Black" Width="15%" />
                                                                                            <ControlStyle />
                                                                                        </asp:TemplateField>
                                                                          
                                                                                        <asp:TemplateField HeaderText="Support Owner ID" SortExpression="SupportOwnerIDSort">
                                                                                            <ItemTemplate>
                                                                                                <div style="text-align: left; margin-removed 5px; margin-removed 5px;">
                                                                                                    <asp:Label ID="SupportOwnerID2" runat="server" Text='<%# Bind("SupportOwnerID") %>'></asp:Label>
                                                                                                </div>
                                                                                            </ItemTemplate>
                                                                                            <EditItemTemplate>
                                                                                                <asp:TextBox ID="SupportOwnerID1"  ValidationGroup="ValGrpEdit" runat="server" CssClass="text" Text='<%# Bind("SupportOwnerID") %>'></asp:TextBox>
                                                                                                <asp:RequiredFieldValidator ID="ReqSupportOwnerID1" runat="server" ControlToValidate="SupportOwnerID1"
                                                                                                    ErrorMessage="Please enter the ID of the Application Support owner." ValidationGroup="ValGrpEdit"
                                                                                                    Width="16px">*</asp:RequiredFieldValidator>
                                                                                            </EditItemTemplate>
                                                                                            <ItemStyle Width="150px" />
                                                                                        </asp:TemplateField>
                                                                            
                                                                                   
                                                                                        <asp:TemplateField HeaderText="Path" SortExpression="Path">
                                                                                            <EditItemTemplate>
                                                                                                <asp:TextBox ID="Path1"  ValidationGroup="ValGrpEdit" runat="server" CssClass="text" Height="76px" Text='<%# Bind("Path") %>'
                                                                                                    TextMode="MultiLine"></asp:TextBox>
                                                                                                <asp:CustomValidator ID="txtApplicationPathV" ValidationGroup="ValGrpEdit" runat="server"
                                                                                                    ErrorMessage="Please fill in a path either in URL / Path or URL / Path" ClientValidationFunction="ValidateEdit"
                                                                                                    ControlToValidate="Path1" ValidateEmptyText="true">*</asp:CustomValidator>
                                                                                            </EditItemTemplate>
                                                                                            <ItemTemplate>
                                                                                                <asp:Label ID="Path2" runat="server" CssClass="DisplayDesc" Text='<%# Bind("Path") %>'
                                                                                                    Visible="false"></asp:Label>
                                                                                                <asp:LinkButton ID="lnkpath" runat="server" CausesValidation="false" CssClass="datalink"
                                                                                                    PostBackUrl="#" Text='<%# Bind("Path") %>'></asp:LinkButton>
                                                                                                <asp:Panel ID="Panel3" runat="server" CssClass="wrapText">
                                                                                                    <cc1:HoverMenuExtender ID="hvrPath"  runat="server" PopupControlID="pnlPath" PopupPosition="Left"
                                                                                                        TargetControlID="lnkpath">
                                                                                                    </cc1:HoverMenuExtender>
                                                                                                    <asp:Panel ID="pnlPath" runat="server" CssClass="balloonstyle" Style="display: none"
                                                                                                        Width="200px">
                                                                                                        <div>
                                                                                                            <table cellpadding="0" cellspacing="2" width="200px">
                                                                                                                <tr>
                                                                                                                    <td class="LableText">
                                                                                                                        <%# Eval("Path")%>
                                                                                                                    </td>
                                                                                                                </tr>
                                                                                                            </table>
                                                                                                        </div>
                                                                                                    </asp:Panel>
                                                                                                </asp:Panel>
                                                                                            </ItemTemplate>
                                                                                            <ItemStyle ForeColor="Black" />
                                                                                            <ControlStyle />
                                                                                        </asp:TemplateField>
                                                                                      
                                                                          
                                                                                        <asp:TemplateField HeaderText="Category" SortExpression="Category">
                                                                                            <EditItemTemplate>
                                                                                                <asp:DropDownList ID="ddlCategoryList"  ValidationGroup="ValGrpEdit" runat="server" AutoPostBack="True" CssClass="text">
                                                                                                </asp:DropDownList>
                                                                                                <asp:RequiredFieldValidator ID="RequiredCategory" runat="server" ControlToValidate="ddlCategoryList"
                                                                                                    ErrorMessage="Please select a valid Category." ValidationGroup="ValGrpEdit" InitialValue="-- Select Category --">*</asp:RequiredFieldValidator>
                                                                                            </EditItemTemplate>
                                                                                            <ItemTemplate>
                                                                                                <asp:Label ID="Category2" runat="server" Text='<%# Bind("Category") %>'></asp:Label>
                                                                                            </ItemTemplate>
                                                                                            <ItemStyle ForeColor="Black" Width="10%" />
                                                                                            <ControlStyle />
                                                                                        </asp:TemplateField>
                                                                            
                                                                                    </Columns>
                                                                                 
                                                                                </asp:GridView>

my validation summary as

 
 <tr>
                                            <td colspan="4"> 
                                              <div> <asp:ValidationSummary ID="ValSumEdit" runat="server" Font-Bold="False" Font-Names="Verdana" ShowMessageBox="true" DisplayMode="List" ShowSummary="false" 
                                                    Font-Size="10px" HeaderText="Error Message(s):" ValidationGroup="ValGrpEdit" Style="margin-bottom: 23px" EnableClientScript="true"   /> </div> 
                                             </td>
                                            </tr> 
 
Notes
i tryied every possibilities,but am unable to validate the fields,also not able to show message box.please help..