nested repeater in user control

Hello,
I'm trying to build a nested repeater control within a datagrid. I've been able to do this within an aspx page, but can't seem to be able to have the itemtemplate data of the template column from the datagrid in a user control. Here's what I have:

main.aspx:
      <asp:DataGrid id="maindg" runat="server" autogeneratecolumns="false"></asp:DataGrid>

template.ascx:
      <%@ Control Language="c#"%>
      <table cellspacing="0" cellpadding="2" width="600" align="center" border="1">
      <tr>
            <td><b>Name: </b><%# DataBinder.Eval( (((DataGridItem)Container).DataItem),"Name") %></td>
      </tr>
      <tr>
            <td>
                  <b>Jobs: </b><br>
                  <asp:Repeater id="jobrep" runat="Server">
                        <ItemTemplate>
                              <%# DataBinder.Eval( Container.DataItem,"[\"Job\"]")%>
                        </ItemTemplate>
                  </asp:Repeater>
            </td>
      </tr>
      </table>

main.aspx.cs:
      DataGrid databinding function:
            string sql = "SELECT PersonID, Name FROM People; SELECT PersonID, Job FROM Jobs";
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            ds.Relations.Add("PersonJobs",ds.Tables[0].Columns["PersonID"],ds.Tables[1].Columns["PersonID"]);
            TemplateColumn tc = new TemplateColumn();
            tc.ItemTemplate = Page.LoadTemplate("template.ascx");
            maindg.Columns.Add(tc);
            maindg.DataSource = ds;
            maindg.DataBind();
      ItemDataBound function for data grid:
            private void mainrep_ItemDataBound(object sender, DataGridItemEventArgs e){
                  DataRowView dv = e.Item.DataItem as DataRowView;
                  Repeater rep = e.Item.FindControl("jobrep") as Repeater;
                        if(rep != null)
                        {
                              rep.DataSource = dv.CreateChildView("PersonJobs");
                              rep.DataBind();
                        }
            }

I'm able to the Name into the datagrid, but the ItemDataBound function is not able to find the jobrep repeater control and returns null every time. Am I missing something obvious? Any help is appreciated. Thanks in advance!
mrndnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PeterFearnCommented:
At some point you'll need to call the InstantiateIn method of the ItemTemplate, I've done it with repeaters, but not with DataGrids.

basically in the itemdatabound eventhandler, I do something like this

ItemTemplate.InstantiateIn(e.Item);

PeterFearnCommented:
something like:

private maindg_ItemDataBound(object sender, DataGridItemEventArgs e)
{
    TemplateColumn tc = maindg.Columns[0] as TemplateColumn;

    if (tc != null)
        tc.ItemTemplate.InstantiateIn(e.Item);
}
mrndnAuthor Commented:
When I do that, I get the following exception:

System.ArgumentException: 'TableRow' cannot have children of type 'template_ascx'.
for  tc.ItemTemplate.InstantiateIn(e.Item);

Is there some sort of page directive on the user control that I need to have? thanks for your help.
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

PeterFearnCommented:
you could try this

private maindg_ItemDataBound(object sender, DataGridItemEventArgs e)
{
    TemplateColumn tc = maindg.Columns[0] as TemplateColumn;

    if (tc != null)
    {
        e.Item.Cells[0].Controls.Clear();

        tc.ItemTemplate.InstantiateIn(e.Item.Cells[0]);

        e.Item.Cells[0].DataBind(); // or try e.Item.DataBind(), or leave out this line - not sure on this either
     }
}

that would be instantiating the template in the table cell rather than the table row.  (may not need the bind)

also the template control may need to implement System.Web.UI.ITemplate, but I'm not sure on that one.
mrndnAuthor Commented:
still can't seem to get the repeater to work... also, when i removed the databind function, the Name expression does not evaluate, but shows the correct number of datagrid items. also, just out of curiousity, why would i need to clear the controls for the item cell?

here's what i have:
      private void maindg_ItemDataBound(object sender, DataGridItemEventArgs e)
      {
            TemplateColumn tc = maindg.Columns[0] as TemplateColumn;
            if (tc != null)
            {
                  Response.Write(e.Item.Cells.Count+"<br>");
                  e.Item.Cells[0].Controls.Clear();
                  tc.ItemTemplate.InstantiateIn(e.Item.Cells[0]);
                  e.Item.DataBind();
            }

            DataRowView dv = e.Item.DataItem as DataRowView;
            Repeater rep1 = e.Item.FindControl("jobrep") as Repeater;
            if(rep1 != null)
            {
                  rep1.DataSource = dv.CreateChildView("PersonJobs");
                  rep1.DataBind();
            }
      }

any ideas?

thanks again
PeterFearnCommented:
what happens if you go back to your original code and try this instead:

          private void mainrep_ItemDataBound(object sender, DataGridItemEventArgs e){
               DataRowView dv = e.Item.DataItem as DataRowView;

// changed this line to do find control in the first cell
               Repeater rep = e.Item.Cells[0].FindControl("jobrep") as Repeater;
                    if(rep != null)
                    {
                         rep.DataSource = dv.CreateChildView("PersonJobs");
                         rep.DataBind();
                    }
          }

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mrndnAuthor Commented:
same results...

what's interesting is that e.Item.Cells[0].Controls only contains 1 control, which is template_ascx. It seems like i'm still referencing the outer naming container.
mrndnAuthor Commented:
i think i figured it out! figured that the repeater would be under the naming container for the template.ascx, so i tried this

e.Item.Cells[0].Controls[0].FindControl("jobrep")

and it worked!

Thanks for your help, Peter...i'll give you the credit since you led me in the right direction.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.