1

这是代码。它没有显示任何编译或运行时错误。然后我也进行了调试,直到数据绑定工作正常。但是网页中也没有显示控件!

示例.aspx:

    <body>
    <form id="form1" runat="server">
    <asp:PlaceHolder ID="_placeHolder1" runat="server">
    </asp:PlaceHolder>
    </form>
    </body>

示例.aspx.cs:

     protected void Page_Load(object sender, EventArgs e)
     {
      _placeHolder1.Controls.Add(CreateReapeater());
     }

     private Control CreateReapeater()
     {
        Repeater _repeater1 = new Repeater();
        Stack _stack1 = new Stack();
        for (int i = 0; i < 7; i++)
        {
            _stack1.Push(i);
        }
        _repeater1.DataSource = _stack1;
        _repeater1.DataBind();
        return _repeater1;
    }
4

1 回答 1

0

实际上中继器没有像gridview那样的内置列结构。因此,当我们动态绑定转发器时,我们还需要为此创建项目模板。

您需要修改 createrepeater 函数,如下所示。

private Control CreateReapeater()
{
    Repeater _repeater1 = new Repeater();
    Stack _stack1 = new Stack();
    for (int i = 0; i < 7; i++)
    {
        _stack1.Push(i);
    }

    _repeater1.DataSource = _stack1;          
    _repeater1.DataBind();

    foreach (RepeaterItem repeatItem in _repeater1.Items)
    {
        int index = repeatItem.ItemIndex;

        RepeaterItem repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Item);
        Label lbl = new Label();

        lbl.Text = "Item No :" + index.ToString() + "<br/>";
        repeatItem.Controls.Add(lbl);

    }



    return _repeater1;
}

这将解决您的问题。

快乐的编码......

于 2012-08-30T09:39:23.947 回答