using an edit form, how do I bind a date field on edit, but not on insert (or use a default value)

2 posts, 0 answers
  1. John Crumpton
    John Crumpton avatar
    12 posts
    Member since:
    Jul 2009

    Posted 27 Oct 2010 Link to this post

    code is below. it works fine for edit, but errors on insert when it tries to bind the date control. text control work fine.
      

    <EditFormSettings InsertCaption="Add new Comment" CaptionFormatString="Edit Comment: {0}"
                                        CaptionDataField="CultivarCommentsID" EditFormType="Template" ColumnNumber="2">
                                        <FormTemplate>
                                            <table id="Table1" cellspacing="1" cellpadding="1" border="0">
                                                <tr>
                                                    <td>
                                                        Date:
                                                    </td>
                                                    <td>
                                                        <telerik:RadDatePicker Skin="Forest" ID="dtpCommentDate" runat="server" SelectedDate='<%# Bind( "Date" ) %>'></telerik:RadDatePicker>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        User:
                                                    </td>
                                                    <td>
                                                        <telerik:RadTextBox Skin="Forest" ID="txtCommentUser" runat="server" Text='<%# Bind( "UserName") %>' Width="325" TabIndex="1">
                                                        </telerik:RadTextBox>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        Comment:
                                                    </td>
                                                    <td>
                                                        <telerik:RadTextBox Skin="Forest" ID="txtComment" runat="server" Text='<%# Bind( "Comments") %>' Width="325" TabIndex="2" Rows="6" TextMode="MultiLine">
                                                        </telerik:RadTextBox>
                                                    </td>
                                                </tr>
                                            </table>
                                            <table style="width100%">
                                                <tr>
                                                    <td align="right" colspan="2">
                                                        <asp:Button ID="Button1" Text='<%# Iif (TypeOf Container is GridEditFormInsertItem, "Insert", "Update") %>'
                                                            runat="server" CommandName='<%# Iif (TypeOf Container is GridEditFormInsertItem, "PerformInsert", "Update") %>'>
                                                        </asp:Button>&nbsp;
                                                        <asp:Button ID="Button2" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel">
                                                        </asp:Button>
                                                    </td>
                                                </tr>
                                            </table>
                                        </FormTemplate>
                                    </EditFormSettings>
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 28 Oct 2010 Link to this post

    Hello John,

    One suggestion is setting the SelectedDate property of RadDatePicker from code behind when the grid is in edit mode.

    VB.Net:
    Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
        If TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode AndAlso Not e.Item.OwnerTableView.IsItemInserted Then
            ' if item is about to edit
            Dim editItem As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
            Dim item As GridDataItem = DirectCast(editItem.ParentItem, GridDataItem)
            Dim datepick As RadDatePicker = DirectCast(editItem.FindControl("dtpCommentDate"), RadDatePicker)
            datepick.SelectedDate = Convert.ToDateTime(item("Date").Text)
        End If
    End Sub

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top