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

"TelerikDemo.User does not contain a property with the name Address" problem

4 Answers 64 Views
Grid
This is a migrated thread and some comments may be shown as answers.
t800t8
Top achievements
Rank 1
t800t8 asked on 30 Jan 2009, 08:30 AM
I have a master/detail RadGrid. I open the form to insert new record to Master table, then open the form to edit a record in Detail table. When I click Cancel on the form to create new, I have an HttpExpcetion "TelerikDemo.User does not contain a property with the name Address". Are there somethings I miss? Thank you.

Here is my markup:

<telerik:RadGrid ID="rgUsers" runat="server" GridLines="None"  
                AutoGenerateColumns="False" AutoGenerateDeleteColumn="True" AutoGenerateEditColumn="True"  
                oneditcommand="rgUsers_EditCommand" 
                onneeddatasource="rgUsers_NeedDataSource"  
                ondetailtabledatabind="rgUsers_DetailTableDataBind"  
                OnUpdateCommand="rgUsers_UpdateCommand" 
                OnInsertCommand="rgUsers_InsertCommand"  
                ondeletecommand="rgUsers_DeleteCommand"
                <HeaderContextMenu EnableTheming="True"
                    <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
                </HeaderContextMenu> 
 
                <MasterTableView Name="Users" CommandItemDisplay="Top" DataKeyNames="Id"
                    <DetailTables> 
                        <telerik:GridTableView Name="Contacts" AutoGenerateColumns="False" CommandItemDisplay="Top" DataKeyNames="Id"
                            <EditFormSettings EditFormType="Template"
                                <FormTemplate>                                     
                                    &nbsp;Address: 
                                    <asp:TextBox ID="txtAddress" runat="server" Text='<%# Bind("Address") %>' /> 
                                    <br /> 
                                    &nbsp;City: 
                                    <asp:TextBox ID="txtCity" runat="server" Text='<%# Bind("City") %>' /> 
                                    <br /> 
                                    &nbsp;Zip 
                                    <asp:TextBox ID="txtZip" runat="server" Text='<%# Bind("Zip") %>' /> 
                                    <br /> 
                                    &nbsp;<asp:LinkButton ID="btnUpdate" runat="server" CommandName="Update" Visible='<%# Eval("Id").ToString().Length != 0  %>'>Update</asp:LinkButton> 
                                    <asp:LinkButton ID="btnAdd" runat="server" CommandName="PerformInsert" Visible='<%# Eval("Id").ToString().Length == 0  %>'>Add</asp:LinkButton> 
                                    &nbsp;&nbsp;&nbsp; 
                                    <asp:LinkButton ID="btnCancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton> 
                                </FormTemplate> 
                            </EditFormSettings> 
                             
                            <Columns> 
                                <telerik:GridBoundColumn DataField="Address" HeaderText="Address" SortExpression="Address" UniqueName="Address"
                                </telerik:GridBoundColumn> 
                                <telerik:GridBoundColumn DataField="City" HeaderText="City" SortExpression="City" UniqueName="City"
                                </telerik:GridBoundColumn> 
                                <telerik:GridBoundColumn DataField="Zip" HeaderText="Zip" SortExpression="Zip" UniqueName="Zip"
                                </telerik:GridBoundColumn> 
                            </Columns> 
                             
                            <CommandItemTemplate> 
                                <asp:LinkButton ID="btnInitInsert" runat="server" CommandName="InitInsert"
                                    Add new Contact 
                                </asp:LinkButton> 
                                <asp:LinkButton ID="btnRefresh" runat="server" CommandName="RebindGrid"
                                    Refresh contact list 
                                </asp:LinkButton> 
                            </CommandItemTemplate> 
                        </telerik:GridTableView> 
                    </DetailTables> 
                 
                    <EditFormSettings EditFormType="Template"
                        <FormTemplate> 
                            &nbsp;First name: 
                            <asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName") %>'></asp:TextBox> 
                            <br /> 
                            &nbsp;Last name: 
                            <asp:TextBox ID="txtLastName" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox> 
                            <br /> 
                            &nbsp;<asp:LinkButton ID="btnUpdate" runat="server" CommandName="Update" Visible='<%# Eval("Id").ToString().Length != 0  %>'>Update</asp:LinkButton> 
                            <asp:LinkButton ID="btnAdd" runat="server" CommandName="PerformInsert" Visible='<%# Eval("Id").ToString().Length == 0 %>'>Add</asp:LinkButton> 
                            &nbsp;&nbsp;&nbsp; 
                            <asp:LinkButton ID="btnCancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton> 
                        </FormTemplate> 
                    </EditFormSettings> 
                     
                    <CommandItemTemplate> 
                        <asp:LinkButton ID="btnInitInsert" runat="server" CommandName="InitInsert" Visible='<%# !rgUsers.MasterTableView.IsItemInserted %>'
                            Add new Customer 
                        </asp:LinkButton> 
                        <asp:LinkButton ID="btnRefresh" runat="server" CommandName="RebindGrid"
                            Refresh customer list 
                        </asp:LinkButton> 
                    </CommandItemTemplate> 
 
                    <RowIndicatorColumn> 
                        <HeaderStyle Width="20px"></HeaderStyle> 
                    </RowIndicatorColumn> 
 
                    <ExpandCollapseColumn> 
                        <HeaderStyle Width="20px"></HeaderStyle> 
                    </ExpandCollapseColumn> 
                     
                    <Columns> 
                        <telerik:GridBoundColumn DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" UniqueName="FirstName"
                        </telerik:GridBoundColumn> 
                        <telerik:GridBoundColumn DataField="LastName" HeaderText="Last Name" SortExpression="LastName" UniqueName="LastName"
                        </telerik:GridBoundColumn> 
                    </Columns> 
                </MasterTableView> 
 
                <FilterMenu EnableTheming="True"
                    <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> 
                </FilterMenu> 
            </telerik:RadGrid>         

Here is my C# code:

protected void Page_Load(object sender, EventArgs e) 
        { 
        } 
 
        protected void rgUsers_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
        { 
            rgUsers.DataSource = TelerikDemo.User.GetAll(); 
        } 
 
        protected void rgUsers_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e) 
        { 
            GridDataItem parent = e.DetailTableView.ParentItem; 
            int userId = Convert.ToInt32(parent.GetDataKeyValue("Id")); 
            rgUsers.MasterTableView.DetailTables[0].DataSource = Contact.GetAllContactsForUser(userId); 
        } 
 
        protected void rgUsers_EditCommand(object source, GridCommandEventArgs e) 
        { 
            e.Item.Edit = true
        } 
 
        protected void rgUsers_UpdateCommand(object source, GridCommandEventArgs e) 
        { 
            GridEditableItem editedItem = e.Item as GridEditableItem; 
 
            if (e.Item.OwnerTableView.Name.Equals("Users")) // Update user 
            { 
                int userId = Convert.ToInt32(editedItem.GetDataKeyValue("Id")); 
                string firstName = (editedItem.FindControl("txtFirstName"as TextBox).Text; 
                string lastName = (editedItem.FindControl("txtLastName"as TextBox).Text; 
 
                TelerikDemo.User.Update(userId, firstName, lastName); 
            } 
            else if (e.Item.OwnerTableView.Name.Equals("Contacts")) // Update contact 
            { 
                int contactId = Convert.ToInt32(editedItem.GetDataKeyValue("Id")); 
                string address = (editedItem.FindControl("txtAddress"as TextBox).Text; 
                string city = (editedItem.FindControl("txtCity"as TextBox).Text; 
                string zip = (editedItem.FindControl("txtZip"as TextBox).Text; 
 
                Contact.Update(contactId, address, city, zip); 
            } 
        } 
 
        protected void rgUsers_InsertCommand(object source, GridCommandEventArgs e) 
        { 
            GridEditableItem editedItem = e.Item as GridEditableItem; 
 
            if (e.Item.OwnerTableView.Name.Equals("Users")) // Create user 
            { 
                string firstName = (editedItem.FindControl("txtFirstName"as TextBox).Text; 
                string lastName = (editedItem.FindControl("txtLastName"as TextBox).Text; 
 
                TelerikDemo.User.Create(firstName, lastName); 
            } 
            else if (e.Item.OwnerTableView.Name.Equals("Contacts"))  // Create contact 
            { 
                string address = (editedItem.FindControl("txtAddress"as TextBox).Text; 
                string city = (editedItem.FindControl("txtCity"as TextBox).Text; 
                string zip = (editedItem.FindControl("txtZip"as TextBox).Text; 
                GridDataItem parent = e.Item.OwnerTableView.ParentItem; 
                int userId = Convert.ToInt32(parent.GetDataKeyValue("Id")); 
 
                Contact.Create(address, city, zip, userId); 
            } 
        } 
 
        protected void rgUsers_DeleteCommand(object source, GridCommandEventArgs e) 
        { 
            GridEditableItem editedItem = e.Item as GridEditableItem;             
            int id = Convert.ToInt32(editedItem.GetDataKeyValue("Id")); 
 
            if (e.Item.OwnerTableView.Name.Equals("Users")) // Remove user 
            { 
                TelerikDemo.User.Remove(id); 
            } 
            else if (e.Item.OwnerTableView.Name.Equals("Contacts"))  // Remove contact 
            { 
                Contact.Remove(id); 
            } 
        } 

4 Answers, 1 is accepted

Sort by
0
t800t8
Top achievements
Rank 1
answered on 02 Feb 2009, 03:03 AM
Anyone can help? Thanks
0
t800t8
Top achievements
Rank 1
answered on 02 Feb 2009, 06:08 AM
The problem here is when I click Cancel to close the edit form for master row, RadGrid will re-fill data for the edit form for detail row, but at that time the selected item is the MASTER one, not the DETAIL one. How can I re-fill the edit from for detail row without any problems?
0
Accepted
Rosen
Telerik team
answered on 02 Feb 2009, 02:55 PM
Hello,

I have examined the code you have pasted and I have noticed that you are incorrectly binding the details table inside DetailTableDataBind. You should assign the datasouce to the currently binded tableView. Thus your code should look similar to the following:

        GridDataItem parent = e.DetailTableView.ParentItem;  
        int userId = Convert.ToInt32(parent.GetDataKeyValue("Id"));  
        e.DetailTableView.DataSource = Contact.GetAllContactsForUser(userId); 

Please give it a try and let us know if this helps.

Regards,
Rosen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
t800t8
Top achievements
Rank 1
answered on 03 Feb 2009, 07:18 AM
Dear Rosen,

It works. You are the man. Thank you :-)

Best regards,
t800t8
Tags
Grid
Asked by
t800t8
Top achievements
Rank 1
Answers by
t800t8
Top achievements
Rank 1
Rosen
Telerik team
Share this question
or