首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将当前日期设置为TemplateField of DetailView

问将当前日期设置为TemplateField of DetailView
EN

Stack Overflow用户
提问于 2014-04-10 16:37:40
回答 2查看 1.2K关注 0票数 1

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

这是我的超文本:

代码语言:javascript
复制
<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> 

这就是背后的代码:

代码语言:javascript
复制
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

关于我做错了什么,我能得到一些帮助吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-10 18:23:05

要在Mych's answer上稍微展开一些,您需要处理DetailsView的ItemCreated事件:

代码语言:javascript
复制
<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">

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

代码语言:javascript
复制
protected void dtlShipModes_ItemCreated(Object sender, EventArgs e)
{
    Dim txtupdatedby As TextBox = DirectCast(dtlShipModes.FindControl("txtUpdateDate"), TextBox)
    txtupdatedby.Text = DateTime.Now.ToString
}

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

这一切都是必要的,因为DetailsView还没有在页面的Load事件中装载数据。

票数 0
EN

Stack Overflow用户

发布于 2014-04-10 18:06:53

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22993934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档