Ad

How To Control Which Validators Are Used In A DataGrid

I'm developing a web page in Asp.Net 1.1 and have a DataGrid which allows users to add, edit, and delete database records. The footer row contains textboxes to allow the adding of new records.

For each column I've defined <ItemTemplate>, <EditItemTemplate> and <FooterItemTemplate> elements. The FooterItemTemplate and EditItemTemplate elements in my aspx markup both contain RequiredFieldValidator controls as well as text boxes. (see below)

<asp:TemplateColumn HeaderText="Offer Code">
  <ItemTemplate>
    <%# DataBinder.Eval(Container, "DataItem.OfferCode") %>
  </ItemTemplate>
  <FooterTemplate>
    <asp:TextBox ID="txtNewOfferCode" Runat="server" />
<asp:RequiredFieldValidator ID="reqNewOfferCode" ControlToValidate="txtNewOfferCode" Display="None" ErrorMessage="Please specify 'Offer Code'" Runat="server" />
  </FooterTemplate>
  <EditItemTemplate>
<asp:TextBox id=txtOfferCode Runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.OfferCode") %>' />
<asp:RequiredFieldValidator ID="reqOfferCode" ControlToValidate="txtOfferCode" Display="None" ErrorMessage="Please specify 'Offer Code'" Runat="server" />
  </EditItemTemplate>
</asp:TemplateColumn>

The problem I have is that when you try to edit an existing record, the blank fields in the footer row prevent validation and hence postback and updating of the selected row.

Ad

Answer

Normally, the GridView is not able to insert new rows. You did the trick, using the footer row. Of course, when you submit your GridView, the validators block the process because the field are empty. You should trap the event when your GridView goes in edit mode and disable the footer's validators. Don't forget to enable them when you leave the GridView edit mode.

Ad
source: stackoverflow.com
Ad