views:

1095

answers:

3

I need to get a value from a textbox inside a FooterTemplate in the OnClick event of a button. My first thought was to loop through the items-property on my repeater, but as you can see in this sample, it only includes the actual databound items, not the footer-item.

ASPX:

<asp:Repeater ID="Repeater1" runat="server">
 <ItemTemplate>
  Item<br />
 </ItemTemplate>
 <FooterTemplate>
  Footer<br />
  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
 </FooterTemplate>
</asp:Repeater>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

Code-behind.cs:

protected void Page_Load(object sender, EventArgs e)
{
 ListItemCollection items = new ListItemCollection();
 items.Add("value1");
 items.Add("value2");
 Repeater1.DataSource = items;
 Repeater1.DataBind();
}

protected void Button1_Click(object sender, EventArgs e)
{
 System.Diagnostics.Debug.WriteLine(Repeater1.Items.Count);
}

This code will only output "2" as the count, so how do I get to reference my textbox inside the footertemplate?

A: 

From the MSDN documentation, the Items is simply a set of RepeaterItems based off the DataSource that you are binding to and does not include items in the Header or FooterTemplates.

If you want to reference the textbox, you can get a reference on ItemDataBound event from the repeater where you can test for the footer.

E.g.

private void Repeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e) 
{

  if (e.Item.ItemType == ListItemType.Footer) 
  {
    TextBox textBox = e.Item.FindControl("TextBox1") as TextBox;
  }
}
Ray Booysen
The Repeater_ItemDataBound happens before the user has entered any text. How can I get that text in the OnClick event after the user has entered the text?
Espo
Hi EspoYou would need to write a recursive method to dig down into Repeater1.Controls collection checking for a textbox named TextBox1. You could make it quicker by only looking in the footer first.
Ray Booysen
Ray: That is the solution i'm using now, but it seems "dirty" to create a recursive function just to get a value from a textbox. I was hoping there would be a cleaner way to do it.
Espo
+1  A: 

You can find controls in the repeater. That will give you all the controls in the repeater(RepeaterItems collection). Now you can do something like this.

RepeaterItem footerItem=null; foreach(Control cnt in Repeater1.Controls) { if(cnt.GetType() == typeof(RepeaterItem) && ((RepeaterItem)cnt).ItemType == ListItemType.Footer) { footerItem = cnt; break; } }

Hope That Helps, Regards Tajinder Sarao

A: 

The footer should be the last child control of the repeater so you can do something like..

RepeaterItem riFooter = Repeater1.Controls[Repeater1.Controls.Count - 1] as RepeaterItem;
if (riFooter != null && riFooter.ItemType == ListItemType.Footer) {
    TextBox TextBox1 = riFooter.FindControl("TextBox1") as TextBox;
    if (TextBox1 != null) {
        TextBox1.Text = "Test";
    }
}
leroy