Q:

asp.net user controls binds inside gridview

Hello fellow programmers, got a few problem here. I am adding a user control inside a gridview. 
Now my question is how can bind it cause inside the user control is a gridviewthat needs the CourseCatID so that it could bind the datas. And by the way I cannot use nested griview cause I need the render of the nested usercontrol for another purpose. Any tutorial/help will be gladly appreciated.
<asp:GridView ID="grdCategory" runat="server" AutoGenerateColumns="False" Width="1100px"
                DataKeyNames="CourseCatID" Font-Names="verdana,arial,helvetica,sans-serif" Font-Size="8pt"
                CellPadding="4" ForeColor="#333333" GridLines="None">
                <Columns>
                    <asp:ButtonField Text="SingleClick" CommandName="SingleClick" Visible="False" />
                    <asp:BoundField HeaderText="CourseCatID" Visible = "false" DataField="CourseCatID" />
                    <asp:TemplateField HeaderText="Course Category">
                        <ItemTemplate>
                            <asp:Label ID="lblCourseCatID" runat="server" Visible="false" Text='<%# Eval("CourseCatID")%>'></asp:Label>
                                 <a href="javascript:toggleDiv('mydiv<%# Eval("CourseCatID")%>')">
                                 <asp:TextBox ID="txtCourseCatName" runat="server" Text='<%# Eval("CourseCatName") %>' Font-Size="XX-Small"
                            Font-Names="Verdana" Width="300px" Visible="false"></asp:TextBox>
                                <asp:Image ID="img" onclick="javascript:Toggle(this);" runat="server" ImageUrl="~/Images/minus.gif"
                                    ToolTip="Collapse" Width="7px" Height="7px" ImageAlign="AbsMiddle" /></a>
                            <asp:Label ID="lbllastname" Height="15px" runat="server" Text='<%# Eval("CourseCatName")%>'> </asp:Label>
                            <div id="mydiv<%# Eval("CourseCatID")%>">

                                <br />
                              &#160&#160&#160&#160&#160&#160<%--OnClick="ImageAdd_click" --%>
                                <asp:ImageButton ID="ImageAdd" Height="17px" ImageUrl="Images/addCourse.png" runat="server"
                                    CommandName="cmdAdd" CommandArgument='<%# Eval("CourseCatID") %>' />
                                    <br />
                                    <br />

                                    &#160&#160&#160&#160&#160&#160
                                <asp:Panel ID="pnlCourse" runat="server"></asp:Panel>
                                <b><cuc1:CourseUserControl ID="CourseUserControl1" runat="server" /></b>
                                <br />
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <br />
                                 <br />
                                  <br />

                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            </div>
                        </ItemTemplate>
                    </asp:TemplateField>

Thank you for your time

A:

You have a couple of options.
The easiest one is to expose a public property on the user control that will let you do this:
<cuc1:CourseUserControl ID="CourseUserControl1" runat="server" CourseCategoryID='<%# (int)Eval("CourseCatID") %>' />

Then databind in the user control as soon as that property is assigned to. Note that I'm assuming the category is an Int32. For example (note that the CourseCategoryID stores its value in a private field, not in ViewState):
private int _courseCategoryID;
public int CourseCategoryID
{
    get { return _courseCategoryID; }
    set
    {
        _courseCategoryID = value;

        // TODO: code that initializes the GridView in user control.

        this.DataBind();
    }
}

Your other option is to expose the same property and handle the RowDataBound event and do this:
if (e.Row.RowType == DataControlType.DataRow)
{
    CourseUserControl courseDetails;
    courseDetails = (CourseUserControl)e.Row.FindItem("CourseUserControl1");

    // Assuming category ID is Int32
    courseDetails.CourseCategoryID = (int)grdCategory.DataKeys[e.Row.RowIndex].Value;
    courseDetails.DataBind();
}

Note that I'm databinding manually instead of immediately after assigning a new category to the user control in the current row.
For more information, see:
GridView.RowDataBound Event (ASP.NET 3.5)
 or 
GridView.RowDataBound Event (ASP.NET 4.0)

