Emad-ud-deen
Emad-ud-deen

Reputation: 4864

Setting today as an ASP.Net SqlDataSource insert parameter default value

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

Upvotes: 0

Views: 6513

Answers (1)

Sam Sussman
Sam Sussman

Reputation: 1045

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.

Upvotes: 1

Related Questions