开发者

Reference a repeater within a gridview on item databound

开发者 https://www.devze.com 2023-03-21 02:21 出处:网络
I\'ve got an asp:DataGrid which has an asp:Gridview within it and this has many nested asp:Repeater\'s within that and i\'m trying to reference the nested repeater from within my OnItemDataBound funct

I've got an asp:DataGrid which has an asp:Gridview within it and this has many nested asp:Repeater's within that and i'm trying to reference the nested repeater from within my OnItemDataBound function

My code is similar to this

<asp:Datagrid runat="server" id="DataGrid1" OnItemDataBound="ItemDB" AutoGenerateColumns="false" Gridlines="None">
    <Columns>
        <asp:TemplateColumn HeaderText="">
            <ItemTemplate>
                <asp:GridView id="GridView1" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Repeater id="Repeater1" runat="server">
                                    <ItemTemplate>
                                        <p>Test</p>
                                    </ItemTemplate>
                                </asp:Repeater>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </ItemTemplate>
        </asp:TemplateColumn>
    </Columns>
</asp:Datagrid>

bit complicated but that's kinda what i'm working with.

In my ItemDB command I have this

Sub ItemDB(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)

    Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType 开发者_Python百科= ListItemType.AlternatingItem Then


        If CType(e.Item.FindControl("GridView1"), GridView).Visible = True Then
            CType(e.Item.FindControl("Repeater"), GridView).Visible = True
        End If
    End If
End Sub

but i get the error

Object reference not set to an instance of an object

and i'm guessing it's because i am referencing a Repeater within a GridView

Any ideas how to reference this properly?

This code may not be the simplest way of doing this but i've taken over someone else's work and need a quick fix before re-coding it all

Thanks in advance


You'll have to find the Gridview in the template and then register the event for it's RowDataBound, and the find the repeater in the event handler. You should use the OnItemCreated Event to register the OnItemDataBound events, but the easiest would be to indicate the methods in your .aspx:

<asp:GridView id="GridView1" runat="server" AutoGenerateColumns="false" onrowdatabound="GridView1_RowDataBound">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Repeater id="Repeater1" runat=""server"
                                     onitemdatabound="Repeater1_ItemDataBound">
                                    <ItemTemplate>
                                        <p>Test</p>
                                    </ItemTemplate>
                                </asp:Repeater>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

and in your code behind:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //you could find the repeater in the gridview's itemtemplate here
    // to the BulletedList
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Repeater rpt = (Repeater)e.Row.FindControl("Repeater1");
        rpt.Visible = false;

    }
}
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
         //you could find controls in the repeater's itemtemplate here.
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消