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.
<InsertParameters>
<asp:Parameter Name="ClassID" Type="Int32" />
<asp:Parameter Name="StudentID" Type="Int32" />
<asp:Parameter Name="Absent" Type="String" />
<asp:Parameter Name="Late" Type="String" />
<asp:Parameter Name="LateTimeArrivedAtSchool" Type="DateTime" />
<asp:Parameter DbType="Date" Name="DateAttendanceTaken" DefaultValue= "<%=DateTime.Now.ToString() %>"/>
</InsertParameters>
I already tried to use DefaultValue = "<%=DateTime.Now.ToString() %>" but no date appears in the Textbox.
This is the markup for the textbox which is in the DetailsView:
<asp:TemplateField
HeaderText="Attendance Date:" SortExpression="DateAttendanceTaken">
<EditItemTemplate>
<asp:TextBox
ID="TextBoxDateAttendanceTaken"
runat="server"
Text='<%# Bind("DateAttendanceTaken", "{0:MM/dd/yyyy}") %>'>
</asp:TextBox>
<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"></asp:RequiredFieldValidator>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox
ID="TextBoxDateAttendanceTakenInsert"
runat="server"
Text='<%# Bind("DateAttendanceTaken", "{0:MM/dd/yyyy}") %>'>
</asp:TextBox>
<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"></asp:RequiredFieldValidator>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label
ID="LabelDateAttendanceTaken"
runat="server"
Text='<%# Bind("DateAttendanceTaken", "{0:MM/dd/yyyy}") %>'>
</asp:Label>
</ItemTemplate>
<ItemStyle ForeColor="Blue" />
</asp:TemplateField>
* UPDATE *
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.
InsertItemTemplate markup:
<InsertItemTemplate>
<asp:TextBox
ID="TextBoxDateAttendanceTakenInsert"
runat="server"
Text='<%# Bind("DateAttendanceTaken", "{0:MM/dd/yyyy}") %>'
OnDataBinding="TextBoxDateAttendanceTakenInsert_DataBinding">
</asp:TextBox>
</InsertItemTemplate>
Handler in the code-behind file:
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