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

Changing header text

7 Answers 254 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David Penny
Top achievements
Rank 2
David Penny asked on 26 Feb 2013, 03:51 PM
Hi,

I'm posting this under grid, but it probably applies to other controls as well.

I'm working on a routine which changes labels on ASP.Net pages to configure terminology for various customers. In order to do this I iterate through all controls on screen recursively and change the label text if needed.

I was wondering is it possible to do the same with the elements of a RadGrid such as the header text?

The core of the replacement routine at present is:

Public Sub ReplaceText(ByRef myControls As ControlCollection)
        Dim ReplacementText As String = HttpContext.Current.Session("ReplacementText")
        If String.IsNullOrEmpty(ReplacementText) Then Exit Sub
        Dim ReplacementTextUpper As String = ReplacementText.ToUpper
        For Each myControl As Control In myControls
            If TypeOf myControl Is Label Then
                Dim lbl As Label = CType(myControl, Label)
                Dim labelText As String = String.Format("[{0}", lbl.Text.Trim.ToUpper)
                Dim textPosition As Integer = InStr(ReplacementTextUpper, labelText)
                If labelText <> "[" And textPosition > 0 Then
                    'Parse ReplacementText for = and > sign and get string between
                    Dim Pos1 = ReplacementText.IndexOf("=", textPosition)
                    Dim Pos2 = ReplacementText.IndexOf("]", Pos1)
                    Dim ReplaceWith As String = ReplacementText.Substring(Pos1 + 1, (Pos2 - Pos1) - 1)
                    lbl.Text = ReplaceWith
                End If
            End If
            If myControl.HasControls Then
                ReplaceText(myControl.Controls)
            End If
        Next
    End Sub

I have tried accessing a RadGrid as a control, but it is defined as a Telerik.Web.UI control. Would that mean I need another routine that passes the Rad Controls for parsing? Is what I am trying to do even possible?

David Penny

7 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 27 Feb 2013, 05:11 AM
Hi,

Try looping through the headeritems in ItemDataBound event as shown below.
VB:
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    For Each item As GridHeaderItem In RadGrid1.MasterTableView.GetItems(GridItemType.Header)
        item("Uniquename").Text = "some text"
    Next
End Sub

Thanks,
Shinu
0
David Penny
Top achievements
Rank 2
answered on 27 Feb 2013, 12:53 PM
Hi Shinu,

How do I find the text of each item? Until I iterate thnrough the items in the Grid header I don't know if the text needs replacing.

I need to get the text property of each header column without knowing the name of it. Can I do this with a numeric index such as item(0).text, and if so, I don't seem to be able to find how to get the count of items in the header.

David Penny
0
Shinu
Top achievements
Rank 2
answered on 28 Feb 2013, 06:32 AM
Hi,

Try the following code.
VB:
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    For Each col As GridColumn In RadGrid1.Columns
        If TypeOf e.Item Is GridHeaderItem Then
            Dim header As GridHeaderItem = DirectCast(e.Item, GridHeaderItem)
            Dim text As String = header(col.UniqueName).Text
        End If
    Next
End Sub

Thanks,
Shinu
0
David Penny
Top achievements
Rank 2
answered on 28 Feb 2013, 09:09 AM
Hi Shinu,

That kind of words, except the text property is returning nothing - except for the first column which returns &nbsp.

In case it's something to do with my grid definition it's below:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" GridLines="None"
            AllowFilteringByColumn="True" AllowSorting="True" ShowGroupPanel="True"
            ShowFooter="True">
            <ClientSettings>
                <Scrolling AllowScroll="True" UseStaticHeaders="True" />
            </ClientSettings>
            <HeaderContextMenu>
                <CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
            </HeaderContextMenu>
            <MasterTableView DataKeyNames="UniqueID" CommandItemDisplay="Top">
                <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
                <RowIndicatorColumn>
                    <HeaderStyle Width="20px" />
                </RowIndicatorColumn>
                <ExpandCollapseColumn>
                    <HeaderStyle Width="20px" />
                </ExpandCollapseColumn>
                <Columns>
                    <telerik:GridTemplateColumn UniqueName="TemplateEditColumn" AllowFiltering="False">
                        <ItemTemplate>
                            <asp:HyperLink ID="EditLink" runat="server" Text="Edit"></asp:HyperLink>
                        </ItemTemplate>
                        <FooterStyle Width="32px" />
                        <HeaderStyle Width="32px" />
                        <ItemStyle Width="32px" />
                    </telerik:GridTemplateColumn>
                    <telerik:GridBoundColumn DataField="Project" HeaderText="Project" SortExpression="Project"
                        UniqueName="Project">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Name" HeaderText="Name" SortExpression="Name"
                        UniqueName="Name">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Client" HeaderText="Client" SortExpression="Client"
                        UniqueName="Client">
                    </telerik:GridBoundColumn>
... etc
0
Kostadin
Telerik team
answered on 01 Mar 2013, 11:18 AM
Hello David,

When sorting is enabled the HeaderItem contains a LinkButton and its text should be changed. Check out the following code snippet.
protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        GridHeaderItem headerItem = RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0] as GridHeaderItem;
        foreach (GridColumn col in RadGrid1.MasterTableView.Columns)
        {
            if (headerItem[col.UniqueName].Controls[0] is LinkButton)
            {
                (headerItem[col.UniqueName].Controls[0] as LinkButton).Text = (headerItem[col.UniqueName].Controls[0] as LinkButton).Text + " Your text";
            }
        }
    }


Kind regards,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
David Penny
Top achievements
Rank 2
answered on 04 Mar 2013, 02:41 PM
Thanks Kostadin,

That solution works great.

As a matter of interest, what type would I used to check if the header is an ordinary item (not sortable?). This would allow me to create a generic routine that works for both sortable and non-sortable columns.

Thanks.

David Penny
0
Kostadin
Telerik team
answered on 07 Mar 2013, 01:03 PM
Hi David,

When sorting is disabled you have to change the cell text only as Shinu suggested. You could check whether the header item contains a control in it.

I hope this information helps.

All the best,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
David Penny
Top achievements
Rank 2
Answers by
Shinu
Top achievements
Rank 2
David Penny
Top achievements
Rank 2
Kostadin
Telerik team
Share this question
or