在表单视图中设置当前日期

2024-04-13

我想将当前日期放入表单视图“dateadded”中,但当我将其加载到浏览器中时它没有显示。我正在使用后台代码,但如何显示日期和时间?

这是我后面的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace LibrarySystem.AdminPage
{
    public partial class ManageBooks : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(FormView1.CurrentMode == FormViewMode.Insert)
            {
                TextBox dateadded = FormView1.FindControl("dateaddedTextBox") as TextBox;
                    dateadded.Text = DateTime.Now.ToString("d");
            }
        }
    }
}

这是我的完整设计

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="Lending.aspx.cs" Inherits="LibrarySystem.Test" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <h3>
    Lending of Books</h3>
<p>
    <asp:FormView ID="FormView1" runat="server" DataSourceID="lendDataSource" DefaultMode="Insert" OnDataBound="FormView1_DataBound">
        <EditItemTemplate>

            Book ID/ISBN:
            <asp:TextBox ID="bookidTextBox" runat="server" Text='<%# Bind("bookid") %>' />
            <br />

            Book Title:
            <asp:TextBox ID="booktitleTextBox" runat="server" 
                Text='<%# Bind("booktitle") %>' />
            <br />

            Employee ID:
            <asp:TextBox ID="employeeidTextBox" runat="server" 
                Text='<%# Bind("employeeid") %>' />
            <br />

            Department:
            <asp:TextBox ID="departmentTextBox" runat="server" 
                Text='<%# Bind("department") %>' />
            <br />

            Date borrowed:
            <asp:TextBox ID="dateborrowedTextBox" runat="server" 
                Text='<%# Bind("dateborrowed") %>' />
            <br />

            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
                CommandName="Update" Text="Update" />
                &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" 
                CausesValidation="False" CommandName="Cancel" Text="Cancel" />
        </EditItemTemplate>

        <InsertItemTemplate>

            Book:
            <asp:DropDownList ID="DropDownList1" runat="server" 
                DataSourceID="booktitleDataSource" DataTextField="booktitle" 
                DataValueField="bookid" SelectedValue='<%# Bind("bookid", "{0}") %>'>
            </asp:DropDownList>

            <asp:SqlDataSource ID="booktitleDataSource" runat="server" 
                ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>" 
                SelectCommand="SELECT [bookid], [booktitle] FROM [TblBooks]">
            </asp:SqlDataSource>
            <br />

            Employee ID:
            <asp:TextBox ID="employeeidTextBox" runat="server" 
                Text='<%# Bind("employeeid") %>' />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="* Required" ControlToValidate="employeeidTextBox" ValidationGroup="lendbook">
            </asp:RequiredFieldValidator>
            <br />

            Department:
            <asp:TextBox ID="departmentTextBox" runat="server" 
                Text='<%# Bind("department") %>' />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="* Required" ControlToValidate="departmentTextBox" ValidationGroup="lendbook">
            </asp:RequiredFieldValidator>
            <br />

            Date borrowed:
            <asp:TextBox ID="dateborrowedTextBox" runat="server" 
                Text='<%# Bind("dateborrowed") %>' />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="* Required" ControlToValidate="dateborrowedTextBox" ValidationGroup="lendbook">
            </asp:RequiredFieldValidator>
            <br />

            <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"  ValidationGroup="lendbook" 
                CommandName="Insert" Text="Insert" />
                &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" 
                CommandName="Cancel" Text="Cancel" />
        </InsertItemTemplate>

        <ItemTemplate>
            <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" 
                CommandName="New" Text="New" />
        </ItemTemplate>

        <EmptyDataTemplate>
            <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" 
                CommandName="New" Text="New" />
        </EmptyDataTemplate>
    </asp:FormView>
</p>
    <p>
    <asp:SqlDataSource ID="lendDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>" 
        DeleteCommand="DELETE FROM [LendTable] WHERE [lenid] = @lenid" 
        InsertCommand="INSERT INTO [LendTable] ([bookid], [employeeid], [department], [dateborrowed], [datereturned]) VALUES (@bookid, @employeeid, @department, @dateborrowed, @datereturned)" 
        SelectCommand="SELECT dbo.LendTable.bookid, dbo.TblBooks.booktitle, dbo.LendTable.employeeid, dbo.LendTable.department, dbo.LendTable.dateborrowed FROM dbo.LendTable INNER JOIN dbo.TblBooks ON dbo.LendTable.bookid = dbo.TblBooks.bookid" 

        UpdateCommand="UPDATE [LendTable] SET [bookid] = @bookid, [employeeid] = @employeeid, [department] = @department, [dateborrowed] = @dateborrowed, [datereturned] = @datereturned WHERE [lenid] = @lenid">
        <DeleteParameters>
            <asp:Parameter Name="lenid" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="bookid" Type="Int64" />
            <asp:Parameter Name="employeeid" Type="string" />
            <asp:Parameter Name="department" Type="String" />
            <asp:Parameter Name="dateborrowed" Type="DateTime" />
            <asp:Parameter Name="datereturned" Type="DateTime" />
            <asp:Parameter Name="lenid" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="bookid" Type="Int64" />
            <asp:Parameter Name="employeeid" Type="string" />
            <asp:Parameter Name="department" Type="String" />
            <asp:Parameter Name="dateborrowed" Type="DateTime" />
            <asp:Parameter Name="datereturned" Type="DateTime" />
        </InsertParameters>
    </asp:SqlDataSource>
</p>
<p>
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
        DataKeyNames="lenid" DataSourceID="lendgridviewDataSource" ForeColor="#333333" 
        GridLines="None">
        <RowStyle BackColor="#EFF3FB" />
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            <asp:BoundField DataField="bookid" HeaderText="Book ID/ISBN" 
                SortExpression="bookid" />
            <asp:BoundField DataField="booktitle" HeaderText="Book Title" 
                SortExpression="booktitle" />
            <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" 
                SortExpression="EmployeeID" />
            <asp:BoundField DataField="department" HeaderText="Department" 
                SortExpression="department" />
            <asp:BoundField DataField="dateborrowed" HeaderText="Date Borrowed" 
                SortExpression="dateborrowed" />
            <asp:BoundField DataField="datereturned" HeaderText="Date Returned" 
                NullDisplayText="-- not yet returned --" SortExpression="datereturned" />
        </Columns>
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#2461BF" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>
    <asp:SqlDataSource ID="lendgridviewDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>" 
        DeleteCommand="DELETE FROM [LendTable] WHERE [lenid] = @lenid" 
        InsertCommand="INSERT INTO [LendTable] ([bookid], [department], [EmployeeID], [dateborrowed], [datereturned]) VALUES (@bookid, @department, @EmployeeID, @dateborrowed, @datereturned)" 
        SelectCommand="SELECT dbo.LendTable.lenid, dbo.LendTable.bookid, dbo.LendTable.department, dbo.LendTable.EmployeeID, dbo.LendTable.dateborrowed, dbo.LendTable.datereturned, dbo.TblBooks.booktitle FROM dbo.LendTable INNER JOIN dbo.TblBooks ON dbo.LendTable.bookid = dbo.TblBooks.bookid" 
        UpdateCommand="UPDATE [LendTable] SET [bookid] = @bookid, [department] = @department, [EmployeeID] = @EmployeeID, [dateborrowed] = @dateborrowed, [datereturned] = @datereturned WHERE [lenid] = @lenid">
        <DeleteParameters>
            <asp:Parameter Name="lenid" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="bookid" Type="Int64" />
            <asp:Parameter Name="department" Type="String" />
            <asp:Parameter Name="EmployeeID" Type="String" />
            <asp:Parameter Name="dateborrowed" Type="DateTime" />
            <asp:Parameter Name="datereturned" Type="DateTime" />
            <asp:Parameter Name="lenid" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="bookid" Type="Int64" />
            <asp:Parameter Name="department" Type="String" />
            <asp:Parameter Name="EmployeeID" Type="String" />
            <asp:Parameter Name="dateborrowed" Type="DateTime" />
            <asp:Parameter Name="datereturned" Type="DateTime" />
        </InsertParameters>
    </asp:SqlDataSource>
</p>
<p>
    <asp:HyperLink ID="HyperLink4" runat="server" 
        NavigateUrl="~/Admin/Returning.aspx">Returning</asp:HyperLink>
</p>
        <asp:HyperLink ID="HyperLink5" runat="server" 
            NavigateUrl="~/Admin/AdminPage.aspx">Back to Admin Page</asp:HyperLink>
    <p>
        &nbsp;</p>

    </asp:Content>

你必须使用Databound event为了那个原因...

 protected void FormView1_DataBound(object sender, EventArgs e)
{
    if (FormView1.CurrentMode == FormViewMode.Insert)
    {
        TextBox dateadded = FormView1.FindControl("dateaddedTextBox") as TextBox;
        dateadded.Text = DateTime.Now.ToString("d");
    }
}

Edit:您需要在 DetailsView 的插入方法中传递日期列

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
    e.Values["DateColumnName"] = ((TextBox)DetailsView1.FindControl("dateaddedTextBox")).Text;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在表单视图中设置当前日期 的相关文章

随机推荐

  • 有没有办法在转码之前使用 ffmpeg 确定文件的编码?

    我计划使用 ffmpeg 确保上传到我网站的所有视频文件都编码为 mp4 h264 我不想自动处理每个文件 而是希望通过仅处理那些还不是 mp4 h264 的文件来最小化处理开销 有没有一种简单的方法可以使用 ffmpeg 或其他命令行实用
  • 如何判断一个文件是否可以删除?

    我怎么能够check我可以用Java删除文件吗 例如 如果一个文件test txt在另一个程序中打开我无法删除它 我必须在实际删除之前知道它 所以我不能这样做 if file delete And srcFile canWrite 也不工作
  • 在 ES6 生成器函数中使用 return

    我无法弄清楚如果您使用 return 语句而不是 Yield 会发生什么 function gen const val yield someAsyncFn assert equal val 4 return val 回报率与收益率有何不同
  • Visual Studio代码侧边栏垂直引导线(自定义侧边栏)

    有人知道 Visual Studio 代码的扩展可以像 netbeans 一样在侧边栏 用于文件和文件夹 上显示垂直指南吗 或者vscode中有一些设置吗 Netbeans 快照 https i stack imgur com CFJsw
  • .Net 哪里可以找到 BinaryFormatter 序列化格式的官方规范?

    我想知道BinaryFormatter的序列化格式是什么 我发现这个网站 http primates ximian com lluis dist binary serialization format htm其中提供了一些很好的信息 但它是
  • Applescript 获取正在运行的应用程序列表?

    Applescript 新手问题又来了 我正在尝试创建一个小 applescript 它允许我从当前运行的应用程序列表中选择多个项目 然后退出这些选定的应用程序 像这样的东西是可行的 但不必单击每个对话框 从列表中进行选择会更容易 tell
  • 将 jQuery 脚本与 Angular 6 CLI 项目结合使用

    我正在使用 Angular 6 开发一个应用程序 我的应用程序使用明顿主题 http coderthemes com minton dark index html 我将所有主题脚本包含在index html我的角度项目的文件 但是当我登录或
  • 我们可以使用 python 生成卡方检验的列联表吗?

    我正在使用 scipy stats chi2 contingency 方法来获取卡方统计数据 我们需要传递频率表 即列联表作为参数 但我有一个特征向量 想要自动生成频率表 我们有这样的功能吗 我目前正在这样做 def contigency
  • PHP OOP MySQL 编程

    我是 PHP 编程的初学者 需要帮助解决一个小问题 请看下面的代码 PHP Code
  • Android 8.0 Oreo - 帐户

    在我的应用程序中 我需要知道是否有任何 Google 帐户或任何三星帐户 在 Android 7 之前 可以通过以下方式轻松获取此信息 Account accounts AccountManager get getContext getAc
  • 如果没有结果,如何停止 grep 创建空文件

    我正在比较两个文件的结果 其中一个文件中的行不在另一个文件中 使用grep v f file1 txt file2 txt gt result txt 假设我的文件看起来像 文件1 txt alex peter zoey 文件2 txt a
  • 在 proguard 之后运行 espresso 测试

    我正在寻找一种在 proguard 运行后运行我的 espresso 测试的方法 这应该是一个针对配置错误的 proguard 的安全网 只是在将 butterknife 更新到 7 0 后忘记更新 proguard 配置 假设您使用 An
  • 垂直对齐:基线在 Chrome 中不起作用

    老实说 我有一个非常愚蠢的问题 不管怎样 就到这里吧 我一直在尝试使用垂直对齐 基线 这本身就起作用 问题是它可以在 IE 和 Firefox 中运行 我的问题是 由于某些奇怪的原因 它在 Chrome 中不起作用 chrome 中问题的屏
  • 正则表达式用标签包装文本[重复]

    这个问题在这里已经有答案了 我需要一个 JavaScript 正则表达式来包装 hashtags span tags Example 之前 我需要 help 请 Thanks 之后 我需要 span help span 请 span Tha
  • Spring Boot 注解 @GetMapping 无法解析为类型

    我第一次尝试在本地计算机上从 Spring Initializr 创建 Spring 项目 但我收到这些错误 GetMapping 无法解析为类型 我的 pom xml 文件
  • C++ 可变参数模板 匹配任何类型参数的模板实参

    我想知道是否可以编写一个模板函数 该函数可以将任何其他任意模板作为参数并正确匹配模板名称 即不仅仅是结果类 我所知道的工作是这样的 template
  • 如何围绕 WPF 中的特定点旋转、平移和缩放控制对象

    我有一个自定义的控件 它是一个矩形 里面有一些细节 但它是一个矩形 我有一个中心点 X Y 我称之为 重心 它 代表 该点 这意味着当我为对象设置新位置时 我希望该点位于设置的位置 当我旋转对象时 我需要它围绕该点旋转 当我缩放对象时 该点
  • 我应该在 MVP(或 VM)中对我的视图进行单元测试,或者如何将视图中的代码保持在最低限度?

    我在应用程序中使用模型 视图 表示模型 我想知道是否需要为视图创建单元测试 我使用的是 net 2 0 WinForms 现在 通常视图应该非常简单 以至于没有必要为其创建单元测试 至少这是我从视图与表示模型 PM 分离的目标中得到的想法
  • Ruby 中的大乘法输出结果为负

    我写了一些代码 应该对 1 sum 0 1 1000 each do n sum n n puts n n sum sum end 由于某种原因 在数字 28 之后输出为负值 n 29 sum 20154009777005735238923
  • 在表单视图中设置当前日期

    我想将当前日期放入表单视图 dateadded 中 但当我将其加载到浏览器中时它没有显示 我正在使用后台代码 但如何显示日期和时间 这是我后面的代码 using System using System Collections Generic