views:

288

answers:

1
<asp:Repeater ID="Repeater1" runat="server">

   <ItemTemplate>
      <li class="closed" >
           <asp:HyperLink runat="server" CssClass="toggler off" 
                ImageUrl="/_layouts/images/NEXT.GIF" 
                Text="<%#Container.DataItem%>" ID="HyperLink1">
           </asp:HyperLink>
      </li>
   </ItemTemplate>

 </asp:Repeater>

I want to get the text in the hyperlink from the arraylist

in my ascx code

I am trying to do this bt its showing error

 HyperLink hypl = (HyperLink)Repeater1.FindControl("HyperLink1");
 hypl.Text = ar.ToString();
 hypl.NavigateUrl = "http//www.yahoo.com";

Anyone is having idea how to resolve this problem

+2  A: 

With a repeater control, you can't use FindControl to locate the hyperlink by name because there can be more than one (this is a template, and it gets rendered 0 to n times).

You need to do the assignment of values to the hyperlink multiple times, once for each item that is bound. This is a job for the repeater's ItemDataBound event. Try something like this:

<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound">
    <ItemTemplate>
      <li class="closed" >
       <asp:HyperLink runat="server" CssClass="toggler off" 
            ImageUrl="/_layouts/images/NEXT.GIF" 
            Text="<%#Container.DataItem%>" ID="HyperLink1">
       </asp:HyperLink>
      </li>
    </ItemTemplate>
</asp:Repeater>

Then you need to handle the event like so:

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    HyperLink hypl = (HyperLink)e.Item.FindControl("HyperLink1");
    hypl.Text = ar.ToString();
    hypl.NavigateUrl = "http//www.yahoo.com";
}
Stephen M. Redd
excellent answer.
Devtron
Thanks Stephen for the reply actually in my case I am getting most of the values from strings or arraylist not directly from column in datasource so I want to do all my thing in one function because otherwise other array or string will be out of scope.Current i m doing in page_load()and in that I m using foeach(repateritem item in repeater1.items){}
TSSS22
Just store the array in a private field or property on your page's class. That way it will stay in scope while the binder events fire.You aren't likely to find a way to do that all during page load using the built-in repeater's mechanisms easily. You could wait until after the whole repeater binds, then walk the repeater's control tree all at once, but that's a LOT of unnecessary work for this case.
Stephen M. Redd