【问题标题】:Add the date from FormView to Gridview将 FormView 中的日期添加到 Gridview
【发布时间】:2015-01-23 04:06:17
【问题描述】:

我有一个 FormView,其中一行是“UserDate”。我的 Formview 可以插入、编辑和删除记录,但 UserDate 字段应该被检索为当前日期和时间。
default.aspx(HTML 标记)

<asp:TextBox ID="UserDateTextBox" runat="server" Text='<%# DateTime.Now %>' />    

当我运行它并在表单视图上选择“插入”时,日期会自动填充。但是,当我添加记录时,它不会显示在网格视图中,而其他所有列值都会显示。
所有其他列都是这样的用户输入:

<asp:TextBox ID="PO_IDTextBox" runat="server" Text='<%# Bind("PO_ID") %>' />    

gridview 类似于我的 sql 数据库表 po_table。

如何在gridview / 数据库表中实现日期?

编辑:
我假设我在我的cs文件中这样做?

protected void FormView2_PageIndexChanging(object sender, FormViewPageEventArgs e)
{

}

到目前为止我尝试过:

<asp:TextBox ID="UserDateTextBox" runat="server" Text='<%# DateTime.Now %>' />

它在表单视图中给出日期,但在添加时不会转移到网格视图。 我试过了:

 <asp:TextBox ID="UserDateTextBox" runat="server" Text='<%# Eval(DateTime.Now) %>' />

给出一个错误。

我的文件:(类文件)

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

namespace PO_1_5_15
{
public partial class _Webform : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {



    }

    protected void FormView2_PageIndexChanging(object sender, FormViewPageEventArgs e)
    {

    }

HTML 标记

 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Test_DatabaseConnectionString %>" SelectCommand="SELECT [PO_ID], [PO_Title], [Date_Received], [Date_Completed], [Username], [PO_Note], [UserDate] FROM [PO_Table]"></asp:SqlDataSource>
    <asp:FormView ID="FormView1" runat="server" DataKeyNames="PO_AutoID" DataSourceID="SqlDataSource2" EnableModelValidation="True">
        <EditItemTemplate>
            PO_AutoID:
            <asp:Label ID="PO_AutoIDLabel1" runat="server" Text='<%# Eval("PO_AutoID") %>' />
            <br />
            PO_ID:
            <asp:TextBox ID="PO_IDTextBox" runat="server" Text='<%# Bind("PO_ID") %>' />
            <br />
            PO_Title:
            <asp:TextBox ID="PO_TitleTextBox" runat="server" Text='<%# Bind("PO_Title") %>' />
            <br />
            Date_Received:
            <asp:TextBox ID="Date_ReceivedTextBox" runat="server" Text='<%# Bind("Date_Received") %>' />
            <br />
            Date_Completed:
            <asp:TextBox ID="Date_CompletedTextBox" runat="server" Text='<%# Bind("Date_Completed") %>' />
            <br />
            Username:
            <asp:TextBox ID="UsernameTextBox" runat="server" Text='<%# Bind("Username") %>' />
            <br />
            UserDate:
            <asp:TextBox ID="UserDateTextBox" runat="server" Text='<%# Eval(DateTime.Now) %>' />
            <br />
            PO_Note:
            <asp:TextBox ID="PO_NoteTextBox" runat="server" Text='<%# Bind("PO_Note") %>' />
            <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" />

【问题讨论】:

  • 你的绑定代码是什么样的......在后面的代码中......?听起来像是绑定/您没有将数据重新绑定到数据网格视图;插入/添加后出现问题..请显示.cs相关代码。如果您正在查看 Late Bound,请将 &lt;%# Bind("PO_ID") %&gt;' 更改为使用 EVAL
  • 您希望FormView 中的same DateTime 显示在GridView 上吗?还是您总是希望显示当前的 DateTime?
  • 是的,我希望 FormView 中的相同 DateTime 显示在 Gridview 上。
  • 我很抱歉忘记了这个问题。作为提示:您可以使用“@”符号将 cmets 指向其他用户。提问者会收到每条评论的通知,而其他用户则不会。使用“@”确保他们收到通知。只需输入“@”,然后输入他们的用户名。

标签: c# sql asp.net


【解决方案1】:

如果您想让 FormView 中的 same DateTime 显示在 GridView 上,则需要将该 DateTime 保存在某处。如果你只是简单地在 GridView 中使用DateTime.Now,它总是只显示当前的 DateTime,而不是从 FormView 输入的时间。

我的建议是在您的数据库表中添加/添加一列,例如创建或更新日期。然后,当您从 FormView 插入/更新数据库时,在这些 SQL 调用中包含新的 DateTime。然后像绑定 GridView 中的其他列一样绑定它。

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 2010-11-28
    • 2011-10-15
    • 1970-01-01
    • 2020-11-27
    • 2013-04-29
    • 1970-01-01
    • 2010-10-08
    相关资源
    最近更新 更多