访问中继器控件中的文本框

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?

我正在从 LINQ-to-SQL 查询中检索一组图像,并将其和一些其他数据数据绑定到转发器.我需要为转发器中的每个项目添加一个文本框,让用户更改每个图像的标题,非常类似于 Flickr.

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.

如何访问中继器控件中的文本框并知道该文本框属于哪个图像?

这是转发器控件的外观,带有一个提交按钮,可以更新 Linq-to-SQL 中的所有图像行:

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

替代文字 http://casonclagg.com/layout.jpg

此代码有效

只要确保你不会像我一样通过在 if(!Page.IsPostBack) 之外绑定来破坏你的价值观.哎呀.

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>

并提交点击:

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
        }
    }
}

推荐答案

你有没有尝试过类似点击按钮的操作:-

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
      }
}

/* 其中txtName和Img分别是repeater中的文本框和图片控件的Id.*/

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

希望这会有所帮助.

这篇关于访问中继器控件中的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:访问中继器控件中的文本框

基础教程推荐