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="Jmx0OyUjIEV2YWwo"Name") %>' href='<%# Eval("Path") %>' rel="gallery">
                    <img alt="Jmx0OyUjIEV2YWwo"Name") %>' src="Jmx0OyUjIEV2YWwo"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.*/
希望这会有所帮助.
这篇关于访问中继器控件中的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:访问中继器控件中的文本框
				
        
 
            
        - Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
 - C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
 - 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
 - 使用 rss + c# 2022-01-01
 - 带问号的 nvarchar 列结果 2022-01-01
 - CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
 - 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
 - Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
 - 在 C# 中异步处理项目队列 2022-01-01
 - 在 LINQ to SQL 中使用 contains() 2022-01-01
 
						
						
						
						
						