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

Binding Error - Type does not contain property 'X'

6 Answers 92 Views
Grid
This is a migrated thread and some comments may be shown as answers.
digitall
Top achievements
Rank 1
digitall asked on 17 Aug 2010, 12:55 AM
I have a hierarchical grid setup with a single child level. When the page loads the grid shows perfectly fine - all levels (hierarchy mode is Client) show up and everything looks great. When I use the Delete command to remove one of the child items the delete process works fine, however the Rebind method dies. It seems when the Rebind() method gets called the detail table's data source is actually being set to the parent object so upon binding it tells me the type I am trying to call a property off of (ImageName is the property) doesn't exist. Any thoughts here? It doesn't make sense that a Rebind wouldn't work, yet binding when the page loads initially works fine. Here is the code:

protected void grProjects_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    if (!e.IsFromDetailTable)
    {
        this.grProjects.DataSource = (from c in new DataContext().Projects select c).ToArray();
    }
}
 
protected void grProjects_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e)
{
    if (e.DetailTableView.Name == "Images")
    {
        GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
        e.DetailTableView.DataSource = ((Project)dataItem.DataItem).ProjectItems;
    }
}

<telerik:RadGrid runat="server" ID="grProjects" OnNeedDataSource="grProjects_NeedDataSource" Skin="Windows7" AutoGenerateColumns="false" ShowFooter="false" OnItemDataBound="grProjects_ItemDataBound" OnItemCommand="grProjects_ItemCommand" OnDetailTableDataBind="grProjects_DetailTableDataBind">
    <MasterTableView DataKeyNames="Id" CommandItemDisplay="Top" CommandItemStyle-Height="30px" ItemStyle-VerticalAlign="Top" AlternatingItemStyle-VerticalAlign="Top" HierarchyDefaultExpanded="false" HierarchyLoadMode="Client">
        <CommandItemTemplate>
            <input type="button" class="rgAdd" onclick="javascript:addItem();" style="margin-left: 10px;" /><a href="javascript:;" onclick="javascript:addItem();">Add Project</a>
        </CommandItemTemplate>
        <Columns>
            <telerik:GridTemplateColumn ItemStyle-Width="20px" ItemStyle-Wrap="false">
                <ItemTemplate>
                    <asp:Button runat="server" ID="cmEdit" Text="Edit" CssClass="rgEdit" />
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridBoundColumn HeaderText="Name" DataField="Name" ItemStyle-Width="125px" />
            <telerik:GridBoundColumn HeaderText="Description" DataField="Description" />
            <telerik:GridButtonColumn ButtonType="ImageButton" HeaderStyle-Width="20px" ItemStyle-Width="20px" CommandName="Delete" ConfirmDialogType="RadWindow" ConfirmText="Are you sure you wish to permanently remove this project?<br /><br /><strong>All images contained inside will be removed as well and this action is not reversible.</strong>" />
        </Columns>
        <DetailTables>
            <telerik:GridTableView Name="Images" DataKeyNames="Id" CommandItemDisplay="Top" CommandItemStyle-Height="30px" ItemStyle-VerticalAlign="Top" AlternatingItemStyle-VerticalAlign="Top" Width="100%">
                <CommandItemTemplate>
                    <asp:Button runat="server" ID="cmAddItem" CssClass="rgAdd" style="margin-left: 10px;" /><asp:HyperLink runat="server" ID="lkAddItem" Text="Add Image" NavigateUrl="javascript:;" />
                </CommandItemTemplate>
                <Columns>
                    <telerik:GridTemplateColumn ItemStyle-Width="20px" ItemStyle-Wrap="false">
                        <ItemTemplate>
                            <asp:Button runat="server" ID="cmEdit" Text="Edit" CssClass="rgEdit" />
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn HeaderText="Image" ItemStyle-Width="100px">
                        <ItemTemplate>
                            <asp:Image runat="server" ImageUrl='<%# GetImageUrl(Eval("ImageName")) %>' />
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn HeaderText="Caption">
                        <ItemTemplate>
                            <%#(Eval("Caption") != null) ? Eval("Caption").ToString().Replace("\r\n", "<br />") : string.Empty %>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridButtonColumn ButtonType="ImageButton" HeaderStyle-Width="20px" ItemStyle-Width="20px" CommandName="Delete" ConfirmDialogType="RadWindow" ConfirmText="Are you sure you wish to permanently remove this item?<br /><br /><strong>This action is not reversible.</strong>" />
                </Columns>
            </telerik:GridTableView>
        </DetailTables>
    </MasterTableView>
</telerik:RadGrid>

6 Answers, 1 is accepted

Sort by
0
digitall
Top achievements
Rank 1
answered on 19 Aug 2010, 09:12 PM
Any thoughts on this?
0
Maria Ilieva
Telerik team
answered on 20 Aug 2010, 02:15 PM
Hi,

Could you please elaborate a bit more on your application? Are you calling Rebind () method explicitly? Also please provide us the DeleteCommand code so we could further research on the problem.

Sincerely yours,
Maria Ilieva
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
digitall
Top achievements
Rank 1
answered on 20 Aug 2010, 02:23 PM
Yes, I call Rebind() explicitly. Here is the delete code:

protected void grProjects_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == "Delete")
    {
        using (DataContext data = new DataContext())
        {
            if (this.grProjects.Items[e.Item.ItemIndexHierarchical].CanExpand)
            {
                data.Projects.DeleteOnSubmit((from p in data.Projects where p.Id == int.Parse(this.grProjects.Items[e.Item.ItemIndexHierarchical].GetDataKeyValue("Id").ToString()) select p).Single());
            }
            else data.ProjectItems.DeleteOnSubmit((from p in data.ProjectItems where p.Id == int.Parse(this.grProjects.Items[e.Item.ItemIndexHierarchical].GetDataKeyValue("Id").ToString()) select p).Single());
             
            data.SubmitChanges();
        }
 
        this.grProjects.Rebind();
    }
}

I also have the following for ItemDataBound:

protected void grProjects_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        if (e.Item.DataItem is Project)
        {
            ((Button)e.Item.FindControl("cmEdit")).OnClientClick = "javascript:return editItem(" + ((Project)e.Item.DataItem).Id + ");";
        }
        else if (e.Item.DataItem is ProjectItem)
        {
            ((Button)e.Item.FindControl("cmEdit")).OnClientClick = "javascript:return editProjectItem(" + ((ProjectItem)e.Item.DataItem).Id + ");";
        }
    }
    else if (e.Item is GridCommandItem)
    {
        Button cmAddItem = e.Item.FindControl("cmAddItem") as Button;
        if (cmAddItem != null)
        {
            //child item binding
            cmAddItem.OnClientClick = "return addProjectItem(" + e.Item.OwnerTableView.ParentItem.GetDataKeyValue("Id").ToString() + ");";
            ((HyperLink)e.Item.FindControl("lkAddItem")).Attributes.Add("onclick", "javascript:" + cmAddItem.OnClientClick);
        }
    }
}

The last block is post data-binding:

void grProjects_DataBound(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(this.Request.QueryString["pid"]))
    {
        int id;
        if (int.TryParse(this.Request.QueryString["pid"], out id))
        {
            foreach (GridDataItem item in this.grProjects.Items)
            {
                if (int.Parse(item.GetDataKeyValue("Id").ToString()) == id && item.CanExpand == true)
                {
                    item.Expanded = true;
                }
                else if (item.CanExpand == false)
                {
                    if (int.TryParse(this.Request.QueryString["Id"], out id))
                        if (int.Parse(item.GetDataKeyValue("Id").ToString()) == id) item.Selected = true;
                }
            }
        }
    }
 
}

0
digitall
Top achievements
Rank 1
answered on 24 Aug 2010, 02:10 PM
Anything?
0
Accepted
Iana Tsolova
Telerik team
answered on 25 Aug 2010, 01:32 PM
Hello digitall,

The grid delete command implicitly rebinds the grid. Therefore there is no need to rebind it explicitly. However in case you have to call the Rebind() method, try canceling the default command prior to the call:

e.Canceled = true;

All the best,
Iana
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
digitall
Top achievements
Rank 1
answered on 26 Aug 2010, 01:59 PM
Worked perfectly - thanks!
Tags
Grid
Asked by
digitall
Top achievements
Rank 1
Answers by
digitall
Top achievements
Rank 1
Maria Ilieva
Telerik team
Iana Tsolova
Telerik team
Share this question
or