Tuesday, September 24, 2013

Find Header & Footer template from Repeater control in .Net



This article will explain you how to find the Repeater Header and Footer template details.

I had created one user control which contain the Repeater. It generate the product showcase. i used that control in multiple pages but some where i need to update few details like header title , footer text at run time and that i have achieved it through find control.
Below example can helps you to understand the clear idea.


<asp:Repeater ID="ProductsDetails" runat="server">  
<HeaderTemplate> 
     <asp:Label ID="lblProductDetails" runat="server" Fore-Color= "Red"  Font-Bold = "true" />
 <br />  
</HeaderTemplate>  
<ItemTemplate>      
 Product Name : <%#Eval("Name") %> <br />  
</ItemTemplate>  
<FooterTemplate>      
 <asp:Label ID="lblPriceDetails" runat="server" Fore-Color= "Green" Font-Bold = "true" />  
</FooterTemplate>
</asp:Repeater>


Here i have define repeater control along with header & footer details , now i want to change the text of label called "lblProductDetails" at run-time. below snippets will helps you to get the Repeater details and base on that you can easily find the footer  & header controls  and change the text.


//Find HeaderTemplate
   Control TempHeaderDet = ProductsDetails.Controls[0].Controls[0];      
   Label lblHeaderDet = TempHeaderDet.FindControl("lblProductDetails") as Label;      
   lblHeaderDet.Text = "Product Description :";

//Find FooterTemplate

   Control FooterTemplateDet = ProductsDetails.Controls[ProductsDetails.Controls.Count - 1].Controls[0];            
   Label lblFooterDet = FooterTemplateDet.FindControl("lblPriceDetails") as Label;      
   lblFooter.Text = "Product Price:";



Yeap, i know its not very big deal... but yes some time this trick will helps you and save time as well. 

put your comments/ question in case of any concerns.

 


2 comments:

  1. Good Article!!!

    one question.
    Can we change the content of repeater control at runtime base on webpage call?

    Thanks in advance.
    Mike

    ReplyDelete
  2. Hi Mike,

    Thanks to write, Yes you can change the content at runtime as well you need to write your logic in onRowCreated function

    ReplyDelete