karthik k
karthik k

Reputation: 3991

bind data to repeater inside a gridview

Hi have a repeater which is inside a gridview. when i bind the data to gridview the data is binding to the controles inside the gridview but repeater is not binding.

<asp:GridView ID="gvMain" runat="server" AllowPaging="false" AutoGenerateColumns="false"
          Width="200px" Height="200px" 
    onrowdatabound="gvMain_RowDataBound">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="lbtnDptName" runat="server" Text='<%# Eval("deptName")%>'></asp:LinkButton>
                    <asp:Label ID="lblDptDesc" runat="server" Text = "sdfsdfsdfdsf"></asp:Label>
                    <asp:Repeater ID="rtFunctions" runat="server" OnItemDataBound="rtFunctions_ItemDataBound" >
                        <HeaderTemplate>
                            <table>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <tr>
                                <td>
                                    <asp:LinkButton ID="lbtnFunctions" runat="server" ></asp:LinkButton>
                                    <asp:Label ID="lbltemp" Style="border:1px solid blue;width:20px;height:20px;background:green" runat="server" Text="TempLabel" ></asp:Label>
                                </td>
                            </tr>
                        </ItemTemplate>
                        <FooterTemplate>
                            </table>
                        </FooterTemplate>
                    </asp:Repeater>

                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

in Page load:

gvMain.DataSource = objDeptColl;
                    gvMain.DataBind();

Codebehind for repeater:

protected void gvMain_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            FunctionCollection objTempFuncColl = new FunctionCollection();
            objTempFuncColl = (FunctionCollection)Cache["objFuncColl"];
            Repeater rt = (Repeater)e.Row.FindControl("rtFunctions");

            if (e.Row.RowType == DataControlRowType.DataRow && objTempFuncColl.Count !=0 )
            {
                rt.DataSource = objTempFuncColl;
                rt.DataBind();
            }
        }
        protected void rtFunctions_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        FunctionCollection objTempFuncColl = new FunctionCollection();
        objTempFuncColl = (FunctionCollection)Cache["objFuncColl"];
        Repeater rt = (Repeater)e.Item.FindControl("rtFunctions");
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            foreach (Functions f in objTempFuncColl)
            {
                LinkButton lnk = (LinkButton)e.Item.FindControl("lbtnFunctions");
                lnk.Text = f.funcName;
            }
        }
    }

linkbutton in gridview is binding but the linkbutton in repeater is not binding.

Upvotes: 1

Views: 11099

Answers (3)

Simon Halsey
Simon Halsey

Reputation: 5469

The problem appears to be with your repeater ondatabound function.

    FunctionCollection objTempFuncColl = new FunctionCollection();
    objTempFuncColl = (FunctionCollection)Cache["objFuncColl"];

The first line is not needed as you then replace it with the contents of the cache, which might be null if it's expired or purged or an instance.

For each row in your repeater, the link will be set to the last value in the objtempfunccoll.

you don't really need any of the function apart from lnk.Text = f.funcName; (you'll need to cast f from dataitem)

When you databind to the gridview, ondatabound is called for each row. you've got that wired-up. For each row you now need to find the repeater, set its datasource (we'll call this inner collection) & call databind on the repeater. this will cause ondatabound on the repeater to be called but the container.dataitem now points to each item in the inner collection. We can use that directly, casting container.dataitem to whatever type the inner collection is a list of.

protected void gvMain_RowDataBound(object sender, GridViewRowEventArgs e)
{
    FunctionCollection objTempFuncColl = (FunctionCollection)Cache["objFuncColl"];
    Repeater rt = (Repeater)e.Row.FindControl("rtFunctions");

    if (e.Row.RowType == DataControlRowType.DataRow && objTempFuncColl.Count !=0 )
    {
        rt.DataSource = objTempFuncColl;
        rt.DataBind();
    }
}

protected void rtFunctions_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    lnk.Text = ((Functions)e.Item.DataItem).funcName;
}

Simon

Upvotes: 1

onof
onof

Reputation: 17377

Why don't databind in the aspx, leaving empty the code behind:

<asp:GridView ID="gvMain" runat="server" AllowPaging="false" AutoGenerateColumns="false"
          Width="200px" Height="200px">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="lbtnDptName" runat="server" Text='<%# Eval("deptName")%>'></asp:LinkButton>
                    <asp:Label ID="lblDptDesc" runat="server" Text = "sdfsdfsdfdsf"></asp:Label>
                    <asp:Repeater ID="rtFunctions" runat="server" DataSource='<%# Cache["objFuncColl"] %>' >
                        <HeaderTemplate>
                            <table>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <tr>
                                <td>
                                    <asp:LinkButton ID="lbtnFunctions" runat="server" Text='<%# Eval("funcName") %>' ></asp:LinkButton>
                                    <asp:Label ID="lbltemp" Style="border:1px solid blue;width:20px;height:20px;background:green" runat="server" Text="TempLabel" ></asp:Label>
                                </td>
                            </tr>
                        </ItemTemplate>
                        <FooterTemplate>
                            </table>
                        </FooterTemplate>
                    </asp:Repeater>

                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Upvotes: 0

Kinexus
Kinexus

Reputation: 12904

You do not appear to be binding the repeater within any of this code. You may have some code to bind data to the GridView control, but this will not automatically bind anything to the repeater within the ItemTemplate.

Upvotes: 0

Related Questions