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

FormTemplate and objects

3 Answers 83 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eric Downey
Top achievements
Rank 1
Eric Downey asked on 28 Nov 2012, 07:33 PM
I'm using a RadGrid to insert/edit records.  I'm using the FormTemplate.  The problem I'm experiencing is that when you click on the Add A New Record button on the grid it doesn't expand to show the form template.  Instead this error bubbles up:  'Telerik.Web.UI.GridInsertionObject' does not contain a property with the name 'Address'.

I'm using an object datasource to populate the grid and do inserts and updates.  Here's the data object I'm using:

public class Person : BusinessBase<Person>, IPerson, IAggregateRoot
{
    public User.User Owner { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName
    {
        get { return LastName + ", " + FirstName; }
    }
    public DateTime BirthDate { get; set; }
    public Address.Address Address { get; set; }
    public IList<Item.Item> Items { get; set; }
 
    protected override void validate()
    {
         
    }
}

public class Address : BusinessBase<Address>, IAddress, IAggregateRoot
{
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    protected override void validate()
    {
         
    }
}


And here's the form template I created for it.

            <EditFormSettings EditFormType="Template" InsertCaption="New Person">
                <FormTemplate>
                    <div id="PersonDiv" >
                        <table>
                            <tr>
                                <td>
                                    First Name:
                                </td>
                                <td>
                                    <telerik:RadTextBox runat="server" ID="txtFirstName" MaxLength="50" Text='<%# Bind("FirstName") %>' />
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtFirstName" ErrorMessage="A first name is required" ValidationGroup="vgPerson" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Last Name:
                                </td>
                                <td>
                                    <telerik:RadTextBox runat="server" ID="txtLastName" MaxLength="50" Text='<%# Bind("LastName") %>' />
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtLastName" ErrorMessage="A last name is required" ValidationGroup="vgPerson" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Use your address:
                                </td>
                                <td>
                                    <telerik:RadButton ToggleType="CheckBox" runat="server" ID="cbUseYourAddress" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Street:
                                </td>
                                <td>
                                    <telerik:RadTextBox runat="server" ID="txtStreet" MaxLength="50" Text='<%# Bind("Address.Street") %>' />
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtStreet" ErrorMessage="A street is required" ValidationGroup="vgPerson" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    City:
                                </td>
                                <td>
                                    <telerik:RadTextBox runat="server" ID="txtCity" MaxLength="50" Text='<%# Bind("Address.City") %>' />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    State:
                                </td>
                                <td>
                                    <telerik:RadTextBox runat="server" ID="txtState" MaxLength="2" Text='<%# Bind("Address.State") %>' />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    Zip code:
                                </td>
                                <td>
                                    <telerik:RadTextBox runat="server" ID="txtZipCode" MaxLength="10" Text='<%# Bind("Address.ZipCode") %>' />
                                </td>
                            </tr>
<%--                            <tr>
                                <td>
                                    Birth date:
                                </td>
                                <td>
                                    <telerik:RadDatePicker ID="dpBirthDate" runat="server" DbSelectedDate='<%# Bind("BirthDate") %>' />
                                </td>
                            </tr>--%>
                            <tr>
                                <td colspan="2">
                                    <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                                runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' ValidationGroup="vgPerson" />
                                    <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                                                CommandName="Cancel" ValidationGroup="vgPerson" />
                                </td>
                            </tr>
                        </table>
                    </div>
                    <asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="vgPerson" ShowMessageBox="True" ShowSummary="False" />
                </FormTemplate>           

When I'm referencing the address information in the Bind I have to use 'Address.Street' in order to get to the street field of the Address object.  Same format for the other fields in the address object.  It would seem the form template doesn't like that.  Any suggestions would be very much appreciated.

3 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 29 Nov 2012, 12:17 PM
Hello,

<telerik:RadTextBox runat="server" ID="txtCity" MaxLength="50" Text='<%# Eval("Address") == null ? null : Eval("Address.City") %>' />
      or
      <telerik:RadTextBox runat="server" ID="RadTextBox1" MaxLength="50" Text='<%# (Container is GridEditFormInsertItem) ? string.Empty : Eval("Address.City") %>' />


Thanks,
Jayesh Goyani
0
Eric Downey
Top achievements
Rank 1
answered on 29 Nov 2012, 07:56 PM
Hhhhmmmmmm....that's interesting.  I see what you're doing there so I tried it and I also added a constructor in my person object that instantiated an address object so it should not be null.  I also created a constructor in the address object and just for good measure I primed the string fields to be string.empty.  Still doesn't work though.  The error sounds like it wants a property that is a type field like string, int, etc and doesn't know what to do with a pointer to an object.  I'm definitely no expert though - just sounds like that's what's going on.  Does Telerik have any demo examples of this type of coding being done with the grid?  The grid itself can display the address info fine and it seems to know how to handle the address.street, address.city, etc and it seems to be ok in edit mode as far as displaying the contents of the address object just not in insert mode.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 30 Nov 2012, 05:11 AM
Hello,

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" OnNeedDataSource="RadGrid1_NeedDataSource"
           AllowPaging="true" AllowFilteringByColumn="true" ShowFooter="true" AllowMultiRowSelection="true"
           AllowMultiRowEdit="true" PageSize="15">
           <MasterTableView CommandItemDisplay="Top" EditMode="EditForms">
               <Columns>
              
                   <telerik:GridBoundColumn DataField="FirstName" UniqueName="FirstName" HeaderText="FirstName">
                   </telerik:GridBoundColumn>
                   <telerik:GridBoundColumn DataField="Address.Street" UniqueName="Address.Street" HeaderText="Address.Street">
                   </telerik:GridBoundColumn>
                   <telerik:GridEditCommandColumn>
                   </telerik:GridEditCommandColumn>
               </Columns>
               <EditFormSettings EditFormType="Template">
                   <FormTemplate>
                       <telerik:RadTextBox runat="server" ID="txtState" MaxLength="2" Text='<%# (Container is GridEditFormInsertItem) ? null : Eval("Address.Street") %>' />
                   </FormTemplate>
               </EditFormSettings>
           </MasterTableView>
           <ClientSettings>
               <Selecting AllowRowSelect="true" />
           </ClientSettings>
           <PagerStyle AlwaysVisible="true" />
       </telerik:RadGrid>

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
         
        List<Person> plist = new List<Person>();
 
        Person p1 = new Person();
        p1.FirstName = "abc";
        Address a1 = new Address();
        a1.Street = "zzz";
        p1.Address = a1;
        plist.Add(p1);
 
 
        //RadGrid1.DataSource = data;
        RadGrid1.DataSource = plist;
    }
public class Person
{
    public string FirstName { get; set; }
    public Address Address { get; set; }
}
 
 
public class Address
{
    public string Street { get; set; }
}


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Eric Downey
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Eric Downey
Top achievements
Rank 1
Share this question
or