gridview在条目有效时在emptydatatemplate中触发验证

时间:2015-03-24 16:37:50

标签: asp.net validation gridview

我有一个Gridview,它有3列,ID,Text,Action。我正在使用VS2010。当网格为空并且用户在文本字段中输入文本时,我希望将该字段验证为必填字段和最大值。长度。如果网格中有数据,但这些验证工作正常,但当网格为空时,在用户输入数据后触发所需的验证。如果用户第二次进入,则数据已成功添加到数据库并在网格中刷新。

其次,当网格为空时,列标题不显示,即使我有属性:ShowHeaderWhenEmpty =" true" 这是我的标记:

<asp:GridView ID="SubjectInfoGridView" runat="server" 
        AutoGenerateColumns="false" Caption="Personal Subject List" 
        CaptionAlign="Top" CssClass="grid"
        RowStyle-Wrap="true" HorizontalAlign="Left" ShowFooter="true" 
        AllowPaging="false" PageSize="5" ShowHeaderWhenEmpty="true" 
        onrowcancelingedit="SubjectInfoGridView_RowCancelingEdit" 
        onrowediting="SubjectInfoGridView_RowEditing" 
        onrowdeleting="SubjectInfoGridView_RowDeleting" 
        onrowcommand="SubjectInfoGridView_RowCommand" 
        onrowupdating="SubjectInfoGridView_RowUpdating">
        <Columns>
            <asp:TemplateField HeaderText="Subject ID">
                <ItemTemplate>
                    <asp:Label ID="sigvLblSubjectID" runat="server" Text='<%# Bind("SubjectID") %>'></asp:Label>
                </ItemTemplate>
             </asp:TemplateField>
             <asp:TemplateField HeaderText="Subject" ItemStyle-Wrap="false"> 
             <ItemTemplate>
                    <asp:Label ID="sigvLblSubject" runat="server" Text='<%# Bind("Subject") %>'></asp:Label>
             </ItemTemplate>
             <EditItemTemplate>
                <asp:TextBox ID="sigvTxtBoxEditSubject" runat="server" Text='<%# Bind("Subject") %>'></asp:TextBox>      
                     <asp:RequiredFieldValidator ID="RequiredFieldEditSubject" ControlToValidate="sigvTxtBoxEditSubject" runat="server"
                        ErrorMessage="Required field." ValidationGroup="EditSubjectValidation" Display="Dynamic" CssClass="message-error">
                    </asp:RequiredFieldValidator>            
                     <asp:RegularExpressionValidator ID="MaxValEditSubject" ControlToValidate="sigvTxtBoxEditSubject" runat="server"
                        ErrorMessage="Maximumn length is 80." ValidationGroup="EditSubjectValidation" Display="Dynamic" CssClass="message-error"
                        ValidationExpression="^.{1,80}$" >
                    </asp:RegularExpressionValidator>
             </EditItemTemplate>
             <FooterTemplate>
                    <asp:TextBox ID="sigvTxtBoxInsertSubject" runat="server" Text='<%# Bind("Subject") %>'></asp:TextBox>      
                     <asp:RequiredFieldValidator ID="RequiredFieldInsertSubject" ControlToValidate="sigvTxtBoxInsertSubject" runat="server"
                        ErrorMessage="Required field." ValidationGroup="InsertSubjectValidation" Display="Dynamic" CssClass="message-error">
                    </asp:RequiredFieldValidator>            
                     <asp:RegularExpressionValidator ID="MaxValInsertSubject" ControlToValidate="sigvTxtBoxInsertSubject" runat="server"
                        ErrorMessage="Maximumn length is 80." ValidationGroup="InsertSubjectValidation" Display="Dynamic" CssClass="message-error"
                        ValidationExpression="^.{1,80}$" >
                    </asp:RegularExpressionValidator>
             </FooterTemplate>
             </asp:TemplateField>
             <asp:TemplateField HeaderText="Action" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center">
                 <ItemTemplate>
                    <asp:Button ID="sigvEditButton" runat="server" CausesValidation="False" CommandName="Edit" 
                                Text="Edit" CssClass="gridActionbutton">
                    </asp:Button>
                    &nbsp;<asp:Button ID="sigvDeleteButton" runat="server" CausesValidation="False" CommandName="Delete" 
                                Text="Delete" CssClass="gridActionbutton"  OnClientClick="return confirm('Are you sure you want to delete this Device Information?')" >
                    </asp:Button>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Button ID="sigvUpdateButton" runat="server" CausesValidation="True" ValidationGroup="EditSubjectValidation" CommandName="Update" 
                                    Text="Update" CssClass="gridActionbutton"></asp:Button>
                    &nbsp;<asp:Button ID="sigvCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" 
                                    Text="Cancel" CssClass="gridActionbutton"></asp:Button>
                </EditItemTemplate>                  
                <FooterTemplate>
                    <asp:Button ID="sigvAddButton" runat="server" CommandName="Add" Text="Add Subject" Width="90%" CausesValidation="true" 
                                CssClass="gridActionbutton" ValidationGroup="InsertSubjectValidation">
                    </asp:Button>
                </FooterTemplate>
            </asp:TemplateField>
       </Columns>
       <EmptyDataTemplate>
        <tr> 
            <td colspan="3" style="text-align:center;">
                No User-defined Subjects were found for you. Subjects can be added by clicking the 'Add Subject' Button.
            </td> 
        </tr>
        <tr>
            <td></td>
            <td>
                <asp:TextBox ID="sigvTxtBoxInsertSubject" runat="server" Text='<%# Bind("Subject") %>' Width="90%"></asp:TextBox>      
                <asp:RequiredFieldValidator ID="RequiredFieldInsertSubjectEmpty" ControlToValidate="sigvTxtBoxInsertSubject" runat="server"
                        ErrorMessage="Required field." ValidationGroup="InsertSubjectValidation" Display="Dynamic" CssClass="message-error">
                </asp:RequiredFieldValidator>            
                <asp:RegularExpressionValidator ID="MaxValInsertSubjectEmpty" ControlToValidate="sigvTxtBoxInsertSubject" runat="server"
                        ErrorMessage="Maximumn length is 80." ValidationGroup="InsertSubjectValidation" Display="Dynamic" CssClass="message-error"
                        ValidationExpression="^.{1,80}$" >
                </asp:RegularExpressionValidator>              
            </td>
            <td>
                <asp:Button ID="sigvAddButtonEmpty" runat="server" CommandName="Add" Text="Add Subject" Width="90%" CausesValidation="true" 
                        CssClass="gridActionbutton" ValidationGroup="InsertSubjectValidation">
                </asp:Button>
            </td>
        </tr>
       </EmptyDataTemplate>
    </asp:GridView>

那么,为什么当网格为空时标题显示为什么,以及在字段中输入数据后触发RequiredValidation的验证我做错了什么?

谢谢!

更新 我解决了标题问题&#39;但只是将它们添加到EmptyDataTemplate。但验证仍然是一个问题。 我在另一家公司使用VS2012这样做了,这不是问题。我尝试使TextBox ID与插入ID不同,但仍然会出现相同的错误。

1 个答案:

答案 0 :(得分:0)

我明白了...... 如果有人有这个问题。 为了纠正验证,我删除了文本绑定(Text =&#39;&lt;%#Bind(&#34; Subject&#34;)%&gt;&#39;)。然后,当有数据时我添加一行时,我将Subject字段的ID更改为Footer中的ID。这允许我只检查RowBound事件中的一个控件。 这是EmptyDataTemplate中的数据行:

<tr>
            <td></td>
            <td>
                <asp:TextBox ID="sigvTxtBoxInsertSubject" runat="server" Width="90%"></asp:TextBox>      
                <asp:RequiredFieldValidator ID="RequiredFieldInsertSubjectEmpty" ControlToValidate="sigvTxtBoxInsertSubject" runat="server"
                        ErrorMessage="Required field." ValidationGroup="InsertSubjectValidationEmpty" Display="Dynamic" CssClass="message-error">
                </asp:RequiredFieldValidator>            
                <asp:RegularExpressionValidator ID="MaxValInsertSubjectEmpty" ControlToValidate="sigvTxtBoxInsertSubject" runat="server"
                        ErrorMessage="Maximumn length is 80." ValidationGroup="InsertSubjectValidationEmpty" Display="Dynamic" CssClass="message-error"
                        ValidationExpression="^.{1,80}$" >
                </asp:RegularExpressionValidator>                              
            </td>
            <td>
                <asp:Button ID="sigvAddButtonEmpty" runat="server" CommandName="Add" Text="Add Subject" Width="90%" CausesValidation="true" 
                        CssClass="gridActionbutton" ValidationGroup="InsertSubjectValidationEmpty" >
                </asp:Button>
            </td>
        </tr>