views:

151

answers:

0

In a gridview I want to allow editing and inserting data.
The Insert row should be in the footer row. But that row is not existing if the datasource is empty.
So I have to create an emptydatatemplate and repeat all of my controls.
Together with the edittemplate I have the same set of cells and controls three times, like this examples shows:

<asp:GridView ID="gridPositions" runat="server" AutoGenerateColumns="false" ShowFooter="true">
 <emptydatatemplate>
  <asp:table id="tableEmpty" runat="server">
   <asp:TableRow class="GridHeader brotf">
    <asp:TableCell>Label</asp:TableCell>
   </asp:TableRow>
   <asp:TableRow class="GridRowWhite brotf">
    <asp:TableCell>No Data</asp:TableCell>
   </asp:TableRow>
   <asp:TableRow id="rowEmptyInsert" runat="server">
    <asp:TableCell>
     <asp:RangeValidator ID="valDistFrom" ControlToValidate="txtInput" Type="Integer" MinimumValue="1" MaximumValue="10000" Text="Invalid" Display="Dynamic" runat="server"/>
     <asp:textbox TabIndex="111" id="txtInput" runat="server" width="30" CssClass="InputRight" /> km
    </asp:TableCell>
   </asp:TableRow>
  </asp:table> 
 </emptydatatemplate>
 <Columns>
  <asp:templatefield headertext="Label">
   <itemtemplate><%#Eval("label")%></itemtemplate>
   <edititemtemplate>
    <asp:RangeValidator ID="valDistFrom" ControlToValidate="txtInput" Type="Integer" MinimumValue="1" MaximumValue="10000" Text="Invalid" Display="Dynamic" runat="server"/>
    <asp:textbox TabIndex="111" id="txtInput" runat="server" width="30" CssClass="InputRight" /> km
   </edititemtemplate>
   <footertemplate>
    <asp:RangeValidator ID="valDistFrom" ControlToValidate="txtInput" Type="Integer" MinimumValue="1" MaximumValue="10000" Text="Invalid" Display="Dynamic" runat="server"/>
    <asp:textbox TabIndex="111" id="txtInput" runat="server" width="30" CssClass="InputRight" /> km
   </footertemplate>
  </asp:templatefield>
 </columns>
</asp:GridView>

In this example it's not that bad, because there is only one cell and not too many controls.
But if I want to create a gridview with plenty of cells, its getting weird.

Any suggestions to get rid of the redundant control statements? The best solution would be having the edit row just once, so I did it in former ASP classic projects.