title: How do I hide/show a <panel> using jquery?
body: <p>I am trying hard to show a panel (which contain two labels and it is hidden when page load) on a button click but not getting success.</p>
  
  <p>When I click on a button(id=Button1) then it should show a panel(id=anspanel) but it is not happening like that.
  Instead of that it remain as it is i.e. nothing happens on a button click.
  I can't find out why. Have a look at my code. Show me where I am making mistake and what is the solution.</p>
  
  <p><b>.aspx :-</b></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Student_Test" %&gt;
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
  
      &lt;link rel="stylesheet" href="http://code.jquery.com/ui/1.8.3/themes/base/jquery-ui.css" /&gt;
  &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" src="http://code.jquery.com/ui/1.8.3/jquery-ui.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
      var currentTab = 0;
      $(function () {
          $("#tabs").tabs({
              select: function (e, i) {
                  currentTab = i.index;
              }
          });
      });
      $("#btnNext").live("click", function () {
          var tabs = $('#tabs').tabs();
          var c = $('#tabs').tabs("length");
          currentTab = currentTab == (c - 1) ? currentTab : (currentTab + 1);
          tabs.tabs('select', currentTab);
          $("#btnPrevious").show();
          if (currentTab == (c - 1)) {
              $("#btnNext").hide();
          } else {
              $("#btnNext").show();
          }
      });
      $("#btnPrevious").live("click", function () {
          var tabs = $('#tabs').tabs();
          var c = $('#tabs').tabs("length");
          currentTab = currentTab == 0 ? currentTab : (currentTab - 1);
          tabs.tabs('select', currentTab);
          if (currentTab == 0) {
              $("#btnNext").show();
              $("#btnPrevious").hide();
          }
          if (currentTab &lt; (c - 1)) {
              $("#btnNext").show();
          }
      });
  
      $(function () {
          //$("#Panel2").hide();
          document.getElementById('form1').onsubmit = function () {
              return false;
          }//Avoid Reloading
          $("#Button1").click(function () {
              if ($('#anspanel').is(":hidden")) {
                  $('#anspanel').show();
         
              }
              else {
                  $('#anspanel').hide();
              }
  
              if ($("#Button1").val() == "Show Answer") {
                  $("#Button1").val("Hide Answer");
              } else {
                  $("#Button1").val("Show Answer");
              }
         
  
          });
         
      });
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
  
  
          &lt;div id="tabs"&gt;
      &lt;ul&gt;
          &lt;li&gt;&lt;a href="#tabs-1"&gt;Tab 1&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href="#tabs-2"&gt;Tab 2&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href="#tabs-3"&gt;Tab 3&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href="#tabs-4"&gt;Tab 4&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href="#tabs-5"&gt;Tab 5&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
      &lt;div id="tabs-1"&gt;
          &lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" &gt;
      &lt;Columns&gt;
          &lt;asp:TemplateField&gt;
              &lt;ItemTemplate&gt;
  
          &lt;asp:Panel ID="Panel1" runat="server"&gt;
  
              &lt;asp:Label ID="Label1" runat="server" Text='&lt;%#Eval("Question") %&gt;'&gt;&lt;/asp:Label&gt;
              &lt;br /&gt;
              &lt;br /&gt;
              &lt;br /&gt;
             &lt;span&gt;A-&lt;/span&gt; &lt;asp:Label ID="Label2" runat="server" Text='&lt;%#Eval("Option1")%&gt;'&gt;&lt;/asp:Label&gt;
              &lt;br /&gt;
              &lt;br /&gt;
              &lt;span&gt;B-&lt;/span&gt; &lt;asp:Label ID="Label3" runat="server" Text='&lt;%#Eval("Option2")%&gt;'&gt;&lt;/asp:Label&gt;
              &lt;br /&gt;
              &lt;br /&gt;
              &lt;span&gt;C-&lt;/span&gt; &lt;asp:Label ID="Label4" runat="server" Text='&lt;%#Eval("Option3")%&gt;'&gt;&lt;/asp:Label&gt;
              &lt;br /&gt;
              &lt;br /&gt;
              &lt;span&gt;D-&lt;/span&gt; &lt;asp:Label ID="Label5" runat="server" Text='&lt;%#Eval("Option4")%&gt;'&gt;&lt;/asp:Label&gt;
              &lt;br /&gt;
              &lt;br /&gt;
              &amp;nbsp;&amp;nbsp;
              &lt;asp:Button ID="Button1" runat="server" Text="Show Answer"  /&gt;
              &lt;br /&gt;
  
              &lt;asp:Panel id="anspanel" runat="server"&gt;
  
                  &lt;span&gt;Correct Answer is :-&lt;/span&gt;&lt;asp:Label ID="Label6" runat="server" Text='&lt;%#Eval("CorrectAns")%&gt;'&gt;&lt;/asp:Label&gt;
                  &lt;br /&gt;
                  &lt;br /&gt;
                  &lt;asp:Label ID="Label7" runat="server" Text='&lt;%#Eval("Explanation")%&gt;'&gt;&lt;/asp:Label&gt;
  
  
              &lt;/asp:Panel&gt;
  
  
          &lt;/asp:Panel&gt;
          &lt;br /&gt;
          &lt;br /&gt;
  
                   &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
      &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  
         
      &lt;/div&gt;
      &lt;div id="tabs-2"&gt;
          Tab 2 Content
      &lt;/div&gt;
      &lt;div id="tabs-3"&gt;
          Tab 3 Content
      &lt;/div&gt;
       &lt;div id="tabs-4"&gt;
          Tab 4 Content
      &lt;/div&gt;
       &lt;div id="tabs-5"&gt;
          Tab 5 Content
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;input type="button" id="btnPrevious" value="Previous" style = "display:none"/&gt;
  &lt;input type="button" id="btnNext" value="Next" /&gt;
  
      
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><b>.aspx.cs :-</b></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data.SqlClient;
  using System.Data;
  using System.Configuration;
  
  
  public partial class Student_Test : System.Web.UI.Page
  {
  protected void Page_Load(object sender, EventArgs e)
  {
      if (!IsPostBack)
      {
          GridView1.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions");
          GridView1.DataBind();
          foreach (GridViewRow row in GridView1.Rows)
          {
              if (row.RowType == DataControlRowType.DataRow)
              {
                  Panel panel1 = (Panel)row.FindControl("Panel1");
                  Panel anspanel = (Panel)panel1.FindControl("anspanel");
                  anspanel.Style.Add("display", "none");
              }
          }
      }
  }
  
  private DataSet GetData(string query)
  {
      string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
      SqlCommand cmd = new SqlCommand(query);
      using (SqlConnection con = new SqlConnection(conString))
      {
          using (SqlDataAdapter sda = new SqlDataAdapter())
          {
              cmd.Connection = con;
              sda.SelectCommand = cmd;
              using (DataSet ds = new DataSet())
              {
                  sda.Fill(ds);
                  return ds;
              }
          }
      }
  }
  }
  </code></pre>
  
  <p>ERROR :-</p>
  
  <pre><code>Server Error in '/' Application.
  
  Object reference not set to an instance of an object.
  
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
  
  Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
  
  Source Error: 
  
  
  Line 24:                     Panel panel1 = (Panel)row.FindControl("Panel1");
  Line 25:                     Panel anspanel = (Panel)panel1.FindControl("anspanel");
  Line 26:                     anspanel.Style.Add("display", "none");
  Line 27:                 }
  Line 28:             }
  
  Source File: e:\Way2Success\Student\Test.aspx.cs    Line: 26 
  
  Stack Trace: 
  
  
  [NullReferenceException: Object reference not set to an instance of an object.]
  Student_Test.Page_Load(Object sender, EventArgs e) in e:\Way2Success\Student\Test.aspx.cs:26
  System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
  System.Web.UI.Control.OnLoad(EventArgs e) +95
  System.Web.UI.Control.LoadRecursive() +59
  System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2952
  </code></pre>