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

Find a control in ItemDataBound

8 Answers 572 Views
Grid
This is a migrated thread and some comments may be shown as answers.
kollam2003
Top achievements
Rank 1
kollam2003 asked on 30 May 2009, 11:06 AM
I have a asp chart in the item template in the detail table view of the gris.
How to find it in ItemDataBound?

8 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 30 May 2009, 11:29 AM
Hello kollam2003,

You could use the FindControl method (e.Item.FindControl("yourControl")) on ItemCreated or ItemDataBound events.

Regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
kollam2003
Top achievements
Rank 1
answered on 30 May 2009, 11:37 AM
In the itemdatabound i used as
Chart Chart_CourseGrade =  (e.Item.FindControl("Chart_CourseGrade") as Chart);

The chart is in the template field. I have a label here itself.
I can find the label control, bt the chart is returning null
0
Daniel
Telerik team
answered on 30 May 2009, 05:21 PM
Replace Chart with RadChart as shown below:

protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    if (e.Item is GridDataItem) 
    { 
        RadChart chart = e.Item.FindControl("Chart_CourseGrade"as RadChart; 
        chart.ChartTitle.TextBlock.Text = "CHANGED!"
    } 

Regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
kollam2003
Top achievements
Rank 1
answered on 01 Jun 2009, 05:45 AM
Hai, I'm using a Asp Chart here.
<telerik:RadGrid id="RadGrid_CourseGrade" runat="server"  
                                 gridlines="None"  
                                AllowFilteringByColumn="True"  
                        AllowMultiRowSelection="True" AllowPaging="True" AllowSorting="True"  
                        PagerStyle-Mode="NextPrevAndNumeric"  
                        AutoGenerateColumns="False" 
                        EnableTheming="True" oninit="RadGrid_CourseGrade_Init"  
                        OnNeedDataSource="RadGrid_CourseGrade_NeedDataSource"  
                        OnDetailTableDataBind="RadGrid_CourseGrade_DetailTableDataBind" 
                        OnItemCommand = "RadGrid_CourseGrade_ItemCommand" 
                         OnItemDataBound="RadGrid_CourseGrade_ItemDataBound" 
                        PageSize="25"  
                        Skin="Office2007"  > 
                        <HeaderStyle ForeColor="#192666" Font-Bold="True" /> 
                        <HeaderStyle ForeColor="#192666" Font-Bold="True" /> 
 
                        <PagerStyle AlwaysVisible="true" /> 
                        <HeaderContextMenu> 
                            <CollapseAnimation Duration="200" Type="OutQuint" /> 
                        </HeaderContextMenu> 
                        <PagerStyle AlwaysVisible="True" Position="TopAndBottom" /> 
                               <ClientSettings> 
                                    <Selecting AllowRowSelect="true" /> 
                                </ClientSettings> 
                                <MasterTableView DataKeyNames="INSID" AllowMultiColumnSorting="True" > 
                                    <DetailTables> 
                                        <telerik:GridTableView datakeynames="INSID" name ="INSID" gridlines="None" runat="server"
                                            <Columns> 
                                             <telerik:GridClientSelectColumn UniqueName="Chk_DeleteAll"
                                        </telerik:GridClientSelectColumn> 
                                              <telerik:GridTemplateColumn datafield="SCCID" headertext="SCCID" uniquename="SCCID" AllowFiltering="false" 
                                                visible="False"
                                                <ItemTemplate> 
                                                    <asp:Label id="lblSCCID" runat="server" text='<%# Eval("SCCID") %>'></asp:Label> 
                                                </ItemTemplate> 
                                                </telerik:GridTemplateColumn> 
                                                    <telerik:GridBoundColumn datafield="CourseName" headertext="Course" sortexpression="CourseName" 
                                                        uniquename="CourseName"
                                                         <HeaderStyle ForeColor="#192666" Font-Bold="True" /> 
                                                    </telerik:GridBoundColumn> 
                                                     
                                                     <telerik:GridBoundColumn datafield="Date" headertext="Date" sortexpression="Date" 
                                                        uniquename="Date"
                                                         <HeaderStyle ForeColor="#192666" Font-Bold="True" /> 
                                                    </telerik:GridBoundColumn> 
                                                     
                                                     <telerik:GridBoundColumn datafield="Grade" headertext="Grade" sortexpression="Grade" AllowFiltering="false" 
                                                        uniquename="Grade"
                                                         <HeaderStyle ForeColor="#192666" Font-Bold="True" /> 
                                                          <ItemStyle horizontalalign="Center" /> 
                                                    </telerik:GridBoundColumn> 
                                                     
                                                     <telerik:GridBoundColumn datafield="GPA" headertext="GPA" sortexpression="GPA" 
                                                        uniquename="GPA" AllowFiltering="false"
                                                         <HeaderStyle ForeColor="#192666" Font-Bold="True" /> 
                                                          <ItemStyle horizontalalign="Center" /> 
                                                    </telerik:GridBoundColumn> 
                                                     
                                                     <telerik:GridBoundColumn datafield="CGNAME" headertext="Status" sortexpression="CGNAME" 
                                                        uniquename="CGNAME"
                                                         <HeaderStyle ForeColor="#192666" Font-Bold="True" /> 
                                                    </telerik:GridBoundColumn> 
                                                     
                                                  
                                                    <telerik:GridTemplateColumn allowfiltering="False" headertext="Edit" uniquename="TemplateEditColumn"
                                            <ItemTemplate> 
                                                <asp:ImageButton id="imgEdit" runat="server" causesvalidation="False" commandname="edits" 
                                                    imageurl="~/images/edits.gif" tooltip="Edit" CssClass="curhand" /> 
                                            </ItemTemplate> 
                                            <HeaderStyle horizontalalign="Center" ForeColor="#192666" Font-Bold="True"  /> 
                                            <ItemStyle horizontalalign="Center" /> 
                                        </telerik:GridTemplateColumn> 
                                                    
                                                    <telerik:GridTemplateColumn allowfiltering="False" headertext="Delete" uniquename="TemplateDeleteColumn"
                                            <ItemTemplate> 
                                                <asp:ImageButton id="imgDelete" runat="server" causesvalidation="False" commandname="deletes" 
                                                    imageurl="~/images/deletes.gif" tooltip="Delete" CssClass="curhand"/> 
                                            </ItemTemplate> 
                                            <HeaderStyle horizontalalign="Center" ForeColor="#192666" Font-Bold="True"  /> 
                                            <ItemStyle horizontalalign="Center" /> 
                                        </telerik:GridTemplateColumn> 
                                                     
                                                    <telerik:GridTemplateColumn allowfiltering="False" headertext="View" uniquename="TemplateViewColumn"
                                            <ItemTemplate> 
                                                <asp:ImageButton id="imgView" runat="server" causesvalidation="False" commandname="view" 
                                                    imageurl="~/images/views.gif" tooltip="View" CssClass="curhand"  /> 
                                            </ItemTemplate> 
                                            <HeaderStyle horizontalalign="Center" ForeColor="#192666" Font-Bold="True"  /> 
                                            <ItemStyle horizontalalign="Center" /> 
                                        </telerik:GridTemplateColumn> 
                                                     
                                                     
                                            </Columns> 
                                            <ExpandCollapseColumn visible="False"
                                                <HeaderStyle width="19px" /> 
                                            </ExpandCollapseColumn> 
                                            <RowIndicatorColumn visible="False"
                                                <HeaderStyle width="20px" /> 
                                            </RowIndicatorColumn> 
                                        </telerik:GridTableView> 
                                    </DetailTables> 
                                     
                                    <Columns> 
                                         
                                        
                                        <telerik:GridTemplateColumn datafield="INSID" headertext="INSID" uniquename="INSID" 
                                            visible="False"
                                            <ItemTemplate> 
                                                <asp:Label id="lblINSID" runat="server" text='<%# Eval("INSID") %>'></asp:Label> 
                                            </ItemTemplate> 
                                        </telerik:GridTemplateColumn> 
                                        <telerik:GridBoundColumn datafield="InsName" headertext="Institution Name" sortexpression="InsName" 
                                            uniquename="InsName"  > 
                                             <HeaderStyle ForeColor="#192666" Font-Bold="True" /> 
                                        </telerik:GridBoundColumn> 
                                         <telerik:GridTemplateColumn AllowFiltering="false" HeaderText="Summary" UniqueName="ChartColumn"
                                        <ItemTemplate> 
                                        <asp:CHART id="Chart_CourseGrade" runat="server" BackColor="WhiteSmoke"  
                                            Height="200px" Width="300px" BorderDashStyle="Solid" BackSecondaryColor="White"  
                                            BackGradientStyle="TopBottom" BorderWidth="2px" BorderColor="#1A3B69"  
                                            ImageLocation="~/UserData/TempImageFiles/ChartPic_#SEQ(300,3)"
                                                    <titles> 
                                                        <asp:Title ShadowColor="32, 0, 0, 0" Font="Trebuchet MS, 9.25pt, style=Bold" ShadowOffset="3" Text="" Name="Title1" ForeColor="26, 59, 105"></asp:Title> 
                                                    </titles> 
                                                    <legends> 
                                                        <asp:Legend BackColor="Transparent" Alignment="Center" Docking="Top" Font="Trebuchet MS, 8.25pt" IsTextAutoFit="true" Name="Default" LegendStyle="Column"></asp:Legend> 
                                                    </legends> 
                                                    <borderskin SkinStyle="None"></borderskin> 
                                                    <series> 
                                                        <asp:Series Name="Default" ChartType="Pie" BorderColor="180, 26, 59, 105"  
                                                            Color="220, 65, 140, 240" ChartArea="ChartArea1" Legend="Default"></asp:Series> 
                                                    </series> 
                                                    <chartareas> 
                                                        <asp:ChartArea Name="ChartArea1" BorderColor="64, 64, 64, 64" BackSecondaryColor="Transparent" BackColor="Transparent" ShadowColor="Transparent" BorderWidth="0"
                                                            <area3dstyle Rotation="0" Enable3D="True" /> 
                                                            <axisy LineColor="64, 64, 64, 64"
                                                            <MajorGrid LineColor="64, 64, 64, 64" /> 
                                                            <LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" /> 
                                                            </axisy> 
                                                            <axisx LineColor="64, 64, 64, 64"
                                                                <MajorGrid LineColor="64, 64, 64, 64" /> 
                                                                <LabelStyle Font="Trebuchet MS, 8.25pt, style=Bold" /> 
                                                            </axisx> 
                                                        </asp:ChartArea> 
                                                    </chartareas> 
                                          </asp:CHART> 
                                        </ItemTemplate> 
                                        </telerik:GridTemplateColumn> 
                                        
 
                                    </Columns> 
                                    <ExpandCollapseColumn> 
                                        <HeaderStyle width="19px" /> 
                                    </ExpandCollapseColumn> 
                                    <RowIndicatorColumn visible="False"
                                        <HeaderStyle width="20px" /> 
                                    </RowIndicatorColumn> 
                                </MasterTableView> 
                                <PagerStyle alwaysvisible="True" /> 
                                <FilterMenu ></FilterMenu
                            </telerik:RadGrid > 
Now i'm geting the chart & its binding correctly. I have written the code to bind the chart in the item data bound. Bt a problem still exits, when I fetch the detail table details the error occurs ''Object reference not set to an instance of an object". Plz help.


0
Shinu
Top achievements
Rank 1
answered on 01 Jun 2009, 10:50 AM
Hi,

Set the Name property for the Master Table and try the following code snippet .

ASPX:
 
<MasterTableView DataKeyNames="INSID" AllowMultiColumnSorting="True" Name="Master" >  
 

CS:
 
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)  
{  
    if ((e.Item is GridDataItem) &&(e.Item.OwnerTableView.Name=="Master")) 
    {  
        RadChart chart = e.Item.FindControl("Chart_CourseGrade"as RadChart;  
        chart.ChartTitle.TextBlock.Text = "CHANGED!";  
    }  
}  


Shinu



0
kollam2003
Top achievements
Rank 1
answered on 01 Jun 2009, 11:47 AM
it works fine. I'm using it in itemdatabound.
But when i'm clicking the detail table the chart is disappearing.
How to avoid this.

If i'm binding the chart in the item created I'm nt able to get a value from a label in the item template.


0
Princy
Top achievements
Rank 1
answered on 01 Jun 2009, 03:20 PM
Hi,

I hope you are trying to set the title of the Chart from the label which is in another template column. I tried the same code on my end and the chart is not getting disappeared.  Here is the code which I tried.

ASPX:
 
 
 <telerik:GridTemplateColumn UniqueName="ChartColumn"  > 
             <HeaderStyle  Width="200px" /> 
              <ItemTemplate> 
                  <telerik:RadChart ID="Chart_CourseGrade" runat="server"
                  </telerik:RadChart> 
              </ItemTemplate> 
                
            </telerik:GridTemplateColumn> 
            <telerik:GridTemplateColumn UniqueName="TempCol" > 
             <ItemTemplate> 
                 <asp:Label ID="Label2" runat="server" Text='<%#Eval("ProductName") %>'></asp:Label> 
             </ItemTemplate> 
            </telerik:GridTemplateColumn> 
 

CS:
 
 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if ((e.Item is GridDataItem) && (e.Item.OwnerTableView.Name == "Master")) 
        { 
            GridDataItem item = (GridDataItem)e.Item; 
            RadChart chart = (RadChart)item.FindControl("Chart_CourseGrade"); 
            Label lbl = (Label)item.FindControl("Label2"); 
            chart.ChartTitle.TextBlock.Text = lbl.Text; 
        } 
   } 


Princy
0
kollam2003
Top achievements
Rank 1
answered on 02 Jun 2009, 07:08 AM
I'm using asp chart. When I'm clicking on detail table view the chart is vanishing.

Item Data Bound code:
if ((e.Item is GridDataItem) && (e.Item.OwnerTableView.Name == "Master")) 
        { 
            int _lblINSID = 0
          
            Label lblINSID = (Label)e.Item.FindControl("lblINSID"); 
            _lblINSID = Convert.ToInt32(lblINSID.Text.ToString()); 
            GridDataItem item = (GridDataItem)e.Item; 
            Chart Chart_CourseGrade = (Chart)item["ChartColumn"].FindControl("Chart_CourseGrade"); 
            int _StudentID = 0
            if (hfStudentID.Value != "") 
            { 
                _StudentID = Convert.ToInt32(hfStudentID.Value); 
            } 
            DataTable dtDisplay = new DataTable(); 
 
            dtDisplay = objClassroom.Classroom_ManageCourseGrade_StudentCollegeCourse_InsID(_StudentID, _lblINSID); 
            if (dtDisplay.Rows.Count > 0) 
            { 
                Chart_CourseGrade.DataSource = dtDisplay
                Chart_CourseGrade.Series.Clear(); 
                Chart_CourseGrade.Series.Add("Series1"); 
                Chart_CourseGrade.Series["Series1"].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column; 
                // Set series members names for the X and Y values 
                Chart_CourseGrade.Series["Series1"].XValueMember = "Name"
                Chart_CourseGrade.Series["Series1"].YValueMembers = "GPA"
                Chart_CourseGrade.DataBind(); 
            } 
            
        } 

Tags
Grid
Asked by
kollam2003
Top achievements
Rank 1
Answers by
Daniel
Telerik team
kollam2003
Top achievements
Rank 1
Shinu
Top achievements
Rank 1
Princy
Top achievements
Rank 1
Share this question
or