asp.net 中继器中的服务器控件

发布于 2024-07-29 11:13:19 字数 241 浏览 8 评论 0原文

看来我在这里碰壁了。 我希望将一些数据源绑定到 asp.net 中继器(好吧,不一定是中继器,但看起来这就是我想要的)。 现在,问题是:我还需要中继器内的一些服务器控件来更新数据(文本框和按钮)。

据我了解,这真的不可能有条不紊地进行吗? 我不能只将文本框添加到项目模板中,然后稍后在代码隐藏中获取它。 至少不容易。 是否有任何已知的技术可以解决此类问题?

我无法使用网格视图,因为数据需要以某种方式格式化。

Seems like I've ran into a wall here. I want some datasource to be bound to an asp.net repeater (well, doesn't have to be a repeater, but it seems like that's what I want). Now, here's the catch: I also need some server-controls inside that repeater for updating the data (TextBox'es and buttons).

To my understanding, this aint really possible to do in an orderly manner? I can't just add a textbox to the itemtemplate, and then fetch it later on in the code-behind it seems. At least not easily. Are there any known techniques for this kind of problem?

I can't use a gridview, since the data needs to be formatted in a certain way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灯下孤影 2024-08-05 11:13:20

如果您不想使用 ItemCommand 并且只想循环遍历 Repeater 的项目集合,那么页面底部有一个“保存”按钮,您可以这样做:

foreach(RepeaterItem itm in MyRepeater.Items)
{
     TextBox t = (TextBox)(itm.FindControl("TextBox1"));
     // do something with it.

}

当然,您需要确保 ASPX 中的 TextBox1 具有 Runat="Server" 属性。

If you don't want to use an ItemCommand and want to just loop through the Repeater's items collection, so you have one "save" button at the bottom of the page, you can do it like this:

foreach(RepeaterItem itm in MyRepeater.Items)
{
     TextBox t = (TextBox)(itm.FindControl("TextBox1"));
     // do something with it.

}

Of course, you'll need to make sure that the TextBox1 in the ASPX has the Runat="Server" attribute.

筱武穆 2024-08-05 11:13:20

您可以使用 Repeater.ItemDataBound 事件 定位嵌套在 Repeater 中的控件。

<asp:Repeater ID="Repeater1" Runat="server" OnItemDataBound="Repeater1_ItemDataBound">
   <ItemTemplate>
      <div><asp:TextBox ID="TextBox1" runat="server" /></div>
   </ItemTemplate>
</asp:Repeater>

然后在后面的代码中:

protected void Repeater1_ItemDataBound(object source, RepeaterCommandEventArgs e)
{
   if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType !=
      ListItemType.AlternatingItem)
      return;

   TextBox textBox1 = e.Item.FindControl("TextBox1") as TextBox;
   if (textBox1 != null)
   {
   // do something with it
   }
}

You can use the Repeater.ItemDataBound Event to locate controls nested in a Repeater.

<asp:Repeater ID="Repeater1" Runat="server" OnItemDataBound="Repeater1_ItemDataBound">
   <ItemTemplate>
      <div><asp:TextBox ID="TextBox1" runat="server" /></div>
   </ItemTemplate>
</asp:Repeater>

Then in code behind:

protected void Repeater1_ItemDataBound(object source, RepeaterCommandEventArgs e)
{
   if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType !=
      ListItemType.AlternatingItem)
      return;

   TextBox textBox1 = e.Item.FindControl("TextBox1") as TextBox;
   if (textBox1 != null)
   {
   // do something with it
   }
}
旧情勿念 2024-08-05 11:13:20

看来我的问题出在我的思考方式上:)

我的解决方案:
我只是像平常一样添加了控件,但在 ItemTemplate 中。 在控件的回调事件上,我会选择:(

按钮示例)

protected void btnUpdate_OnClick(object sender, EventArgs e)
    {
        Button b = sender as Button;
        if (b != null)
        {
            RepeaterItem ri = b.Parent as RepeaterItem;
            if (ri != null)
            {
                string name = null;

                //Fetch data
                TextBox txtName = ri.FindControl("txtName") as TextBox;

..等等。

因此,在找到 RepeaterItem 之后,我只是像处理任何 ControlGroup 一样对待它。 我实际上有 5 个不同的 textbosses,用 ID="txtName" 编码,但这并不重要,因为 asp.net 会自动在客户端标记中为控件提供“混淆”名称,并在回发时将其转换回我的 ID。

希望这对某人有帮助,抱歉打扰:)

Seems like my problem was in the way I was thinking :)

My solution:
I just added controls as I normally would do, but inside the ItemTemplate. On callback events of the controls, I'd go for:

(Button example)

protected void btnUpdate_OnClick(object sender, EventArgs e)
    {
        Button b = sender as Button;
        if (b != null)
        {
            RepeaterItem ri = b.Parent as RepeaterItem;
            if (ri != null)
            {
                string name = null;

                //Fetch data
                TextBox txtName = ri.FindControl("txtName") as TextBox;

.. etc..

So, after finding the RepeaterItem i just treat it as I would with any ControlGroup. Doesn't matter that I actually got 5 different textbosses, coded with ID="txtName", since asp.net automagically gives the controls "obfuscated" names in the client markup, and translates this back to my ID's on postback.

Hope this helps someone, and sorry for bothering :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文