将当前日期设置为DetailView的TemplateField

时间:2014-04-10 16:37:41

标签: asp.net vb.net detailview

我使用DetailView通过sqlDataSource将数据插入到GridView中。我试图将DetailView的一个字段设置为Page Load上的当前日期/时间,这样用户就不必输入日期/时间。我没有错误 - 但是,"更新日期" 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

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

要在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事件中加载数据。

相关问题