title: How to make a TemplateField read-only until the edit button is pressed?
body: <p>I have a <code>GridView</code> with various fields, one of which I have used <code>TemplateFields</code> for in order to have a datepicker in it. I've set the <code>GridView</code> to be editable and for all the other fields, this works great and they stay locked until the <code>edit</code> button is pressed.</p>
  
  <p>Unfortunately, the <code>TemplateField</code> stays editable at all times. I want them to stay readonly until 'Edit' is clicked.</p>
  
  <p>Some code to illustrate what I'm doing.</p>
  
  <p>default.aspx</p>
  
  <pre><code>&lt;asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
  AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
  AllowSorting="True" AutoGenerateEditButton="True" 
  OnPreRender="GridView1_PreRender" 
  CssClass="gvdatatablem, table table-striped table-bordered" 
  DataKeyNames="fmatter" PageSize="25"&gt;
  
  &lt;Columns&gt;
      &lt;asp:TemplateField HeaderText="Letter Sent"&gt;
          &lt;ItemTemplate&gt;
               &lt;asp:TextBox ID="udtltrsent" runat="server" ReadOnly="false" Class='datepicker' &gt;&lt;/asp:TextBox&gt;
          &lt;/ItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
      &lt;asp:CheckBoxField DataField="uheld" HeaderText="Held" 
      SortExpression="uheld" /&gt;
      &lt;asp:BoundField DataField="udtresponse" HeaderText="Response" 
      SortExpression="udtresponse" /&gt;
      &lt;asp:BoundField DataField="clname1" HeaderText="clname1" 
      SortExpression="clname1" readonly="true" /&gt;
  &lt;/Columns&gt;
  
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>Javascript at the bottom to implement <a href="https://datatables.net" rel="nofollow">Datatables</a> and Bootstrap datepicker</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      $(document).ready(function () {
      $('#example').DataTable();
      $('.datepicker').datepicker();
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>Also the GridView1_PreRender method is just to change the table format, for Datatables to work</p>
  
  <pre><code>protected void GridView1_PreRender(object sender, EventArgs e)
          {
              if (GridView1.Rows.Count &gt; 0)
              {
                  //Replace the &lt;td&gt; with &lt;th&gt; and adds the scope attribute
                  GridView1.UseAccessibleHeader = true;
  
                  //Adds the &lt;thead&gt; and &lt;tbody&gt; elements required for DataTables to work
                  GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
  
                  //Adds the &lt;tfoot&gt; element required for DataTables to work
                  //GridView1.FooterRow.TableSection = TableRowSection.TableFooter;
              }
  </code></pre>