views:

312

answers:

3

Hi all..

Seems like I've ran into a wall here. I want some datasource to be bound to an asp.net repeater (well, doesn't have to be a repeater, but it seems like that's what I want). Now, here's the catch: I also need some server-controls inside that repeater for updating the data (TextBox'es and buttons).

To my understanding, this aint really possible to do in an orderly manner? I can't just add a textbox to the itemtemplate, and then fetch it later on in the code-behind it seems. At least not easily. Are there any known techniques for this kind of problem?

I can't use a gridview, since the data needs to be formatted in a certain way.

+1  A: 

You can use the Repeater.ItemDataBound Event to locate controls nested in a Repeater.

<asp:Repeater ID="Repeater1" Runat="server" OnItemDataBound="Repeater1_ItemDataBound">
   <ItemTemplate>
      <div><asp:TextBox ID="TextBox1" runat="server" /></div>
   </ItemTemplate>
</asp:Repeater>

Then in code behind:

protected void Repeater1_ItemDataBound(object source, RepeaterCommandEventArgs e)
{
   if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType !=
      ListItemType.AlternatingItem)
      return;

   TextBox textBox1 = e.Item.FindControl("TextBox1") as TextBox;
   if (textBox1 != null)
   {
   // do something with it
   }
}
Dan Diplo
Tried that already. Didn't work :( Seems that the ItemDataBound-event happens before the button is rendered and added? Anyway, textBox1 is null for me. BTW; the event-args type is RepeaterItemEventArgs :)
cwap
Instead of using ItemDataBound, I believe you would want to add a button and use the RepeaterItemCommand event handler.
David Stratton
A: 

Seems like my problem was in the way I was thinking :)

My solution: I just added controls as I normally would do, but inside the ItemTemplate. On callback events of the controls, I'd go for:

(Button example)

protected void btnUpdate_OnClick(object sender, EventArgs e)
    {
        Button b = sender as Button;
        if (b != null)
        {
            RepeaterItem ri = b.Parent as RepeaterItem;
            if (ri != null)
            {
                string name = null;

                //Fetch data
                TextBox txtName = ri.FindControl("txtName") as TextBox;

.. etc..

So, after finding the RepeaterItem i just treat it as I would with any ControlGroup. Doesn't matter that I actually got 5 different textbosses, coded with ID="txtName", since asp.net automagically gives the controls "obfuscated" names in the client markup, and translates this back to my ID's on postback.

Hope this helps someone, and sorry for bothering :)

cwap
+1  A: 

If you don't want to use an ItemCommand and want to just loop through the Repeater's items collection, so you have one "save" button at the bottom of the page, you can do it like this:

foreach(RepeaterItem itm in MyRepeater.Items)
{
     TextBox t = (TextBox)(itm.FindControl("TextBox1"));
     // do something with it.

}

Of course, you'll need to make sure that the TextBox1 in the ASPX has the Runat="Server" attribute.

David Stratton