locked
how to findControl in RadGrid hierarchical RRS feed

  • Question

  • User729207731 posted

    i have radgrid with 4 level, at level 3 i have a linkbutton to view details, but i find control then error.

     protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
            {
                if (e.Item is GridDataItem)
                {
                    HyperLink editLink = (HyperLink)e.Item.FindControl("EditLink");
                    editLink.Attributes["href"] = "javascript:void(0);";
                    editLink.Attributes["onclick"] = String.Format("return ShowEditForm('{0}','{1}');", e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["id3"], e.Item.ItemIndex);
                }
    }



    and aspx

    ...................
    ......
    
    
    <ParentTableRelation>
                                                                    <telerik:GridRelationFields MasterKeyField="id2" DetailKeyField="idc3"></telerik:GridRelationFields>
                                                                </ParentTableRelation>
    
    
                                                                <Columns>
                                                                    <telerik:GridBoundColumn SortExpression="id3" HeaderText="Id" HeaderButtonType="TextButton"
                                                                        DataField="id3" UniqueName="id3">
                                                                        <HeaderStyle Width="112px" />
                                                                    </telerik:GridBoundColumn>
                                                                    <telerik:GridBoundColumn Visible="false" SortExpression="idc3" HeaderText="father" HeaderButtonType="TextButton"
                                                                        DataField="id3" UniqueName="id33">
                                                                    </telerik:GridBoundColumn>
                                                                    
    
                                                                    <telerik:GridTemplateColumn UniqueName="TemplateEditColumn">
                                                                        <ItemTemplate>
                                                                            <asp:HyperLink ID="EditLink" runat="server" Text="Xem chi tiết"></asp:HyperLink>
                                                                        </ItemTemplate>
                                                                    </telerik:GridTemplateColumn>
    
    
                                                                    <telerik:GridButtonColumn FilterControlAltText="Filter column column" UniqueName="column"></telerik:GridButtonColumn>
                                                                </Columns>
    
                                                            </telerik:GridTableView>
    
    ..........
    .........

    when i run and error

    Server Error in '/' Application.
    
    Object reference not set to an instance of an object.
    
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    
    Source Error: 
    
    
    Line 119:            {
    Line 120:                HyperLink editLink = (HyperLink)e.Item.FindControl("EditLink");
    Line 121:                editLink.Attributes["href"] = "javascript:void(0);";
    Line 122:                editLink.Attributes["onclick"] = String.Format("return ShowEditForm('{0}','{1}');", e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["id3"], e.Item.ItemIndex);
    Line 123:            }
    

    plz help me, thanks in advance

    Thursday, October 27, 2016 4:27 AM

All replies

  • User1724605321 posted

    Hi hoitm,

    Please try below code  :

    if (e.Item is GridDataItem)  
    {  
       GridDataItem item = (GridDataItem)e.Item;  
       HyperLink editLink = (HyperLink)item["TemplateEditColumn"].FindControl("EditLink");   
    }

    Best Regards,

    Nan Yu

    Friday, October 28, 2016 5:40 AM