Loking for your insight as to why I’m getting this error, actually better yet, how to fix it. I have a nested gridview inside gridview. There may be a better way but at this point I’ve got the modal popup to work which also took forever, so unless this is completely stupid way to display master-detail then say so now.
I am able to pass the datakey id to the detail grid but that is where I get the null reference exception, I guess the bugger doesen’t exist yet, or is not visible. I mean, what-ever-the-reason is, how do I fix this? Thanks in advace.
aspx file:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:GridView ID="gvForum" runat="server" DataSourceID="odsForumApproval" DataKeyNames="id" Width="200px"
RepeatColumns="1" DataKeyField="id" CssClass="gridview"
AutoGenerateColumns="False" GridLines="None" OnSelectedIndexChanged="_OnCommand">
<AlternatingRowStyle CssClass="altbgcolor" />
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:Label runat="server" ID="lblTitle" Text='<%# Bind("Title") %>' />
<asp:Panel id="div" runat="server" align="center" class="confirm" style="display:none" >
<asp:GridView runat="server" ID="gvForumDetail" AutoGenerateColumns="False" DataKeyNames="id"
EmptyDataText="No Forum Detail" AllowPaging='true'
AllowSorting="true" PageSize="5" >
<AlternatingRowStyle CssClass="altbgcolor" />
<RowStyle VerticalAlign="Top" HorizontalAlign="Left" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="lblTraining" Text='<%# Bind("title") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="lblCompletionDate" Text='<%# Bind("description") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="lblRecurence" Text='<%# Bind("MemberName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="lblNotes" Text='<%# Bind("itemdate") %>' Width="200" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowCancelButton="true" ShowEditButton="true" ShowDeleteButton="true" />
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnClose" runat="server" Text="Close" Width="50px" />
</asp:Panel>
<ajaxToolKit:ModalPopupExtender ID="ModalPopupExtender1" runat="server"
TargetControlID="lblTitle"
PopupControlID="div"
CancelControlID="btnClose"
BackgroundCssClass="modalBackground" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="True" ShowDeleteButton="true" />
</Columns>
</asp:GridView>
code file:
public void _OnCommand(object sender, EventArgs e)
{
ObjectDataSource ods = new ObjectDataSource();
ods.ID = "ods_ForumDetail";
ods.EnableViewState = true;
ods.TypeName = "Forum";
ods.SelectMethod = "GetForumPostByID";
string id = "";
int rowIndex = gvForum.SelectedIndex;
id = gvForum.DataKeys[rowIndex].Value.ToString();
ods.SelectParameters.Add("id", System.TypeCode.Int32, id);
GridView gvForumDetail = (GridView)Master.FindControl("ContentPlaceHolder1").FindControl("gvForum").FindControl("gvForumDetail");
gvForumDetail.DataSource = ods;
gvForumDetail.DataBind();
}
You haven’t told where exactly you get the null-reference-exception.
But why are you going this indirect way via MasterPage to find your Detail-Grid?
The direct way is much easier and above all less error-prone:
Apart from that, your approach to find the nested GridView can never work. FindControl will only search the current NamingContainer for the given ID. Hence following might work, but is hardlinking the Master with one of it’s ContentPages what is always bad:
But this will not lead you to your nested GridView since…
…will not search all GridViewRows in the GridView for
gvForumDetailbut only the NamingContainer of the GridView itself. A GridViewRow has it’s own NamingContainer(to enable to use same ID’s in ItemTemplate although multiple rows exist), so the only correct approach is to get the current SelectedRow and search there your nested Grid(see above).You haven’t yet shown where you
DataBindyour nested GridView. That should be done in RowDataBound of the outer GridView. That might be the reason why it’s null.Another reason for getting a NullRefernceException might be that you are possibly DataBinding your outer GridView on every postback in page_load instead of only
if(!IsPostBack). That would deselect your Grid which causesSelectedRowto be nothing in my approach.