簡體   English   中英

如何在Webpart的用戶控件中引用中繼器

[英]How can I reference a repeater inside a usercontrol inside a webpart

Noobie,問題,我不記得了!

我創建了一個可視化Web部件,它創建了一個用戶控件,在htnml視圖中添加了一個轉發器,現在我需要將其綁定到數據,但是我似乎無法在后面的代碼中找到轉發器控件。

<table id="engagementletterReport" class="display" border="0">
     <thead>
         <tr>
             <th>JobCode</th>
             <th>JobName</th>
          </tr>
     </thead>
     <tbody>
         <asp:Repeater runat="server" ID="repeater">
             <ItemTemplate>
                 <tr>
                     <td>
                         <%# DataBinder.Eval(Container.DataItem, "JobCode") %>
                     </td>
                     <td>
                         <%# DataBinder.Eval(Container.DataItem, "JobName") %>
                     </td>
                 </tr>
             </ItemTemplate>
         </asp:Repeater>
     </tbody>
 </table>

還有我的webpart

public class EngagementLetterWebPart : WebPart
    {
        // Visual Studio might automatically update this path when you change the Visual Web Part project item.
        private const string _ascxPath = @"~/_CONTROLTEMPLATES/15/xx.SP.xx.WebParts.WebParts/EngagementLetterWebPart/EngagementLetterWebPartUserControl.ascx";

        protected override void CreateChildControls()
        {
            Control control = Page.LoadControl(_ascxPath);
            Controls.Add(control);
        }

        protected override void OnLoad(EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            //here, how??
            repeater.DataSource = JobContext.GetEngagementLetterReport(SPContext.Current.Site, true, 500, 1);
            repeater.DataBind();
        }
    }

您可以使用以下步驟綁定轉發器

1,聲明一個公共變量

Control innerControl;

2,更改createchildcontrols函數如下

protected override void CreateChildControls()
{
        innerControl = Page.LoadControl(_ascxPath);
        Controls.Add(innerControl);
}

3,按如下所示更改BindData函數

private void BindData()
{
    if (innerControl != null)
    {
        //here, how??
        Repeater repeater = innerControl.FindControl("repeater") as Repeater;
        if (repeater != null)
        {
            repeater.DataSource = JobContext.GetEngagementLetterReport(SPContext.Current.Site, true, 500, 1);
            repeater.DataBind();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM