25

All the ways I can think to do this seem very hackish. What is the right way to do this, or at least most common?

I am retrieving a set of images from a LINQ-to-SQL query and databinding it and some other data to a repeater. I need to add a textbox to each item in the repeater that will let the user change the title of each image, very similar to Flickr.

How do I access the textboxes in the repeater control and know which image that textbox belongs to?

Here is what the repeater control would look like, with a submit button which would update all the image rows in Linq-to-SQL:

alt text http://casonclagg.com/layout.jpg

Edit:

This code works

Just make sure you don't blow your values away by Binding outside of if(!Page.IsPostBack) like me.. Oops.

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <div class="itemBox">
            <div class="imgclass">
                <a title='<%# Eval("Name") %>' href='<%# Eval("Path") %>' rel="gallery">
                    <img alt='<%# Eval("Name") %>' src='<%# Eval("Path") %>' width="260" />
                </a>
            </div>
            <asp:TextBox ID="TextBox1" Width="230px" runat="server"></asp:TextBox>
        </div>
    </ItemTemplate>
</asp:Repeater>

And Submit Click:

protected void Button1_Click(object sender, EventArgs e)
{
    foreach (RepeaterItem item in Repeater1.Items)
    {
        TextBox txtName = (TextBox)item.FindControl("TextBox1");
        if (txtName != null)
        {
            string val = txtName.Text;
            //do something with val
        }
    }
}
Jason
  • 11,435
  • 24
  • 77
  • 131

3 Answers3

34

Have you tried something like following on the button click:-

foreach (RepeaterItem item in Repeater1.Items)
{
      TextBox txtName= (TextBox)item.FindControl("txtName");
      if(txtName!=null)
      {
      //do something with txtName.Text
      }
      Image img= (Image)item.FindControl("Img");
      if(img!=null)
      {
      //do something with img
      }
}

/* Where txtName and Img are the Ids of the textbox and the image controls respectively in the repeater.*/

Hope this helps.

Ashish Gupta
  • 14,869
  • 20
  • 75
  • 134
  • You don't know the value of "txtName", do you? – Jason Mar 20 '10 at 21:36
  • 1
    You will get the value using that txtName.Text property in the above example. – Ashish Gupta Mar 20 '10 at 21:38
  • I meant you don't know the name of the control. So how do you find it...like in FindControl("nameOfControl"). What is nameOfControl? – Jason Mar 20 '10 at 22:05
  • Really? Could you please put the Repeater mark up in the ASPX. I think It should be there. Also could you please put just some detail on how are you building this repeater? – Ashish Gupta Mar 20 '10 at 22:22
  • 3
    Nevermind, I am an idiot. I didn't have a if(!Page.IsPostback) in my page load. So the textboxes were getting reset. Ignore everything. =D – Jason Mar 20 '10 at 22:54
  • 1
    Thanks, in your EDIT, the TextBox1 is the name of the control for textbox and you can get the textboxt using FindControl("TextBox1").. If you add runat="server" in your Img tag and have an Id for that say Id="imgPuppy", you can get the image using FindControl("imgPuppy"). Let me know If you need more details. – Ashish Gupta Mar 20 '10 at 22:57
  • What If I want to cycle through all the controls and get ONLY the textboxes and add it to a List? – Si8 Dec 18 '15 at 14:43
12

.aspx

        <asp:Repeater ID="rpt" runat="server" EnableViewState="False">
        <ItemTemplate>
                <asp:TextBox ID="txtQty" runat="server" /> 
        </ItemTemplate>
        </asp:Repeater>

.cs

        foreach (RepeaterItem rptItem in rpt.Items)
        {
            TextBox txtQty = (TextBox)rptItem.FindControl("txtQty");
            if (txtQty != null) { Response.Write(txtQty.Text); }          
        }

Be sure to add EnableViewState="False" to your repeater, otherwise you will get empty string. (That wasted my time, dont waste yours :) )

Etienne Dupuis
  • 13,548
  • 6
  • 47
  • 58
  • What if I wanted to get all the textboxes and add it to a List to disable the `readonly` property to true? – Si8 Dec 18 '15 at 14:46
0

On postback, you can iterate over the collection of RepeaterItems in repeater.Items. You could then retrieve each TextBox with code such as

TextBox tbDemo = (TextBox)rptr.Items[index].FindControl("textBox");
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246