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>
share|improve this question

feedback

1 Answer

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.

share|improve this answer
Thanks for the quick reply. Can you show sample coding since I don't know how to do that coding? Thanks. – Emad-ud-deen 13 hours ago
Sure, one moment – thantos 13 hours ago
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. :-) – Emad-ud-deen 13 hours ago
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. – Emad-ud-deen 13 hours ago
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 – Emad-ud-deen 12 hours ago
show 7 more comments
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

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