繁体   English   中英

将数据绑定到 gridview 内的中继器

[英]bind data to repeater inside a gridview

您好,有一个中继器位于 gridview 内。 当我将数据绑定到 gridview 时,数据绑定到 gridview 内的控件,但中继器未绑定。

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

在页面加载中:

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

中继器的代码隐藏:

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

gridview 中的链接按钮已绑定,但中继器中的链接按钮未绑定。

问题似乎出在您的转发器 ondatabound function 上。

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

不需要第一行,因为然后将其替换为缓存的内容,如果它已过期或清除或实例,则可能是 null。

对于转发器中的每一行,链接将设置为 objtempfunccoll 中的最后一个值。

你真的不需要任何 function 除了lnk.Text = f.funcName; (您需要从数据项中转换 f)

当您对 gridview 进行数据绑定时,将为每一行调用 ondatabound。 你已经连接好了。 对于您现在需要找到中继器的每一行,设置其数据源(我们将称之为内部集合)并在中继器上调用 databind。 这将导致转发器上的 ondatabound 被调用,但 container.dataitem 现在指向内部集合中的每个项目。 我们可以直接使用它,将 container.dataitem 转换为内部集合是列表的任何类型。

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

西蒙

您似乎没有在任何此代码中绑定转发器。 您可能有一些代码将数据绑定到 GridView 控件,但这不会自动将任何内容绑定到 ItemTemplate 中的转发器。

为什么不在aspx中进行数据绑定,留下空白代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM