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

How to access and change the value of a label in a gridtemplatecolumn

2 Answers 727 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Denise Duggan
Top achievements
Rank 1
Denise Duggan asked on 09 Dec 2009, 10:45 PM
I have a gridtemplate column, like so:

<telerik:GridTemplateColumn UniqueName="colClockInToShow" HeaderStyle-Width="45px"
    <HeaderTemplate> 
        <asp:Label ID="lblClockInHeader">Clock In</asp:Label> 
    </HeaderTemplate> 
    <ItemTemplate> 
        <asp:Label ID="lblClockInData" Text="temp" /> 
    </ItemTemplate> 
</telerik:GridTemplateColumn> 
 

During the course of my radGridTimeCard_ItemDataBound sub, I have logic that will select the proper value to be displayed in the label (which is now displaying "temp").  But I just can't seem to figure out how to access that label's text property programatically. 

Help??

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 10 Dec 2009, 05:12 AM
Hello Denise,

You can try out the following code to access a label in the GridTemplateColumn:
c#:
 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem dataItem = (GridDataItem)e.Item; 
            Label lbl = (Label)dataItem.FindControl("lblClockInData"); 
            lbl.Text = "My Custom Text"
        } 
    } 

Thanks
Princy.
0
Denise Duggan
Top achievements
Rank 1
answered on 10 Dec 2009, 01:22 PM
Thanks for the help.  I tried that and it didn't work.  I converted it to VB as that's what I'm using and came up with the following (and if I mis-translated it please let me know).

If (TypeOf (e.Item) Is GridDataItem) Then 
    Dim dataItem As GridDataItem = e.Item  
    Dim lbl As New Label  
    Try 
        ' Clock In Data  
        If Not (DirectCast(e.Item.DataItem, DataRowView)(8).ToString = ""Then 
            lbl = dataItem.FindControl("lblClockInData")  
            lbl.Text = "My Custom Text" 
        End If 
    Catch ex As Exception  
        Throw New ApplicationException("Problem correctly formating date(s) and/or time(s) in grid", ex)  
    End Try 
End If 

I stepped through to find that when I try to find the control and assign that to the lbl variable, it returns Nothing.  So then when I try to assign the text of that control (lbl.Text) the app throws an error saying that the "Object reference not set to an instance of an object."  I don't know why it doesn't find that control?  I think if I could get that solved I'd be on my way.
Tags
Grid
Asked by
Denise Duggan
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Denise Duggan
Top achievements
Rank 1
Share this question
or