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

Using findControl in ItemCreated in a DetailTable

3 Answers 246 Views
Grid
This is a migrated thread and some comments may be shown as answers.
pbarron
Top achievements
Rank 1
pbarron asked on 16 Oct 2008, 02:35 PM
Hi,

I've got a Radgrid with a detailTable and I want to add an onclick attribute using the ItemCreated function.

I have done this previously in a RadGrid using the following code:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem){
HyperLink editLink = (HyperLink)e.Item.FindControl("UserResults");
editLink.Attributes[
"href"] = "#";
editLink.Attributes[
"onclick"] = String.Format("return ShowUserResults('{0}','{1}');", e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["AssessmentId"], e.Item.ItemIndex);}
}


However I am having some trouble referencing the hyperlink now it is in a detailTable.
Can anyone help me rewrite my code to add the attribute to the 'IndividualResults' hyperlink in the following radgrid/detailTable:

 

<telerik:RadGrid ID="RadGrid2" runat="server" Skin="WebBlue" EnableEmbeddedSkins="False"GridLines="None" AllowMultiRowSelection="true" ShowStatusBar="false" ShowFooter="false" ImagesPath="~/img/Grid/" AutoGenerateColumns="False" OnItemCreated="RadGrid2_ItemCreated">

<
MasterTableView AutoGenerateColumns="False" DataKeyNames="UserId">
<Columns>
<telerik:GridBoundColumn DataField="UserId" HeaderText="UserId" ReadOnly="True" SortExpression="UserId" UniqueName="UserId" Visible="false">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="StudentName" HeaderText="Name" ReadOnly="True" SortExpression="StudentName" UniqueName="StudentName">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="UserName" HeaderText="Username" ReadOnly="True" SortExpression="UserName" UniqueName="UserName">
</telerik:GridBoundColumn>
</Columns>

 

 

<DetailTables>
<telerik:GridTableView DataKeyNames="UserId" Width="100%" Name="IndividualAssessments" runat="server" TableLayout="Fixed" AutoGenerateColumns="false" CssClass="subForm">
<ParentTableRelation>
<telerik:GridRelationFields DetailKeyField="UserId" MasterKeyField="UserId" />
</ParentTableRelation>
<Columns>
<telerik:GridBoundColumn DataField="UserId" HeaderText="UserId" ReadOnly="True" SortExpression="UserId" UniqueName="UserId" Visible="false">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn UniqueName="IndividualResults" HeaderText="Results">
<ItemTemplate>
<asp:HyperLink ID="IndividualResults" runat="server">
<asp:Image ID="imgResults" AlternateText="Individual results" ImageUrl="../img/grid/status_report.gif" runat="server" />
</asp:HyperLink>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="AssessmentName" HeaderText="Name" ReadOnly="True" SortExpression="AssessmentName" UniqueName="AssessmentName">
</telerik:GridBoundColumn>
</Columns>
</telerik:GridTableView>
</DetailTables>
</MasterTableView>

</telerik:RadGrid>

 

3 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 17 Oct 2008, 05:38 AM
Hi,

To access a control in the DetailTable you need to access it as shown below using the Name Property of the DetailTable.
cs:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem && e.Item.OwnerTableView.Name == "IndividualAssessments") 
        { 
            GridDataItem data = (GridDataItem)e.Item; 
            HyperLink hyper = (HyperLink)data.FindControl("IndividualResults"); 
            .... 
        } 
    } 

Thanks
Princy.
0
pbarron
Top achievements
Rank 1
answered on 17 Oct 2008, 09:18 AM
Thanks Princy that worked!

However I have one more issue - I need to pass the value of one of the other fields, I have tried this:

e.Item.OwnerTableView.Items[e.Item.ItemIndex][

"AssessmentId"]

But it does not work, what am I doing wrong?

The entire line is:

 

hyper.Attributes[

"onclick"] = String.Format("return ShowIndividualUserResults('{0}','{1}','{2}');", e.Item.OwnerTableView.Items[e.Item.ItemIndex]["AssessmentId"], e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["UserId"], e.Item.ItemIndex);

Thanks for your help

 

0
pbarron
Top achievements
Rank 1
answered on 17 Oct 2008, 10:04 AM
I worked it out by adding the 'AssessmentId' field to the DataKeyNames array and referencing it that way:

hyper.Attributes[

"onclick"] = String.Format("return ShowIndividualUserResults('{0}','{1}','{2}');", e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["AssessmentId"], e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["UserId"], e.Item.ItemIndex);

 

Tags
Grid
Asked by
pbarron
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
pbarron
Top achievements
Rank 1
Share this question
or