读一个中继器内的文本框 [英] read textbox inside a repeater
问题描述
我有2个中继器打印菜单标题和菜单项 - 在其他中。
他们看起来是这样的:
I have 2 repeaters that print menu headers and menu items - on inside the other. They look like this:
<asp:Repeater ID="ParentRepeater" runat="server" OnItemDataBound="ParentRepeater_ItemDataBound">
<ItemTemplate>
<h2>
<%#DataBinder.Eval(Container.DataItem, "typenavn") %></h2>
<asp:HiddenField ID="HiddenField1" Value='<%# Eval("id") %>' runat="server" />
<asp:Repeater ID="ChildRepeater" runat="server">
<ItemTemplate>
<table>
<tr>
<td style="width: 200px">
<%#DataBinder.Eval(Container.DataItem, "productName") %>
</td>
<td style="width: 200px">
<%#DataBinder.Eval(Container.DataItem, "pris") %>
</td>
<td>
<asp:HiddenField ID="HiddenField2" runat="server" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
这一切都很好,有趣和作品。
但现在我需要找到不同的文本框 - 在文本框可以写多少你想不同的菜单项。
我已经尝试了许多不同的事情:
It's all good and fun and works. But now I need to find the different textboxes - in the textboxes you can write how many of the different menu items you want. I have tried many different things:
Control myControl1 = FindControl("MainContent_ParentRepeater_ChildRepeater_0_HB1_0");
和这样的:
foreach (RepeaterItem item in ParentRepeater.Items)
{
if (item.ItemType == ListItemType.Item)
{
TextBox txt = (TextBox)item.FindControl(("MainContent_ParentRepeater_ChildRepeater_0_HB1_0")) as TextBox;
// do something with "myTextBox.Text"
break;
}
}
和这样的:
foreach (RepeaterItem item1 in ParentRepeater.Items)
{
if (item1.ItemType == ListItemType.Item || item1.ItemType == ListItemType.AlternatingItem)
{
ChildRepeater = (Repeater)item1.FindControl("ChildRepeater");
foreach (RepeaterItem item2 in ChildRepeater.Items)
{
if (item2.ItemType == ListItemType.Item || item2.ItemType == ListItemType.AlternatingItem)
{
TextBox txt = (TextBox)item2.FindControl(("ct100$MainContent$ParentRepeater$ct100$ChildRepeater$ct100$HB1")) as TextBox; // MainContent_ParentRepeater_ChildRepeater_0_HB
}
}
}
break;
}
和它没有一个工作。有人在那里可以帮助我吗?如何让我的中继器??
And none of it work. Can anybody out there help me?? How do I get hold of my textbox inside the repeater??
推荐答案
的的FindControl
功能应采取的服务器的控件的ID,而不是呈现的客户端的控制。你应该能够做到这一点:
The FindControl
function should take the ID of the server control, not the rendered client control. You should be able to do this:
var txt = item.FindControl("TextBox1") as TextBox;
if (txt != null)
{
// found it!
}
要调整code:
foreach (RepeaterItem item1 in ParentRepeater.Items)
{
if (item1.ItemType == ListItemType.Item || item1.ItemType == ListItemType.AlternatingItem)
{
ChildRepeater = (Repeater)item1.FindControl("ChildRepeater");
foreach (RepeaterItem item2 in ChildRepeater.Items)
{
if (item2.ItemType == ListItemType.Item || item2.ItemType == ListItemType.AlternatingItem)
{
TextBox txt = (TextBox)item2.FindControl(("TextBox1")) as TextBox;
}
}
}
}
这篇关于读一个中继器内的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!