repeater.TemplateControl.FindControl("pnlOuterPanel") - Gets me the Panel
(why the repeater gives me access to the panel control when it is its parent I don't know?!?)
I gave the entire sample page so you can see how I got it to work no tricks! But I was not sure if you had something else going on in your page so I am not sure if this is enough.
Partial Class linkbutinHdr
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Repeater1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles Repeater1.ItemCommand
If e.CommandName = "Product"Then' here you have access to the button again
End If
End Sub
Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
If e.Item.ItemType = ListItemType.Header Then'this is one way
Dim lnk As LinkButton = CType(e.Item.FindControl("lnkProduct"), LinkButton)
End If
End Sub
End Class
You cannot do a FindControl on a template control. FindControl gives you access to nested controls(child controls). In your case, the link button is a template control of your repeater & not a nested control. So it can only be accessed through repeater's
events.
To prove, you can do a quickwatch on Repeater.Controls.Count, the value will be 0.
This is by design & makes sense. Say, if you want to do FindControl on a list control like datagrid,repeater etc then you need to do Repeater.Controls.Add(childControl). Then you can do FindControl for this childControl.
Once you do this, this childControl will be rendered within the div tag in the HTML for this repeater. You can try this out at your end.
Again, template controls are more like controls that you operate on the grid,repeater etc & they are not nested controls for which you can use FindControl.
Hi, i've just been struggling with this one myself- and i found i needed to use a different method to reference my controls- as noted earlier you cant simply do a FindControl against the repeater item which got sent through with the sender, however you can
if you run the find control against the e.Item which is passed over;
Protected Sub rptAddedSeries_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptAddedSeries.ItemDataBound
If Not e.Item.ItemType = ListItemType.Header AndAlso Not e.Item.ItemType = ListItemType.Footer Then
'Dim this_repeater_item As RepeaterItem = DirectCast(sender, Repeater).Controls.Item(_repeater_counter)
'Dim child_repeater As Repeater = DirectCast(this_repeater_item.FindControl("rptBooks"), Repeater)
'Dim child_repeater As Repeater = DirectCast(this_repeater_item.TemplateControl.FindControl("rptBooks"), Repeater)
Dim child_repeater As Repeater = DirectCast(e.Item.FindControl("rptBooks"), Repeater)
Interestingly, i only had to do this when i had added a headertemplate and footertemplate to my repeater- before i added those elements it ran fine referencing the this_repeater_item directly.
swaino
Member
207 Points
589 Posts
Using FindControl with control nested inside Header Template of Repeater
May 02, 2008 09:48 AM|LINK
Hi,
Again I run into problems with FindControl:(
I have a link button inside the header template of a repeater control.
I want to be able to enable/disable it without doing this in any of the Item_Created/Item_DataBound events of the repeater.
My repeater is inside a panel control.
e.g.
<asp:panel>
<asp:Repeater>....
<HeaderTemplate>....</HeaderTemplate>
etc...
</asp:Repeater>
</asp:panel>
My question is, how do I get to the link button inside the repeater, more specifically inside the header template.
repeater.TemplateControl.FindControl("lnkButton") - Nothing
repeater.TemplateControl.FindControl("pnlOuterPanel") - Gets me the Panel (why the repeater gives me access to the panel control when it is its parent I don't know?!?)
repeater.FindControl("lnkButton") - Nothing
Bizarre...
aamador
Contributor
5211 Points
1107 Posts
Re: Using FindControl with control nested inside Header Template of Repeater
May 02, 2008 10:33 AM|LINK
I gave the entire sample page so you can see how I got it to work no tricks! But I was not sure if you had something else going on in your page so I am not sure if this is enough.
Let me know
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="linkbutinHdr.aspx.vb" Inherits="linkbutinHdr" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"> <HeaderTemplate> <table border="1"> <tr> <td>Product ID</td> <td>Product Name</td> <td>Product Link:<asp:LinkButton ID="lnkProduct" runat="server" CommandName="Product">Product</asp:LinkButton> </td> </tr> </HeaderTemplate> <ItemTemplate > <tr> <td><%# Eval("ProductID") %></td> <td><%# Eval("ProductName") %></td> <td> </td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT [ProductID], [ProductName], [SupplierID] FROM [Products]"> </asp:SqlDataSource> </div> </form> </body> </html>Abhishek khanna
Participant
1255 Points
217 Posts
Re: Using FindControl with control nested inside Header Template of Repeater
May 02, 2008 10:56 AM|LINK
Hi
You cannot do a FindControl on a template control. FindControl gives you access to nested controls(child controls). In your case, the link button is a template control of your repeater & not a nested control. So it can only be accessed through repeater's events.
To prove, you can do a quickwatch on Repeater.Controls.Count, the value will be 0.
Hope this helps.
Thanks
Abhi
swaino
Member
207 Points
589 Posts
Re: Using FindControl with control nested inside Header Template of Repeater
May 02, 2008 01:07 PM|LINK
Ok, is this by design or a flaw on Microsoft's part?
Abhishek khanna
Participant
1255 Points
217 Posts
Re: Using FindControl with control nested inside Header Template of Repeater
May 02, 2008 02:16 PM|LINK
This is by design & makes sense. Say, if you want to do FindControl on a list control like datagrid,repeater etc then you need to do Repeater.Controls.Add(childControl). Then you can do FindControl for this childControl.
Once you do this, this childControl will be rendered within the div tag in the HTML for this repeater. You can try this out at your end.
Again, template controls are more like controls that you operate on the grid,repeater etc & they are not nested controls for which you can use FindControl.
pramodchavan
Member
2 Points
3 Posts
Re: Using FindControl with control nested inside Header Template of Repeater
Oct 11, 2008 11:51 AM|LINK
I tried all the methods but couldn't read asp button inside header at the same time i'm able to read checkbox control placed inside item template.
please suggest how to read control from header?
<
asp:Repeater ID="rptShareType" Runat="server" OnItemDataBound="rptShareType_ItemDataBound" OnItemCommand="rptShareType_ItemCommand" OnItemCreated="rptShareType_ItemCreated" > <ItemTemplate> <ewUI:AccordionPanel runat="server" ExpandImageUrl="../Images/Expand.gif" ExpandText="asd" id="Accordionpanel2" AllowSliding="false" SlideSpeed=2 ShowLinkOrImage="False" TitleText='<%#DataBinder.Eval(Container.DataItem,"SHAR_TYPE_NAME")%>' AllowTitleRowExpandCollapse="True" AllowTitleExpandCollapse="True" TitleStyleContainerMode="TitleOnly" TitleStyle-CssClass="PanelExpand" CollapsedTitleStyle-CssClass="PanelCollapse" GroupName="Demo1" > <asp:DataList ID="dlComparGrps" Runat="server" CellSpacing=0 RepeatDirection="Vertical" DataSource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("StatusRelation") %>' Width="500"> <HeaderTemplate> <table border=1px rules=all bordercolor="#999966" width=500 cellpadding=0 cellspacing=0 > <tr><td class=GridHeader align="center" >Edit</td><td class=GridHeader align="center">Comparison Groups</td> <td class=GridHeader align="center" >Description</td><td class="GridHeader" align="center"> <asp:Button ID="btnMoveGrp" Runat=server Text = "Copy Group(s) To Next Year" CssClass="Button" OnClick="btnMoveGrp_Click" Width="197px" Visible="true"></asp:Button></td></tr> </HeaderTemplate> <ItemTemplate> <TR align=left >
<td width=30 height=50 > <!--<asp:Label ID="lblCompar_Grp" runat="server" Text='<%# ((System.Data.DataRow)Container.DataItem)["Compar_Grp_Id"] %>'Visible="false"></asp:Label>
<asp:Label ID="lblCompar_GrpName" runat="server" Text='<%# ((System.Data.DataRow)Container.DataItem)["Compar_Grp_Name"] %>'
Visible="false"></asp:Label>-->
<%#ChkGroupOwner(Convert.ToInt32(((System.Data.DataRow)Container.DataItem)["Compar_Grp_Id"]),Convert.ToBoolean(((System.Data.DataRow)Container.DataItem)["UserGroup"]))%> </td> <td width=200 > <%# ((System.Data.DataRow)Container.DataItem)["Compar_Grp_Name"] %>
<%#(ChkPermission(Convert.ToInt32(((System.Data.DataRow)Container.DataItem)["Compar_Grp_Id"]), Convert.ToBoolean(((System.Data.DataRow)Container.DataItem)["UserGroup"])) ) %>
</td> <td width=170><%
# ((System.Data.DataRow)Container.DataItem)["Compar_Grp_Desc"] %>
</td > <td align=center >
<asp:CheckBox ID="chkSelect" runat="server" Visible="false" />
</td> </TR> </ItemTemplate> <FooterTemplate> <asp:Button ID="Button1" runat="server" Text="Button" /> </table> </FooterTemplate> </asp:DataList>
</ewUI:AccordionPanel> </ItemTemplate> </asp:Repeater>shawson
Member
2 Points
1 Post
Re: Using FindControl with control nested inside Header Template of Repeater
Oct 27, 2008 11:03 AM|LINK
Hi, i've just been struggling with this one myself- and i found i needed to use a different method to reference my controls- as noted earlier you cant simply do a FindControl against the repeater item which got sent through with the sender, however you can if you run the find control against the e.Item which is passed over;
Interestingly, i only had to do this when i had added a headertemplate and footertemplate to my repeater- before i added those elements it ran fine referencing the this_repeater_item directly.
Hope this helps!
Shaw.