I have been working on this code for 2 days straight now and I can not figure out how to assign my "detailsPanel" panel, which is inside of my GridView1, an ID that is only for that row.
What I am wanting to do is that when a user clicks on a name in the gridview, I want my panel to appear with more information about that name. So, I will need a panel for each row and I am guessing each panel is going to need a distinct id so I can access
each one individually.
I have tried everything in my code behind that I have come across including doing this in my RowCreated for GridView1:
Panel panel = GridView1.FindControl("detailsPanel") as Panel;
panel = (sender as GridView).FindControl("detailsPanel") as Panel;
panel.ID = "detailsPanel" + e.Row.RowIndex;
I keep getting this error:
"System.NullReferenceException: Object reference not set to an instance of an object." I believe it is referring to my panel id.
I tried your solution with no success. I am still receiving this error: "System.NullReferenceException: Object reference not set to an instance of an object."
gvinson89
Member
2 Points
7 Posts
Showing and Hiding Panel in GridView
Apr 30, 2012 08:16 PM|LINK
Hello,
I have been working on this code for 2 days straight now and I can not figure out how to assign my "detailsPanel" panel, which is inside of my GridView1, an ID that is only for that row.
What I am wanting to do is that when a user clicks on a name in the gridview, I want my panel to appear with more information about that name. So, I will need a panel for each row and I am guessing each panel is going to need a distinct id so I can access each one individually.
Here is my GridView code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="0" GridLines="None" OnRowCommand="GridView1_RowCommand" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" ShowFooter="True" OnRowCreated="GridView1_RowCreated" DataKeyNames="id,adminCategory,positionApplyingFor" AllowSorting="true" OnSorting="gridView_Sorting" PageSize="200" Style="margin: 0 auto;" Width="900px" RowStyle-CssClass="adminRowStyle" RowStyle-Height="25px"> <Columns> <asp:TemplateField HeaderText=" " ItemStyle-Width="35px"> <HeaderStyle CssClass="adminPageHeaders" BackColor="#CCCCCC" /> <ItemTemplate> <asp:CheckBox ID="actionCheckBox" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="NAME" ItemStyle-Width="150px" SortExpression="firstName"> <HeaderStyle CssClass="adminPageHeaders" BackColor="#CCCCCC" /> <ItemTemplate> <asp:LinkButton ID="nameButton" runat="server" CommandName="ShowPDF" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "id") %>'> <%# Eval("firstName") %> <%#Eval("lastName") %> </asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="APP DATE" ItemStyle-Width="100px" SortExpression="date"> <HeaderStyle CssClass="adminPageHeaders" BackColor="#CCCCCC" /> <ItemTemplate> <%#DataBinder.Eval(Container.DataItem, "date", "{0:d}").ToString() %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="JOB TITLE" ItemStyle-Width="220px" SortExpression="positionApplyingFor"> <HeaderStyle CssClass="adminPageHeaders" BackColor="#CCCCCC" /> <ItemTemplate> <asp:LinkButton runat="server" ID="sortByJobTitle" CommandName="SortByJobTitle" CommandArgument='<%# Container.DisplayIndex %>'> <%#DataBinder.Eval(Container.DataItem, "positionApplyingFor") %> </asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="DEPARTMENT" ItemStyle-Width="200px" SortExpression="departmentApplyingFor"> <HeaderStyle CssClass="adminPageHeaders" BackColor="#CCCCCC" /> <ItemTemplate> <%#DataBinder.Eval(Container.DataItem, "departmentApplyingFor") %> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="RESUME" ItemStyle-Width="95px" SortExpression="resumePath"> <HeaderStyle CssClass="adminPageHeaders" BackColor="#CCCCCC" /> <ItemTemplate> <asp:LinkButton runat="server" ID="resumeLinkButton" CommandName="GetResume" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "resumePath").ToString() %>'> <%#DataBinder.Eval(Container.DataItem, "resumePath").ToString().Remove(7) %> </asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="LICENSE" ItemStyle-Width="95px" SortExpression="licensePath"> <HeaderStyle CssClass="adminPageHeaders" BackColor="#CCCCCC" /> <ItemTemplate> <asp:LinkButton runat="server" ID="dlLinkButton" CommandName="GetLicense" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "licensePath").ToString() %>'> <%#DataBinder.Eval(Container.DataItem, "licensePath").ToString().Remove(7)%> </asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="STATUS" ItemStyle-Width="65px" SortExpression="adminCategory"> <HeaderStyle CssClass="adminPageHeaders" BackColor="#CCCCCC" /> <ItemTemplate> <%#DataBinder.Eval(Container.DataItem, "adminCategory") %> </ItemTemplate> </asp:TemplateField> <%--Here is where my panel is--%> <asp:TemplateField> <ItemTemplate> </td</tr> <tr><td colspan="100%"> <asp:Panel style="width: 850px; margin: 0 auto; border: 0px solid #000; padding: 3px; margin: 2px;" ID='detailsPanel' Visible="true" runat="server"> This is some tex taslkjfl; kasd jfkljsadklf jaskldjflksa jflkj; </asp:Panel> </td></tr> </ItemTemplate> </asp:TemplateField> </Columns> <AlternatingRowStyle BorderColor="#8AABCC" BackColor="#D5DCE3" BorderStyle="Solid" BorderWidth="1px" /> <PagerStyle Font-Bold="False" Font-Size="9pt" Font-Names="Verdana" BackColor="#CCCCCC" /> </asp:GridView>I have tried everything in my code behind that I have come across including doing this in my RowCreated for GridView1:
Panel panel = GridView1.FindControl("detailsPanel") as Panel; panel = (sender as GridView).FindControl("detailsPanel") as Panel; panel.ID = "detailsPanel" + e.Row.RowIndex;I keep getting this error:
"System.NullReferenceException: Object reference not set to an instance of an object." I believe it is referring to my panel id.
Panel gridview
pierrefrc
Participant
947 Points
201 Posts
Re: Showing and Hiding Panel in GridView
Apr 30, 2012 08:27 PM|LINK
Hi
You should use FindControl not in GridView but in e.Row.
gvinson89
Member
2 Points
7 Posts
Re: Showing and Hiding Panel in GridView
Apr 30, 2012 08:34 PM|LINK
I tried your solution with no success. I am still receiving this error: "System.NullReferenceException: Object reference not set to an instance of an object."
This is my behind code:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { Panel panel = (Panel)e.Row.FindControl("detailsPanel"); panel.Visible = true; }pierrefrc
Participant
947 Points
201 Posts
Re: Showing and Hiding Panel in GridView
May 01, 2012 01:11 AM|LINK
Sorry, I forgot this if:
basheerkal
Star
10672 Points
2426 Posts
Re: Showing and Hiding Panel in GridView
May 01, 2012 03:00 AM|LINK
void HidePanels() { foreach (GridViewRow gvr in GridView1.Rows) { Panel pnl = (Panel)(gvr.FindControl("detailspanel")); pnl.Visible = false; } } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "GetResume") { HidePanels(); GridViewRow gvR = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer; Panel pnl = (Panel)(gvR.FindControl("detailspanel")); pnl.Visible=true; } }(Talk less..Work more)
AmalO.Abdull...
Contributor
3116 Points
764 Posts
Re: Showing and Hiding Panel in GridView
May 01, 2012 06:29 AM|LINK
you can get you panel in gridview1_rowdatabound event
protected void GridView1_RowDataBounf(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { Panel panel = (Panel)e.Row.FindControl("detailsPanel"); panel.Visible = true; } }