views:

349

answers:

1

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.

+2  A: 

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.

Fabian Vilers
How do you access the validators then? I don't have programmatic access to them. I can access the EditItemTemplate's validators via the Edit event's argument but I can't see how to get at the FooterItemTemplate validators.
mdresser
If you have access to the EditItemTemplate object, you'll be able to access the GridView and then the FooterTemplate. This will give ugly code :-(Maybe the best thing to do is to stop using the FooterTemplate and add a blank line to the GridView when you want to insert a new record.
Fabian Vilers