如何在c#后面的代码中从Repeater传递参数到User Control

时间:2011-02-04 21:51:16

标签: c# user-controls repeater

我需要遍历集合并将值传递给UserControl。它是动态的,我无法弄清楚如何传递参数值。我正在使用中继器。

<asp:Repeater runat="server" id="Repeater1">
  <ItemTemplate>
     <uc1:testControl ID="testControl1" runat="server"   />
  </ItemTemplate>
</asp:Repeater>

在背后的代码中:

Repeater1.DataSource = _myCollection;
                Repeater1.DataBind();

任何帮助将不胜感激!感谢

3 个答案:

答案 0 :(得分:8)

ASPX代码:

<asp:Repeater runat="server" id="Repeater1" OnItemDataBound="Repeater1_ItemDataBound">
    <ItemTemplate> 
        <uc1:testControl ID="testControl1" runat="server" /> 
    </ItemTemplate > 
</asp:Repeater>

代码隐藏:

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    testControl ctrl = e.Item.FindControl("testControl1") as testControl;

    if (ctrl != null)
        ctrl.MyParameterName = "Foo";
}

答案 1 :(得分:0)

Repeater.ItemDataBound事件添加事件处理程序...

答案 2 :(得分:0)

你可以用更简单的方式实现这一目标:

<asp:DataList RepeatLayout="Table" RepeatColumns="2" RepeatDirection="Horizontal" runat="server" ID="dl">
            <ItemTemplate>              
                <uc1:UCGroup ID="UCGroup1" runat="server" Title=<%# DataBinder.Eval(Container.DataItem, "RecipientGroup.Name") %> />

                    <div class="template_over"> 
                        <a href="/ScheduleCampaign/<%# DataBinder.Eval(Container.DataItem, "RecipientGroup.Name") %>">

                      <%# DataBinder.Eval(Container.DataItem, "Email") %>

                    </a>
                    </div>
              </ItemTemplate>               
            </asp:DataList>

public partial class ChooseGroup : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        dl.DataSource = GroupsHndlr.GetExtendedRecipients(MySession.Current.ClientId);
        dl.DataBind();
    }
}

控件的代码:

<asp:Label ID="lbltitle" runat="server"></asp:Label>

<asp:Repeater ID="rpRecipients" runat="server">
   <ItemTemplate>   
       1
       </ItemTemplate>
</asp:Repeater>

public partial class UCGroup : System.Web.UI.UserControl
{
    public string Title { get; set; }
    public List<string> Recipients { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        this.lbltitle.Text = Title;
        this.rpRecipients.DataSource = Recipients;
        this.rpRecipients.DataBind();
    }
}