Luis Valencia
Luis Valencia

Reputation: 33978

How can I reference a repeater inside a usercontrol inside a webpart

Noobie, question, I dont remember !

I created a visual webpart, which creates a user control, I added a repeater in htnml view, and now I need to bind it to data, however I cant seem to find the repeater control in the codebehind.

<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>

And my 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();
        }
    }

Upvotes: 0

Views: 332

Answers (1)

Kiran Hegde
Kiran Hegde

Reputation: 3681

You can bind the repeater using following steps

1.Declare a public variable

Control innerControl;

2.Change the createchildcontrols function as below

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

3.Change the BindData function as below

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();
        }
    }
}

Upvotes: 1

Related Questions