将中继器绑定到堆栈列表

时间:2012-08-30 08:44:57

标签: c# asp.net

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

Sample.aspx:

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

Sample.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;
    }

1 个答案:

答案 0 :(得分: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;
}

这将解决您的问题。

快乐编码.....