0

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

1 Answer 1

1

You can use the post back of the new button to set the textbox by grabbing the ListView's insert item and finding the TextBoxDateAttendanceTakenInsert textbox.

Set the value of the textbox to DateTime.Now.

The Insert Item

protected void NewButtonClick(object sender, EventArgs e)
{
    //Code to change to set Insert
    //get insert item
    object control = MyDetailsView.FindControl("TextBoxDateAttendanceTaken")
    //check for null
    if(control != null){
        TextBox tBox = (TextBox)control; //cast to TextBox
        tBox.Text = DateTime.Now.ToString(); //set text
    }
}

VB:

Dim tBox As TextBox =  CType(MyDetailsView.FindControl("TextBoxDateAttendanceTaken"), TextBox)
tBox.Text = DateTime.Now.ToString

That should do it, it just sets the textbox in the insert item to the current datetime each time new is clicked.

11
  • Thanks for the quick reply. Can you show sample coding since I don't know how to do that coding? Thanks. Jan 8, 2013 at 16:08
  • Thanks. To make things easier for you this is in a web page based on a MasterPage with the content id called "ContentBody" if that helps. :-) Jan 8, 2013 at 16:12
  • Thanks for the coding. I tried to convert it to VB.Net with a converter but the converter says there's an error on the object control = line. Jan 8, 2013 at 16:43
  • Thanks and almost there. I get this error maybe because it's a DetailsView and not a ListView: Error 1 Argument not specified for parameter 'causesValidation' of 'Public Overridable Sub InsertItem(causesValidation As Boolean)'. C:\Development\Knowledge Academy Internal\Knowledge Academy\Knowledge Academy\Attendance.aspx.vb 148 51 Knowledge Academy Jan 8, 2013 at 16:56
  • Thanks for the correction. The code compiles and when I click the new button there is this error: Object reference not set to an instance of an object. Looks like FindControl is not finding it. I made sure the spelling is correct by copying the name directly from the markup id of the textbox. Jan 8, 2013 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.