C# 将数据绑定到gridview中的中继器

C# 将数据绑定到gridview中的中继器,c#,asp.net,C#,Asp.net,嗨,有一个中继器在gridview中。当我将数据绑定到gridview时,数据绑定到gridview中的控件,但repeater没有绑定 <asp:GridView ID="gvMain" runat="server" AllowPaging="false" AutoGenerateColumns="false" Width="200px" Height="200px" onrowdatabound="gvMain_RowDataBound">

嗨,有一个中继器在gridview中。当我将数据绑定到gridview时,数据绑定到gridview中的控件,但repeater没有绑定

<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>
中继器的代码隐藏:

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中的linkbutton已绑定,但中继器中的linkbutton未绑定。

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

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

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

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

为什么不在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>

问题似乎出在中继器ondatabound函数上

    FunctionCollection objTempFuncColl = new FunctionCollection();
    objTempFuncColl = (FunctionCollection)Cache["objFuncColl"];
不需要第一行,因为随后将其替换为缓存的内容,如果缓存已过期、已清除或是实例,则缓存的内容可能为null

对于中继器中的每一行,链接将被设置为objtempfuncoll中的最后一个值

除了
lnk.Text=f.funcName(您需要从dataitem强制转换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;
}

Simon

问题似乎出在中继器ondatabound函数上

    FunctionCollection objTempFuncColl = new FunctionCollection();
    objTempFuncColl = (FunctionCollection)Cache["objFuncColl"];
不需要第一行,因为随后将其替换为缓存的内容,如果缓存已过期、已清除或是实例,则缓存的内容可能为null

对于中继器中的每一行,链接将被设置为objtempfuncoll中的最后一个值

除了
lnk.Text=f.funcName(您需要从dataitem强制转换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;
}


Simon

实际将数据绑定到中继器的代码在哪里?我已经添加了代码。请参见问题中的内容。如果您在中继器的DataBind()上放置断点,它是否命中?是的。。Itemdatabound事件中的foreach循环正在迭代,funcName值将附加到链接按钮,但它不会显示在中继器中。如果向链接按钮添加一些默认文本,并注释掉试图更改它的代码,它会显示吗?老实说,注释掉整个rtFunctions\u ItemDataBound,看看会发生什么。实际将数据绑定到中继器的代码在哪里?我已经添加了代码。请看问题中的内容。如果在中继器的DataBind()上放置断点,它会命中吗?是的。。Itemdatabound事件中的foreach循环正在迭代,funcName值将附加到链接按钮,但它不会显示在中继器中。如果向链接按钮添加一些默认文本,并注释掉试图更改它的代码,它会显示吗?老实说,注释掉整个rtFunctions\u ItemDataBound,看看会发生什么。在
gvMain\u RowDataBound
方法中有一个
DataBind
调用。发布代码时没有调用,查看问题中的评论。是的,chris我在问了这个特殊问题后已经准备好了在
gvMain\u RowDataBound
方法中有一个
DataBind
调用。代码发布时没有,请查看问题中的评论。是的,chris我在问了这个特殊问题后已经准备好了拆分头发,但你不是指数据源='@Simon,你说得对,非常感谢。我被最近使用的Scala欺骗了:)吹毛求疵,但你不是说DataSource='@Simon,你说得对,谢谢。我被最近使用的Scala欺骗了:)西蒙:我认为你给我的解决方案可能有效。你能解释得更清楚一点吗?添加了更详细的解释。你能发布代码将containerdataitem添加到中继器吗?添加了代码,假设FunctionCollection是一个函数集合