在Repeater控件中访问文本框

我能想到的所有这些方式看起来都很苛刻。 这样做的正确方法是什么,或者至少是最常见的?

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

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

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

alt text http://sofzh.miximages.com/c%23/

编辑:

这段代码有效

只要确保你不要像我一样在if(!Page.IsPostBack)之外绑定你的价值。哎呀。

  
<a title='' href='' rel="gallery"> <img alt='' src='' width="260" />

并提交点击:

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

您是否尝试过按下按钮点击的内容: –

 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分别是转发器中文本框的ID和图像控件。* /

希望这可以帮助。

的.aspx

       

的.cs

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

确保将EnableViewState =“False”添加到转发器中,否则将获得空字符串。 (这浪费了我的时间,不要浪费你的:))

在回发时,您可以迭代repeater.Items中的RepeaterItems集合。 然后,您可以使用如下代码检索每个TextBox

 TextBox tbDemo = (TextBox)rptr.Items[index].FindControl("textBox");