title: Setting today as an ASP.Net SqlDataSource insert parameter default value
body: <p>Using this ASP.Net we would like to see today's date appear in a date Textbox when the user clicks the "New" button of a DetailsView so they can see the date in the Textbox prior to actually inserting the data.</p>
  
  <pre><code>&lt;InsertParameters&gt;
      &lt;asp:Parameter Name="ClassID" Type="Int32" /&gt;
      &lt;asp:Parameter Name="StudentID" Type="Int32" /&gt;
      &lt;asp:Parameter Name="Absent" Type="String" /&gt;
      &lt;asp:Parameter Name="Late" Type="String" /&gt;
      &lt;asp:Parameter Name="LateTimeArrivedAtSchool" Type="DateTime" /&gt;
      &lt;asp:Parameter DbType="Date" Name="DateAttendanceTaken"  DefaultValue= "&lt;%=DateTime.Now.ToString() %&gt;"/&gt;
  &lt;/InsertParameters&gt;
  </code></pre>
  
  <p>I already tried to use DefaultValue = "&lt;%=DateTime.Now.ToString() %>" but no date appears in the Textbox.</p>
  
  <p>This is the markup for the textbox which is in the DetailsView:</p>
  
  <pre><code>&lt;asp:TemplateField 
      HeaderText="Attendance Date:" SortExpression="DateAttendanceTaken"&gt;
  
      &lt;EditItemTemplate&gt;
          &lt;asp:TextBox 
              ID="TextBoxDateAttendanceTaken" 
              runat="server" 
              Text='&lt;%# Bind("DateAttendanceTaken", "{0:MM/dd/yyyy}") %&gt;'&gt;
          &lt;/asp:TextBox&gt;
  
          &lt;asp:RequiredFieldValidator ID="RequiredFieldValidatorEditDate" runat="server" ControlToValidate="TextBoxDateAttendanceTaken" 
              ErrorMessage="Please enter the Attendance Date." Font-Bold="True" Font-Italic="True" ForeColor="Red" 
              SetFocusOnError="True" Display="Dynamic"&gt;&lt;/asp:RequiredFieldValidator&gt;
      &lt;/EditItemTemplate&gt;
  
      &lt;InsertItemTemplate&gt;
          &lt;asp:TextBox 
              ID="TextBoxDateAttendanceTakenInsert" 
              runat="server" 
              Text='&lt;%# Bind("DateAttendanceTaken", "{0:MM/dd/yyyy}") %&gt;'&gt;
          &lt;/asp:TextBox&gt;
  
          &lt;asp:RequiredFieldValidator ID="RequiredFieldValidatorInsertDate" runat="server" ControlToValidate="TextBoxDateAttendanceTakenInsert" 
              ErrorMessage="Please enter the Attendance Date." Font-Bold="True" Font-Italic="True" ForeColor="Red" 
              SetFocusOnError="True" Display="Dynamic"&gt;&lt;/asp:RequiredFieldValidator&gt;
      &lt;/InsertItemTemplate&gt;
  
      &lt;ItemTemplate&gt;
          &lt;asp:Label 
              ID="LabelDateAttendanceTaken" 
              runat="server" 
              Text='&lt;%# Bind("DateAttendanceTaken", "{0:MM/dd/yyyy}") %&gt;'&gt;
          &lt;/asp:Label&gt;
      &lt;/ItemTemplate&gt;
  
      &lt;ItemStyle ForeColor="Blue" /&gt;
  &lt;/asp:TemplateField&gt;
  </code></pre>
  
  <p><strong>* UPDATE *</strong></p>
  
  <p>I found out that to get this to work, OnDataBinding needs to be added as shown here plus making sure there is a handler in the code-behind file as shown below.</p>
  
  <p>InsertItemTemplate markup:</p>
  
  <pre><code>&lt;InsertItemTemplate&gt;
      &lt;asp:TextBox 
          ID="TextBoxDateAttendanceTakenInsert" 
          runat="server" 
          Text='&lt;%# Bind("DateAttendanceTaken", "{0:MM/dd/yyyy}") %&gt;'
          OnDataBinding="TextBoxDateAttendanceTakenInsert_DataBinding"&gt;
      &lt;/asp:TextBox&gt;
  
  &lt;/InsertItemTemplate&gt;
  </code></pre>
  
  <p>Handler in the code-behind file:</p>
  
  <pre><code>Protected Sub TextBoxDateAttendanceTakenInsert_DataBinding(sender As Object, e As EventArgs)
  
      Dim txtBox As New TextBox
      txtBox = DetailsView.FindControl("TextBoxDateAttendanceTakenInsert")
      txtBox.Text = DateTime.Now.ToString("d")
  End Sub
  </code></pre>