Asp.net 将当前日期设置为DetailView的TemplateField
我使用DetailView通过sqlDataSource将数据插入GridView。我正在尝试将DetailView的一个字段设置为页面加载时的当前日期/时间,以便用户不必输入日期/时间。我没有收到任何错误-但是,DetailView的Update Date字段无法显示当前日期/时间 这是我的超文本:Asp.net 将当前日期设置为DetailView的TemplateField,asp.net,vb.net,detailview,Asp.net,Vb.net,Detailview,我使用DetailView通过sqlDataSource将数据插入GridView。我正在尝试将DetailView的一个字段设置为页面加载时的当前日期/时间,以便用户不必输入日期/时间。我没有收到任何错误-但是,DetailView的Update Date字段无法显示当前日期/时间 这是我的超文本: <asp:DetailsView id="dtlShipModes" DataSourceID="SqlDataSource1" AutoGe
<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>
我做错了什么,能不能请你帮个忙 您需要设置数据视图中的项目绑定到数据源的日期。您需要使用ItemCreated事件 看 要稍微展开,您需要处理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">
请注意,我稍微更改了您的实现,使用了对文本框的引用,而不是字符串
这是必需的,因为DetailsView在页面的加载事件中还没有加载数据。感谢您的解释和示例,一切正常well@user1724708不客气!我很高兴能帮上忙=
<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
}