views:

495

answers:

1

Hello,

I have a repeater like this:

<asp:Repeater runat="server" ID="pde">
<HeaderTemplate></HeaderTemplate>

<ItemTemplate>
<asp:Literal runat="server" ID="literal1"></asp:Literal>
</ItemTemplate>

<FooterTemplate><asp:Literal runat="server" ID="literal2"></asp:Literal></FooterTemplate>
</asp:Repeater>

Now in literal2 I want to get the value from code behind.But I am not able to get the literal2 in code behind.Any idea how to get this?

A: 

You should be able to access it by accessing the last RepeaterItem in your repeater which will be your footer. Then do a search, using FindControl, on the RepeaterItem for any control you are looking for.

Using your example above do:

Literal controlYouWant = pde.Controls[pde.Controls.Count - 1].FindControl("literal2") as Literal;

You can break down the statements to:

// This will get you the footer
RepeaterItem item = pde.Controls[pde.Controls.Count - 1];
// From here you finds any control you want within the RepeaterItem.
Literal controlYouWant = item.FindControl("literal2") as Literal;
Kelsey
above statement pde.Controls[pede.Controls.Count-1].FindControl...is throwing an exception...I wonder because in my footertemplate I have like this<FooterTemplate></li><asp:Literal runat="server" ID="literal2"/></div></FooterTemplate>but i tried pde.Controls.Count-2...its also not working exception
TSSS22
I don't think that the other stuff in your template should matter. I tested it with the same HTML that you just posted with the `</li>` and `</div>` and it still works fine. Make sure you are binding to a `Datasource` with at least one item. Break each item into a seperate line of code and debug from there to see exactly what is failing and post back.
Kelsey
Yah I got it,thanks a lot....mistake was i was doing this before binding data item into repeater so thats why there were no controls at that time and it was showing error.I need to use it after I do DataBind in repeater ....Wokrs perfectly...Thanks...
TSSS22