Question

This error freaks me out. It turns out that the format of a date in a DetailsView proves wrong. It seems like a pretty obvious error as you just have to apply a DataFormatString but as you can see I have already done that. I have tried the formats {0:d} and {0:dd/MM/yyyy} and I have tried to replace the BoundFields with TemplateField where I use the Bind function with the format string as the second parameter. It shows the correct date but in the format dd/MM/yyyy hh:mm:ss.

<asp:DetailsView DataSourceID="SqlDataSource1" runat="server" DefaultMode="Edit" AutoGenerateRows="false" ID="EditView" DataKeyNames="id" CssClass="editing-padding" AutoGenerateEditButton="true" OnModeChanging="EditView_ModeChanging" OnItemUpdating="EditView_ItemUpdating">
    <Fields>
        <asp:BoundField HeaderText="Startdato" DataField="start_time" DataFormatString="{0:dd/MM/yyyy}"/>
        <asp:BoundField HeaderText="Slutdato" DataField="end_time" DataFormatString="{0:dd/MM/yyyy}"/>
        <asp:TemplateField>
            <HeaderTemplate>
                Kommentar
            </HeaderTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="commentBox" runat="server" Text='<%# Bind("comment") %>' TextMode="MultiLine" Width="300" Height="150"></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
    </Fields>
    <RowStyle BackColor="White" />
</asp:DetailsView>

Thanks for the help beforehand

Was it helpful?

Solution

It seems that a BoundField only applies the formatting specified by the DataFormatString in read-only mode by default. Try applying an ApplyFormatInEditMode="true" attribute to your BoundField, like so:

<asp:BoundField HeaderText="Slutdato"
DataField="end_time" 
DataFormatString="{0:dd/MM/yyyy}" 
ApplyFormatInEditMode="true" />

Hope that helps, worked for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top