2012-01-09 47 views
2

簡單的問題?我有一個在頁腳插入行的gridview,我有驗證控制該行和edittemplate如何讓它只驗證該行,因爲當我做一個編輯它也驗證插入行。我只是想驗證插入行或編輯行。驗證GridView中的編輯行和插入行

<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

可以使用

驗證組

定義一個單獨的組用於每個操作

SaveRecords

個&

EditRecords

。必須在保存/編輯按鈕&上指定每個驗證組,以便爲​​該事件觸發它們。 MSDN

0

很長一段時間後 ,但你必須在頁腳的RequiredFieldValidator的ValidationGroup一個一個獨特的名字添加和添加此來的ValidationGroup添加按鈕