0

使用这个 ASP.Net,当用户单击 DetailsView 的“新建”按钮时,我们希望看到今天的日期出现在日期文本框中,这样他们就可以在实际插入数据之前在文本框中看到日期。

<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>

我已经尝试使用 DefaultValue = "<%=DateTime.Now.ToString() %>" 但文本框中没有出现日期。

这是 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>

* 更新 *

我发现要让它工作,需要添加 OnDataBinding,如下所示,并确保代码隐藏文件中有一个处理程序,如下所示。

InsertItemTemplate 标记:

<InsertItemTemplate>
    <asp:TextBox 
        ID="TextBoxDateAttendanceTakenInsert" 
        runat="server" 
        Text='<%# Bind("DateAttendanceTaken", "{0:MM/dd/yyyy}") %>'
        OnDataBinding="TextBoxDateAttendanceTakenInsert_DataBinding">
    </asp:TextBox>

</InsertItemTemplate>

代码隐藏文件中的处理程序:

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
4

1 回答 1

1

您可以使用新按钮的 post back 通过抓取 ListView 的插入项并找到TextBoxDateAttendanceTakenInsert文本框来设置文本框。

将文本框的值设置为DateTime.Now.

插入项

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

应该这样做,它只是在每次单击 new 时将插入项中的文本框设置为当前日期时间。

于 2013-01-08T16:03:56.473 回答