This is a migrated thread and some comments may be shown as answers.

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

1 Answer 110 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John Crumpton
Top achievements
Rank 1
John Crumpton asked on 27 Oct 2010, 11:22 PM
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>

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 28 Oct 2010, 06:27 AM
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.
Tags
Grid
Asked by
John Crumpton
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or