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.*/
希望这会有所帮助.
这篇关于访问中继器控件中的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:访问中继器控件中的文本框
基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01