سؤال

I'm using a DetailView to insert data into a GridView via an sqlDataSource. I'm attempting to set one of the fields of the DetailView to the current date/time on Page Load so the user does not have to enter the date/time. I get no errors - however, the "Update Date" field of the DetailView fails to display the current date/time.

This is my hypertext:

<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> 

This is the code behind:

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

Could I get some help please as to what I'm doing wrong?

هل كانت مفيدة؟

المحلول

To expand a little on Mych's answer, you would want to handle the ItemCreated event of the DetailsView:

<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">

And then write the event handler like this:

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

Notice that I changed your implementation a little, to use a reference to the TextBox, rather than the string.

This is all necessary because the DetailsView is not loaded with data yet in the Page's Load event.

نصائح أخرى

You need to set your date when the Items in the DataView are being bound to the datasource. You need to use the ItemCreated event....

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top