c# - 如何从代码隐藏 C# 中获取重复的 ID ASP 元素
问题描述
我的代码:
<asp:DataList ID="datalist" runat="server" >
<ItemTemplate>
<asp:Textbox ID="Values" runat="server" type="text" />
</ItemTemplate>
</asp:DataList>
<asp:Button ID="Button1" runat="server" Text="SEND" OnClick="send" />
如何从 C# 后面的代码中获取 DataList 的每个 Values ID 元素?
解决方案
您循环 DataList 中的所有项目并使用 FindControl 来定位 TextBox。
protected void send(object sender, EventArgs e)
{
//loop all the items in the datalist
foreach (DataListItem item in datalist.Items)
{
//find the textbox with findcontrol
TextBox tb = item.FindControl("Values") as TextBox;
//do something with the textbox content
string value = tb.Text;
}
}
推荐阅读
- r - R - 在循环内循环
- pg-promise - 如何调用 pg-promise helpers.insert 函数?
- azure-devops - 如何解决无法启动服务以在运行 Linux VM 的 Azure 上运行私有构建代理的问题
- azure-active-directory - 如何通过 AD Graph API 访问自定义注册字段
- sql-server - 查找不同订阅期的客户流失
- c# - 使用 AutoMapper 将 Viewmodel 映射到模型
- javascript - ChartJS 仅显示特定刻度的大字体
- python - WTForms 动态字段:使用 bind() 绑定无界字段
- javascript - Forge Viewer 无法显示边缘
- python - Tensorflow 模型训练:缺少 1 个必需的位置参数:'self'