1

我在转发器中放置了一个文本框,但我不知道访问这些文本框的 ID 是什么。那么我应该如何访问它们?

    <asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1">
        <ItemTemplate>
            <asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" AutoPostBack="true" ></asp:TextBox>
        </ItemTemplate>
    </asp:Repeater>

请不要使用 FindControl!

我想要类似于以下代码的内容来访问。

TextBox1.Text = "Hi";
4

4 回答 4

1

我建议你这样做......

// another way to search for asp elements on page


 public static void GetAllControls<T>(this Control control, IList<T> list) where T : Control
        {
            foreach (Control c in control.Controls)
            {
                if (c != null && c is T)
                    list.Add(c as T);
                if (c.HasControls())
                    GetAllControls<T>(c, list);
            }
        }
于 2013-02-17T08:44:00.830 回答
0

典型的方法是在没有大量递归的情况下使用 FindControl(效率不高)连接 OnItemDataBound,甚至在中继器上,并在后面的代码中访问数据行的各个元素。您几乎必须使用 FindControl - 但在这种情况下,您不需要递归到控件集合中。

void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {

      // This event is raised for the header, the footer, separators, and items.

      // Execute the following logic for Items and Alternating Items.
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {

         if (((Evaluation)e.Item.DataItem).Rating == "Good") {
            ((Label)e.Item.FindControl("RatingLabel")).Text= "<b>***Good***</b>";
         }
      }
   }   
于 2013-02-17T16:56:51.570 回答
0

恕我直言,最短的方法是遍历转发器的所有项目,找到所需的控件并使用它做任何你想做的事情。示例,在 VB.NET 中

 For Each item As RepeaterItem In Repeater1.Items
     Dim temporaryVariable As TextBox = DirectCast(item.FindControl("TextBox1"), TextBox)
     temporaryVariable.Text = "Hi!"
 Next

但请记住,您必须Repeater1 之后执行此操作。数据绑定()

于 2015-07-28T11:11:04.833 回答
0

您可以使用Repeater.ItemDataBound

<asp:Repeater id="Repeater1" OnItemDataBound="R1_ItemDataBound" runat="server">
于 2018-09-25T15:02:29.407 回答