验证GridView中的编辑行和插入行

时间:2012-01-09 23:09:52

标签: asp.net

简单的问题?我在页脚中有一个插入行的gridview,我有该行的验证控件和edittemplate如何让它只验证该行,因为当我进行编辑时,它验证插入行也是如此。我只是想验证插入行或编辑行。

    <asp:GridView ID="PageSettings" runat="server"
    AllowPaging="True" AllowSorting="True"
    AutoGenerateColumns="False"
    AutoGenerateDeleteButton="True"
    AutoGenerateEditButton="True"   
    DataKeyNames="Id"
    DataSourceID="ObjectDataSourcePages"
    OnLoad="PageSettings_Load"         
    OnRowDataBound="PageSettings_DataBound"
    OnRowCommand="PageSettings_RowCommand"
    OnRowCreated="PageSettings_RowCreated"
    OnRowEditing="PageSettings_RowEditing" 
    OnRowCancelingEdit="PageSettings_RowCancelingEdit" 
    OnRowUpdating="PageSettings_RowUpdating"      
    OnPageIndexChanging="PageSettings_PageIndexChanging"
    OnSorting="PageSettings_Sorting" 
    OnSorted="PageSetting_Sorted"    
    PageSize="2"
    ShowFooter="True"
    ShowHeaderWhenEmpty="True">
    <Columns>
        <asp:TemplateField HeaderText="Page Name" HeaderStyle-HorizontalAlign="Left" SortExpression="Name">
            <ItemTemplate>
                <%# Eval("Name") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="Name" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                <asp:RequiredFieldValidator ID="valRequireName" runat="server"
                    ControlToValidate="Name"
                    CssClass="gridview-error"     
                    Display="Dynamic"
                    SetFocusOnError="true">*</asp:RequiredFieldValidator>    
            </EditItemTemplate> 
            <FooterTemplate>
                <asp:TextBox ID="InsertName" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="valRequireInsertName" runat="server"
                    ControlToValidate="InsertName"
                    Display="Dynamic"
                    SetFocusOnError="true">*</asp:RequiredFieldValidator>
            </FooterTemplate>               
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Path" HeaderStyle-HorizontalAlign="Left" SortExpression="Path">
            <ItemTemplate>
                <%# Eval("Path") %>
            </ItemTemplate>
            <EditItemTemplate>                    
                <asp:TextBox ID="Path" runat="server" Text='<%# Bind("Path") %>'></asp:TextBox>
                <asp:RequiredFieldValidator ID="valRequirePath" runat="server"
                    ControlToValidate="Path"
                    Display="Dynamic"
                    SetFocusOnError="true">*</asp:RequiredFieldValidator>    
            </EditItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="InsertPath" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="valRequireInsertPath" runat="server"
                    ControlToValidate="InsertPath"
                    Display="Dynamic"
                    SetFocusOnError="true">*</asp:RequiredFieldValidator>   
            </FooterTemplate>               
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Route Value" HeaderStyle-HorizontalAlign="Left" SortExpression="RouteValue">
            <ItemTemplate>
                <%# Eval("RouteValue") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="RouteValue" runat="server" Text='<%# Bind("RouteValue") %>'></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="InsertRouteValue" runat="server"></asp:TextBox>
            </FooterTemplate>               
        </asp:TemplateField>
        <asp:TemplateField HeaderText="RegExp" HeaderStyle-HorizontalAlign="Left" SortExpression="RegExp">
            <ItemTemplate>
                <%# Eval("RegExp") %>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="RegExp" runat="server" Text='<%# Bind("RegExp") %>'></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="InsertRegExp" runat="server"></asp:TextBox>
            </FooterTemplate>               
        </asp:TemplateField>
    </Columns>                
</asp:GridView>

2 个答案:

答案 0 :(得分:2)

您可以使用

  

验证组

为每个操作定义一个单独的组

  

SaveRecords

&安培;

  

EditRecords

。您必须在“保存/编辑”按钮上指定每个验证组。所有相应的验证控件,以便为该事件触发它们。 MSDN

答案 1 :(得分:0)

经过很长一段时间 但您必须在页脚必填字段中添加一个具有唯一名称的验证组,并添加此验证组以添加按钮