在Repeater控件访问文本框 [英] Accessing Textboxes in Repeater Control
问题描述
我能想到做到的所有方面,这显得非常的hackish。什么是做正确的方式,或者至少是最常见?
我检索从LINQ到SQL查询一组图像和数据绑定它和其他一些数据转发器。我需要一个文本框添加到中继器的每个项目,这将让用户改变每个图像的标题,很相似到Flickr。
如何访问文本框的Repeater控件,并知道哪些形象文本框是属于?
下面是中继器控制会是什么样子,有一个提交按钮,这将更新所有的图像行LINQ到SQL:
编辑:
这code工作
只要确保你不要被绑定之外吹你的价值观的路程,如果(!Page.IsPostBack)像我..哎呀。
< ASP:直放站ID =Repeater1=服务器>
<&ItemTemplate中GT;
< DIV CLASS =itemBox>
< DIV CLASS =imgclass>
<标题='<%#的eval(姓名)%>' HREF ='<%#的eval(路径)%GT;'相对=画廊>
< IMG ALT ='<%#的eval(姓名)%>' SRC ='<%#的eval(路径)%GT;' WIDTH =260/>
&所述; / A>
< / DIV>
< ASP:文本框ID =TextBox1的WIDTH =230px=服务器>< / ASP:文本框>
< / DIV>
< / ItemTemplate中>
< / ASP:直放站>
和提交点击:
保护无效的button1_Click(对象发件人,EventArgs的发送)
{
的foreach(在Repeater1.Items的RepeaterItem项)
{
文本框txtName的=(文本框)item.FindControl(TextBox1中);
如果(txtName的!= NULL)
{
字符串VAL = txtName.Text;
//做一些VAL
}
}
}
您是否尝试过类似下面的按钮点击: -
的foreach(在Repeater1.Items的RepeaterItem项)
{
文本框txtName的=(文本框)item.FindControl(改为txtName);
如果(txtName的!= NULL)
{
//做一些txtName.Text
}
图片IMG =(图)item.FindControl(图);
如果(IMG!= NULL)
{
//做一些IMG
}
}
/ *如果txtName的和IMG是文本框的ID和图像中的中继器分别控制。* /
希望这有助于。
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:
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
}
}
}
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.
这篇关于在Repeater控件访问文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!