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

Loop Header Columns and update dynamic text in Header Template

1 Answer 166 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mohan
Top achievements
Rank 1
Mohan asked on 02 Jul 2012, 04:56 PM
Team,

I need to update Header Template controls data from code behind during Item_Databound. Suggest possible solution.

ASPX
-------

                                                    <telerik:GridTemplateColumn UniqueName="Leg" HeaderText="Leg1">
                                                        <HeaderTemplate>
                                                            <asp:HiddenField ID="hdnLeg1" runat="server" />
                                                            <asp:Label ID="lbLeg1" Text="" runat="server" />
                                                        </HeaderTemplate>
                                                        <ItemTemplate>
                                                            <asp:DropDownList ID="DropDownList1" runat="server">
                                                            </asp:DropDownList>
                                                        </ItemTemplate>
                                                    </telerik:GridTemplateColumn>
                                                    <telerik:GridTemplateColumn UniqueName="Leg" HeaderText="Leg1">
                                                        <HeaderTemplate>
                                                            <asp:HiddenField ID="hdnLeg2" runat="server" />
                                                            <asp:Label ID="lbLeg2" Text="" runat="server" />
                                                        </HeaderTemplate>
                                                        <ItemTemplate>
                                                            <asp:DropDownList ID="DropDownList2" runat="server">
                                                            </asp:DropDownList>
                                                        </ItemTemplate>
                                                    </telerik:GridTemplateColumn>
.............

ASPX.CS

// Code must update each Hidden variables with it's ID and Label Text for Grid Column Header
 if (e.Item is GridHeaderItem)
                {
                    int PreviousLegIDIndex = 0;
                    int counter = 0;
                    foreach (GridHeaderItem headerItem in dgPassanger.MasterTableView.GetItems(GridItemType.Header))
                    {
                        counter = 0;
                        foreach (var Leg in Log.Legs)
                        {
                            counter++;
                            if (counter > PreviousLegIDIndex)
                            {
                                foreach (var headerControl in headerItem["Leg"].Controls)
                                {
                                    PreviousLegIDIndex = counter;
                                    if (headerControl.GetType() == typeof(Label))
                                    {
                                        Label lbHeader = (Label)headerControl;
                                        lbHeader.Text = tripLeg.POLegsDescription;
                                    }
                                    if (headerControl.GetType() == typeof(HiddenField))
                                    {
                                        HiddenField hdnHeader = (HiddenField)headerControl;
                                        hdnHeader.Value = tripLeg.LegID.ToString();
                                    }
                                }
                                break;
                            }
                        }
                    }
                }

1 Answer, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 02 Jul 2012, 05:22 PM
Hello Mohan,

protected void dgPassanger_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridHeaderItem)
            {
                GridHeaderItem item = e.Item as GridHeaderItem;
                Label lbLeg1 = item.FindControl("lbLeg1") as Label;
                lbLeg1.Text = "your text come here";
            }
        }
<telerik:GridTemplateColumn UniqueName="Leg" HeaderText="Leg1">
    <HeaderTemplate>
        <asp:HiddenField ID="hdnLeg1" runat="server" />
        <asp:Label ID="lbLeg1" runat="server" />
    </HeaderTemplate>
</telerik:GridTemplateColumn>


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Mohan
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Share this question
or