title: Keep Child Gridview expanded on PostBack
body: <p>Having some issues with keeping Child Gridview expanded after postback. I have implemented answers from other questions, but the Gridivew still minimizes on postback. Any help or direction would be appreciated, thanks. </p>
  
  <p><strong>JAVASCRIPT</strong></p>
  
  <pre><code>&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
    $("[src*=plus]").live("click", function () {
      $(this).closest("tr").after("&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td colspan = '999'&gt;" + $(this).next().html() + "&lt;/td&gt;&lt;/tr&gt;")
      $(this).attr("src", "../img/minus.png");
      $(this).next().next().val(1);
    });
  
    $("[src*=minus]").live("click", function () {
      $(this).attr("src", "../img/plus.png");
      $(this).closest("tr").next().remove();
      $(this).next().next().val("");
  
    });
  
    $(function () {
      $("[id*=IsExpanded]").each(function () {
        if ($(this).val() == "1") {
          $(this).closest("tr").after("&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td colspan = '999'&gt;" + $("[id*=childpanel]", $(this).closest("tr")).html() + "&lt;/td&gt;&lt;/tr&gt;")
          $("[src*=plus]", $(this).closest("tr")).attr("src", "../img/minus.png");
        }
      });
    });
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>.NET</strong></p>
  
  <pre><code>&lt;asp:Panel ID="panel1" runat="server" Visible="false" Width="100%"&gt;
    &lt;div class="griddiv" style="overflow-y: scroll; max-height: 500px; max-width: 80%;"&gt;
  
      &lt;asp:GridView ID="GridView1" runat="server" Font-Size="Medium" CellPadding="5" GridLines="None" AutoGenerateColumns="False"
        RowStyle-CssClass="rows" HeaderStyle-CssClass="gridheader" DataKeyNames="batchID" OnRowDataBound="OnRowDataBound"&gt;
  
        &lt;Columns&gt;
          &lt;asp:TemplateField&gt;
            &lt;ItemTemplate&gt;
              &lt;img alt="" style="cursor: pointer" src="../img/plus.png" /&gt;
              &lt;asp:Panel ID="childpanel" runat="server" Style="display: none;"&gt;
                &lt;div class="griddiv" style="overflow-y: scroll; max-height: 500px; max-width: 80%;"&gt;
                  &lt;asp:GridView ID="gvchild" runat="server" Font-Size="Medium" CellPadding="5" GridLines="None" AutoGenerateColumns="False"
                    CssClass="datagrid1" RowStyle-CssClass="rows" HeaderStyle-CssClass="gridheader" DataKeyNames="id"&gt;
                    &lt;AlternatingRowStyle BackColor="#e8e8f9" /&gt;
                    &lt;Columns&gt;
                      &lt;asp:TemplateField ItemStyle-Width="30px" HeaderText=""&gt;
                        &lt;ItemTemplate&gt;
                          &lt;asp:Button ID="lnkEdit" runat="server" Text="Edit" OnClick="Edit" CssClass="button" /&gt;
                        &lt;/ItemTemplate&gt;
                        &lt;ItemTemplate&gt;
                          &lt;asp:Label runat="server" ID="rowidtest" Text='&lt;%#Eval("id") %&gt;' Visible="false"&gt;&lt;/asp:Label&gt;
                        &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                      &lt;asp:BoundField ItemStyle-Width="150px" DataField="id" HeaderText="ID" /&gt;
                      &lt;asp:BoundField ItemStyle-Width="150px" DataField="optionnum" HeaderText="Option Number" /&gt;
                      &lt;asp:BoundField ItemStyle-Width="150px" DataField="optiondesc" HeaderText="Option Description" /&gt;
                      &lt;asp:BoundField ItemStyle-Width="150px" DataField="optioncat" HeaderText="Option Category" /&gt;
                      &lt;asp:BoundField DataField="optionsub" HeaderText="Option Sub-Category" ItemStyle-Width="150px" /&gt;
                      &lt;asp:BoundField ItemStyle-Width="150px" DataField="houseplan" HeaderText="House Plan" /&gt;
                      &lt;asp:BoundField ItemStyle-Width="150px" DataField="craftcode" HeaderText="Craft Code" /&gt;
                      &lt;asp:BoundField ItemStyle-Width="150px" DataField="parentoption" HeaderText="Parent Option" /&gt;
                      &lt;asp:BoundField DataField="parentoption2" HeaderText="Parent Option 2" ItemStyle-Width="150px" /&gt;
                      &lt;asp:BoundField DataField="parentoption3" HeaderText="Parent Option 3" ItemStyle-Width="150px" /&gt;
                      &lt;asp:BoundField DataField="parentoption4" HeaderText="Parent Option 4" ItemStyle-Width="150px" /&gt;
                      &lt;asp:BoundField DataField="parentoption5" HeaderText="Parent Option 5" ItemStyle-Width="150px" /&gt;
                      &lt;asp:TemplateField&gt;
                        &lt;HeaderTemplate&gt;
                          &lt;asp:CheckBox runat="server" ID="chkHeader" TextAlign="Right" /&gt;
                        &lt;/HeaderTemplate&gt;
                        &lt;ItemTemplate&gt;
                          &lt;asp:CheckBox runat="server" ID="chkRow" /&gt;
                        &lt;/ItemTemplate&gt;
                      &lt;/asp:TemplateField&gt;
                    &lt;/Columns&gt;
                  &lt;/asp:GridView&gt;
                &lt;/div&gt;
  
              &lt;/asp:Panel&gt;
              &lt;asp:HiddenField ID="IsExpanded" runat="server" /&gt;
            &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:BoundField DataField="batchID" HeaderText="Batch ID" HtmlEncode="true" HeaderStyle-Width="25%" /&gt;
          &lt;asp:BoundField DataField="requestdate" HeaderText="Request Date" HtmlEncode="true" HeaderStyle-Width="25%" /&gt;
          &lt;asp:BoundField DataField="requestby" HeaderText="Requested By" HtmlEncode="true" HeaderStyle-Width="25%" /&gt;
          &lt;asp:BoundField DataField="entityname" HeaderText="Division" HtmlEncode="true" HeaderStyle-Width="25%" /&gt;
  
        &lt;/Columns&gt;
  
      &lt;/asp:GridView&gt;
    &lt;/div&gt;
    &lt;h4&gt;&lt;/h4&gt;
    &lt;div&gt;
      &lt;asp:Button ID="Button" runat="server"
        Text="Approve" CssClass="button" /&gt;
    &lt;/div&gt;
  &lt;/asp:Panel&gt;
  </code></pre>
  
  <p><strong>VB</strong></p>
  
  <pre><code>Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    For Each row As GridViewRow In GridView1.Rows
      If row.RowType = DataControlRowType.DataRow Then
        Dim IsExpanded As HiddenField = TryCast(row.FindControl("IsExpanded"), HiddenField)
  
        IsExpanded.Value = Request.Form(IsExpanded.UniqueID)
      End If
    Next
  End Sub
  </code></pre>