我有一个asp:DataGrid
,其中有一个asp:Gridview
,其中有许多嵌套asp:Repeater
,我试图从我的内部引用嵌套转发器OnItemDataBound
功能
我的代码与此类似
<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>
有点复杂,但这与我正在合作。
在我的ItemDB
命令中,我有这个
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 = 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
但我收到了错误
对象引用未设置为对象的实例
我猜它是因为我在GridView中引用了一个Repeater
任何想法如何正确引用?
此代码可能不是最简单的方法,但我已经接管了其他人的工作,需要快速修复才能重新编码
提前致谢
答案 0 :(得分:1)
你必须在模板中找到Gridview,然后为它的RowDataBound注册事件,并在事件处理程序中找到转发器。您应该使用OnItemCreated事件来注册OnItemDataBound事件,但最简单的方法是指示.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>
并在您的代码中:
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.
}