【问题标题】:Set Current Date to TemplateField of a DetailView将当前日期设置为 DetailView 的 TemplateField
【发布时间】:2014-05-24 12:11:11
【问题描述】:

我正在使用 DetailView 通过 sqlDataSource 将数据插入到 GridView。我正在尝试将 DetailView 的字段之一设置为页面加载上的当前日期/时间,这样用户就不必输入日期/时间。我没有收到任何错误 - 但是,DetailView 的“更新日期”字段无法显示当前日期/时间。

这是我的超文本:

<asp:DetailsView
        id="dtlShipModes"
        DataSourceID="SqlDataSource1"
        AutoGenerateRows="False"
        DefaultMode="Insert"
        Runat="server" BackColor="White" BorderColor="White" BorderStyle="Ridge" 
        BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None">
        <EditRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
        <Fields>
        <asp:BoundField
            DataField="ShipMode"
            HeaderText="Ship Mode:" />
              <asp:CheckBoxField
            DataField="Active"
            HeaderText="Active:" />
            <asp:TemplateField HeaderText="Update Date:">
                <EditItemTemplate>
                    <asp:TextBox ID="txtUpdateDate" runat="server" Text='<%# Bind("UpdateDate") %>'></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="txtUpdateDate" runat="server" Text='<%# Bind("UpdateDate") %>'></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("UpdateDate") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
             <asp:BoundField
            DataField="UpdateBY"
            HeaderText="Update BY:" />

            <asp:CommandField ShowInsertButton="true" InsertText="Add" />

        </Fields>
        <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
        <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
        <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
    </asp:DetailsView> 

这是后面的代码:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim txtupdatedby As String = DirectCast(dtlShipModes.FindControl("txtUpdateDate"), TextBox).Text
        txtupdatedby = DateTime.Now.ToString
    End Sub

我可以就我做错了什么寻求帮助吗?

【问题讨论】:

    标签: asp.net vb.net detailview


    【解决方案1】:

    要稍微扩展Mych's answer,您需要处理DetailsView 的ItemCreated 事件:

    <asp:DetailsView
        id="dtlShipModes"
        DataSourceID="SqlDataSource1"
        AutoGenerateRows="False"
        DefaultMode="Insert"
        ItemCreated="dtlShipModes_ItemCreated"
        Runat="server" BackColor="White" BorderColor="White" BorderStyle="Ridge" 
        BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None">
    

    然后像这样编写事件处理程序:

    protected void dtlShipModes_ItemCreated(Object sender, EventArgs e)
    {
        Dim txtupdatedby As TextBox = DirectCast(dtlShipModes.FindControl("txtUpdateDate"), TextBox)
        txtupdatedby.Text = DateTime.Now.ToString
    }
    

    请注意,我稍微更改了您的实现,使用对 TextBox 的引用,而不是字符串。

    这都是必要的,因为 DetailsView 尚未在 Page 的 Load 事件中加载数据。

    【讨论】:

    • 谢谢你的解释和例子,一切都很好
    • @user1724708 不客气!我很高兴能帮上忙 =)
    【解决方案2】:

    您需要设置 DataView 中的项目绑定到数据源的日期。您需要使用 ItemCreated 事件....

    见...http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.itemcreated(v=vs.110).aspx

    【讨论】:

      猜你喜欢
      • 2019-01-07
      • 1970-01-01
      • 1970-01-01
      • 2014-05-06
      • 1970-01-01
      • 2014-12-07
      • 2012-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多