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

Nested TemplateColumn FindControl

2 Answers 148 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 2
Mike asked on 12 Nov 2010, 07:58 PM

Quick question - What's the best way to access a control in a nested GridTableView (specifically lblPassword below) in the code behind?  The password is encrypted in the database and I need to decrypt before displaying on the web page.  

<telerik:RadGrid runat="server" ID="gridDatabases" AutoGenerateColumns="False"
    GridLines="None" Skin="Windows7" Width="100%"                 
    DataSourceID="LinqDataSourceDatabases" 
    OnItemDataBound="gridDatabases_ItemDataBound">
    <MasterTableView DataSourceID="LinqDataSourceDatabases" DataKeyNames="ID">
        <DetailTables>
            <telerik:GridTableView DataKeyNames="ID" DataSourceID="LinqDataSourceUsers" runat="server">
                <ParentTableRelation>
                    <telerik:GridRelationFields DetailKeyField="ID" MasterKeyField="DatabaseDetailID" />                                
                </ParentTableRelation>
                <Columns>
                    <telerik:GridBoundColumn DataField="UserName" HeaderText="User Name" SortExpression="UserName" UniqueName="UserName" />
                    <telerik:GridTemplateColumn HeaderText="Password">
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblPassword" />
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </telerik:GridTableView>
        </DetailTables>
        <ExpandCollapseColumn Visible="True">
        </ExpandCollapseColumn>
        <Columns>
            <telerik:GridBoundColumn DataField="DatabaseName" HeaderText="Database" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>


I thought I could use the OnItemDatabound event, like this, but I can't seem call FindControl to drill down to get to the nested label control.  This is basically what I'm trying to accomplish, and this would work if the control wasn't in a nested table.

protected void gridUserDetails_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
    {
        GridDataItem gridItem = (GridDataItem)e.Item;
        UserDetail userDetail = (UserDetail)gridItem.DataItem;
        Label lblPassword = (Label)gridItem.FindControl("lblPassword");
        string decryptedPassword = DecryptPassword(userDetail.Password);
        lblPassword.Text = decryptedPassword;
    }
}

Any help is greatly appreciated!  Thanks.

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 15 Nov 2010, 05:55 AM
Hello Mike,

To identify the table to which the current row belongs/is bound (in the ItemCreated/ItemDataBound handler), you can use the DataSourceID property of the respective GridTableView .

C#:
protected void gridUserDetails_ItemDataBound(object sender, GridItemEventArgs e)
   {
       
       if (e.Item is GridDataItem && e.Item.OwnerTableView.DataSourceID == "LinqDataSourceUsers")
       //identify to which table belongs the currently bound item
       {
           GridDataItem gridItem = (GridDataItem)e.Item;
           Label lblPassword = (Label)gridItem.FindControl("lblPassword"); // accessing Label inside DetailTable
           . . . . . . . .
       }
   }

Please refer the following documentation for more details.
Distinguish grid rows in hierarchy on ItemCreated/ItemDataBound

Thanks,
Princy.
0
Mike
Top achievements
Rank 2
answered on 15 Nov 2010, 05:20 PM
Hello Princy, thanks for the reply.  Unfortunately, I do not have a gridUserDetails control. I am using a nested GridTableView with the name UserDetailsView and that does not have an itemdatabound event I can hook into.   Thanks for the help though! 

Edit:  You however did point me in the right direction!  What I needed to do was find the label using the OwnerTableView.Name on the OnItemCreated event, not the OnItemDatabound event.
protected void gridDatabaseDetails_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item.OwnerTableView.Name == "UserDetailsView")
        {
            Label lblPassword = (Label)e.Item.FindControl("lblPassword");
            if (lblPassword != null)
            {
                GridDataItem gridItem = (GridDataItem)e.Item;
                UserDetail userDetail = (UserDetail)gridItem.DataItem;
                lblPassword.Text = DecryptPassword(userDetail.Password);
            }
        }
    }

Thanks again.
Tags
Grid
Asked by
Mike
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Mike
Top achievements
Rank 2
Share this question
or