2011-02-04 46 views
3

我需要遍歷集合並將值傳遞給UserControl。它是動態的,我不知道如何傳遞參數值。我正在使用中繼器。如何從Repeater在代碼隱藏中將參數傳遞給UserControl c#

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

在後面的代碼:

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

任何幫助將不勝感激!由於

+0

不知道爲什麼,但在代碼框中完全失敗了上面。我嘗試粘貼我的轉發器代碼,打字,包裝引號,並在其周圍放置代碼標籤。失敗。 – mokumaxCraig

+0

您可以突出顯示代碼並按** {} **按鈕。 –

+1

男人,這讓我瘋狂。感謝賈斯汀的信息! – mokumaxCraig

回答

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"; 
} 
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(); 
    } 
} 
相關問題