locked
On cc1:XDataGrid data load, shows Unable to cast object error RRS feed

  • Question

  • User1914091530 posted

    Below Aspx page

                                                            <asp:TemplateColumn HeaderText="Corp A/C" ItemStyle-Height="50px">
                                                                <ItemTemplate>
                                                                    <asp:Label runat="server" style="word-wrap:break-word;" Text='<%# DataBinder.Eval(Container, "DataItem.AccountNo") %>'
                                                                        ID="Corpaccount">
                                                                    </asp:Label>
                                                                    
                                                                 </ItemTemplate>
                                                                <HeaderStyle width="10%"></HeaderStyle>
                                                            </asp:TemplateColumn>
                                                            <asp:TemplateColumn HeaderText="Corp Name">
                                                                <ItemTemplate>
                                                                    <asp:Label runat="server" style="word-wrap:break-word;" Text='<%# DataBinder.Eval(Container, "DataItem.CorporateName") %>'
                                                                        ID="CorpName">
                                                                    </asp:Label>
                                                                
                                                                 </ItemTemplate>
                                                                <HeaderStyle width="4%"></HeaderStyle>
                                                            </asp:TemplateColumn>
                                                            <asp:ButtonColumn DataTextField="FileName" HeaderText="NON-MOL File" CommandName="FileName" >
                                                                <HeaderStyle width="15%" />
                                                            </asp:ButtonColumn>

    shows below error on data load

    Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.LinkButton'.

    But below aspx page

                                                            <asp:TemplateColumn HeaderText="Corp A/C" ItemStyle-Height="50px">
                                                                <ItemTemplate>
                                                                    <asp:Label runat="server" style="word-wrap:break-word;" Text='<%# DataBinder.Eval(Container, "DataItem.AccountNo") %>'
                                                                        ID="Corpaccount">
                                                                    </asp:Label>
                                                                    
                                                                 </ItemTemplate>
                                                                <HeaderStyle width="10%"></HeaderStyle>
                                                            </asp:TemplateColumn>
    
                                                            <asp:ButtonColumn DataTextField="FileName" HeaderText="NON-MOL File" CommandName="FileName" >
                                                                <HeaderStyle width="15%" />
                                                            </asp:ButtonColumn>
                                                            <asp:TemplateColumn HeaderText="Corp Name">
                                                                <ItemTemplate>
                                                                    <asp:Label runat="server" style="word-wrap:break-word;" Text='<%# DataBinder.Eval(Container, "DataItem.CorporateName") %>'
                                                                        ID="CorpName">
                                                                    </asp:Label>                                                            
                                                                 </ItemTemplate>
                                                                <HeaderStyle width="4%"></HeaderStyle>
                                                            </asp:TemplateColumn>

    has no error on data load

    Please help?

    Thursday, February 21, 2019 6:05 AM

Answers

  • User-1174608757 posted

    Hi shijiostephen,

    Could you please post the code behind? I don't think the error happens just because changing the position of column.I think there exists  operations on the foreground page in the code behind. 

    Best Regards

    Wei Zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 25, 2019 9:01 AM
  • User1914091530 posted

    It was error in the ItemDataBound event

        protected void grdSalaryPostCutOffQueue_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            try
            {
                if ((e.Item.ItemType == ListItemType.AlternatingItem) || (e.Item.ItemType == ListItemType.Item))
                {
                    System.Web.UI.WebControls.LinkButton btnColumn = (System.Web.UI.WebControls.LinkButton)e.Item.Cells[3].Controls[0];

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 25, 2019 11:41 AM

All replies

  • User-1174608757 posted

    Hi shijostephen,

    According to your description, the error is clearly telling you that you cannot convert an object of type A to an object of type B.This error happens in your code behind. So I hope you could post more detail about your codes in code behind.

    Else,simply look at this error message, I think you are trying to convert the label control to  LinkButton in code behind.Error may happen in some codes as 

     LinkButton lblCountry = (LinkButton)row.Cells[4].Controls[0];

    Best  Regards

    Wei Zhang

    Friday, February 22, 2019 2:02 AM
  • User1914091530 posted

    But below aspx page

                                                            <asp:TemplateColumn HeaderText="Corp A/C" ItemStyle-Height="50px">
                                                                <ItemTemplate>
                                                                    <asp:Label runat="server" style="word-wrap:break-word;" Text='<%# DataBinder.Eval(Container, "DataItem.AccountNo") %>'
                                                                        ID="Corpaccount">
                                                                    </asp:Label>
                                                                    
                                                                 </ItemTemplate>
                                                                <HeaderStyle width="10%"></HeaderStyle>
                                                            </asp:TemplateColumn>
    
                                                            <asp:ButtonColumn DataTextField="FileName" HeaderText="NON-MOL File" CommandName="FileName" >
                                                                <HeaderStyle width="15%" />
                                                            </asp:ButtonColumn>
                                                            <asp:TemplateColumn HeaderText="Corp Name">
                                                                <ItemTemplate>
                                                                    <asp:Label runat="server" style="word-wrap:break-word;" Text='<%# DataBinder.Eval(Container, "DataItem.CorporateName") %>'
                                                                        ID="CorpName">
                                                                    </asp:Label>                                                            
                                                                 </ItemTemplate>
                                                                <HeaderStyle width="4%"></HeaderStyle>
                                                            </asp:TemplateColumn>

    has no error on data load, Here in this I have just changed the position of "Corp Name" Template Column.

    Please tell how this error could happen for simply changing the grid column position..

    Friday, February 22, 2019 11:56 AM
  • User-1174608757 posted

    Hi shijiostephen,

    Could you please post the code behind? I don't think the error happens just because changing the position of column.I think there exists  operations on the foreground page in the code behind. 

    Best Regards

    Wei Zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 25, 2019 9:01 AM
  • User1914091530 posted

    It was error in the ItemDataBound event

        protected void grdSalaryPostCutOffQueue_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            try
            {
                if ((e.Item.ItemType == ListItemType.AlternatingItem) || (e.Item.ItemType == ListItemType.Item))
                {
                    System.Web.UI.WebControls.LinkButton btnColumn = (System.Web.UI.WebControls.LinkButton)e.Item.Cells[3].Controls[0];

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 25, 2019 11:41 AM