Access controls in Grid coloumn Header

5 posts, 0 answers
  1. Duy
    Duy  avatar
    69 posts
    Member since:
    Apr 2009

    Posted 07 Dec 2009 Link to this post


    Hi,

    Please advise how I can find controls by iusing ID string  which the controls locates in grid column header.

    Regards,

    Duy
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 07 Dec 2009 Link to this post

    Hello Duy,

    Try the following code snippet in order to access the control placed in HeaderTemplate.

    ASPX:
     
     <telerik:GridTemplateColumn UniqueName="Temp"
            <ItemTemplate> 
            </ItemTemplate> 
            <HeaderTemplate> 
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 
            </HeaderTemplate> 
        </telerik:GridTemplateColumn> 

    CS:
     
        GridHeaderItem headerItem = (GridHeaderItem)RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0]; 
        Label lbl = (Label)headerItem.FindControl("Label1"); 
        lbl.Text = "New Text"

    You could use the following code if you want to access the control in ItemDataBound event.
    CS:
     
        protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
        { 
            if (e.Item is GridHeaderItem) 
            { 
                GridHeaderItem headerItem = (GridHeaderItem)e.Item; 
                Label lbl = (Label)headerItem.FindControl("Label1"); 
                lbl.Text = "My Text"
            } 
        } 

    -Shinu.
  3. Duy
    Duy  avatar
    69 posts
    Member since:
    Apr 2009

    Posted 08 Dec 2009 Link to this post

    Thanks Shinu, I got it.

    Cheers,

    Duy
  4. Jinson
    Jinson avatar
    1 posts
    Member since:
    Oct 2019

    Posted 19 Feb in reply to Shinu Link to this post

    Hi,

    Could you please share the vb.net code for accessing the header template in rowdatabound?

  5. Attila Antal
    Admin
    Attila Antal avatar
    575 posts

    Posted 24 Feb Link to this post

    Hi Jinson,

    Here is the VB version of the code posted by Shinu.

    VB

    Dim headerItem As GridHeaderItem = CType(RadGrid1.MasterTableView.GetItems(GridItemType.Header)(0), GridHeaderItem)
    Dim lbl As Label = CType(headerItem.FindControl("Label1"), Label)
    lbl.Text = "New Text"

     

    VB

    Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs)
        If TypeOf e.Item Is GridHeaderItem Then
            Dim headerItem As GridHeaderItem = CType(e.Item, GridHeaderItem)
            Dim lbl As Label = CType(headerItem.FindControl("Label1"), Label)
            lbl.Text = "My Text"
        End If
    End Sub

     

    I would also recommend the Telerik Code Converted we have which is an online tool for converting C# to VB or vice versa, see http://converter.telerik.com/. This could come handy in the future if you're finding examples only in C#.

     

    Kind regards,
    Attila Antal
    Progress Telerik

    Get quickly onboarded and successful with UI for ASP.NET AJAX with the Virtual Classroom technical trainings, available to all active customers. Learn More.
Back to Top