Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

c# - Accessing Textboxes in Repeater Control

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
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...