title: how to show hidden div from codebehind c#
body: <p>I'm trying to initialize a usercontrol that contains a gridview as hidden when a page first loads. When the user clicks a "Search" button on the page then I want to show the gridview that's within that usercontrol. I've tried a bunch of different methods for showing and hiding the user control. I've tried placing the usercontrol in a div and then using style="display: none" to initialize the div as being hidden, and then in the code behind changing the style to 'block'. I've tried using 2 classes with one class having display set to none and the other with display set to block, and then switching between these 2 classes based on user clicking on "Search" button. I've tried using a Panel and setting its visibility to true/false, but the panel never shows when I set its visibility to true after the user clicks on the Search button. All methods I've tried, except using a panel and its visibility option, give me the following exception:</p>
  
  <pre><code>{InnerText = '((System.Web.UI.HtmlControls.HtmlContainerControl)(divGrid)).InnerText' threw an exception of type 'System.Web.HttpException'}
  </code></pre>
  
  <p>Here's the code I'm using that's causing me this error:</p>
  
  <p>markup:</p>
  
  <pre><code>&lt;div id="divGrid" runat="server"&gt;
      &lt;uc1:ContactsGridUserControl ID="ContactsGridUserControl1" runat="server" UpdateMode="Conditional"/&gt;      
  &lt;/div&gt; 
  
  &lt;asp:Button ID="btnSearch" runat="server" Text="Search" Font-Size="Small" Height="30px" 
                  Width="67px" ClientIDMode="Static" onclick="btnSearch_Click"&gt;&lt;/asp:Button&gt;
  </code></pre>
  
  <p>code behind:</p>
  
  <pre><code>protected void btnSearch_Click(object sender, EventArgs e)
  {
      //string redirect = CommonStrings.editContactPage;
      bool bStatus = false;
  
      try
      {
          bStatus = validateFields();
  
          if (bStatus)
          {
              divGrid.Style["display"] = "block";           
  
          }
          else
          {
              lblError.Text = "Please enter characters for at least one field below";
  
          }
      }
      catch (Exception ex)
      {
          lblError.Text = "Exception: " + ex.ToString();
      }
  }
  
  protected bool validateFields()
  {
      try
      {
          if ((txtbxFirstName.Text != null &amp;&amp; txtbxFirstName.Text.Length &gt; 0) ||
              (txtbxLastName.Text != null &amp;&amp; txtbxLastName.Text.Length &gt; 0) ||
              (txtbxPhoneNum.Text != null &amp;&amp; txtbxPhoneNum.Text.Length &gt; 0) ||
              (txtbxCity.Text != null &amp;&amp; txtbxCity.Text.Length &gt; 0) ||
              (txtbxZipCode.Text != null &amp;&amp; txtbxZipCode.Text.Length &gt; 0) ||
              (txtDate1.Text != null &amp;&amp; txtDate1.Text.Length &gt; 0) ||
              (txtDate2.Text != null &amp;&amp; txtDate2.Text.Length &gt; 0) ||
              (txtbxCompanyName.Text != null &amp;&amp; txtbxCompanyName.Text.Length &gt; 0))
          {
              return true;
  
          }
          else
          {
              return false;
          }
      }
      catch (Exception ex)
      {
          lblError.Text = "Exception: " + ex.ToString();
          return false;
      }
  }
  </code></pre>
  
  <p>Full Stack Trace: (I right clicked on Call Stack Window within Visual Studio and chose "show external code". I'm not sure if I did this right to show full stack trace, as opposed to showing what's on top of the stack by default)</p>
  
  <blockquote>
    <p>App_Web_gjla55xr.dll!AdvancedSearch.btnSearch_Click(object sender, System.EventArgs e) Line 50  C#
        System.Web.dll!System.Web.UI.WebControls.Button.OnClick(System.EventArgs e) + 0x77 bytes<br>
        System.Web.dll!System.Web.UI.WebControls.Button.RaisePostBackEvent(string eventArgument) + 0x71 bytes<br>
        System.Web.dll!System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(string eventArgument) + 0xb bytes<br>
        System.Web.dll!System.Web.UI.Page.RaisePostBackEvent(System.Web.UI.IPostBackEventHandler sourceControl, string eventArgument) + 0xe bytes<br>
        System.Web.dll!System.Web.UI.Page.RaisePostBackEvent(System.Collections.Specialized.NameValueCollection postData) + 0x25 bytes<br>
        System.Web.dll!System.Web.UI.Page.ProcessRequestMain(bool includeStagesBeforeAsyncPoint, bool includeStagesAfterAsyncPoint) + 0x15bc bytes<br>
        System.Web.dll!System.Web.UI.Page.ProcessRequest(bool includeStagesBeforeAsyncPoint, bool includeStagesAfterAsyncPoint) + 0x8d bytes<br>
        System.Web.dll!System.Web.UI.Page.ProcessRequest() + 0x4f bytes 
        System.Web.dll!System.Web.UI.Page.ProcessRequestWithNoAssert(System.Web.HttpContext context) + 0x16 bytes<br>
        System.Web.dll!System.Web.UI.Page.ProcessRequest(System.Web.HttpContext context) + 0x32 bytes<br>
        App_Web_gjla55xr.dll!ASP.advancedsearch_aspx.ProcessRequest(System.Web.HttpContext context) + 0x33 bytes    C#
        System.Web.dll!System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() + 0x65 bytes 
        System.Web.dll!System.Web.HttpApplication.ExecuteStep(System.Web.HttpApplication.IExecutionStep step, ref bool completedSynchronously) + 0x4c bytes 
        System.Web.dll!System.Web.HttpApplication.ApplicationStepManager.ResumeSteps(System.Exception error) + 0x13e bytes<br>
        System.Web.dll!System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(System.Web.HttpContext context, System.AsyncCallback cb, object extraData) + 0xf8 bytes<br>
        System.Web.dll!System.Web.HttpRuntime.ProcessRequestInternal(System.Web.HttpWorkerRequest wr) + 0x1a2 bytes 
        System.Web.dll!System.Web.HttpRuntime.ProcessRequestNoDemand(System.Web.HttpWorkerRequest wr) + 0x7d bytes<br>
        System.Web.dll!System.Web.HttpRuntime.ProcessRequest(System.Web.HttpWorkerRequest wr) + 0x47 bytes<br>
        WebDev.WebHost40.dll!Microsoft.VisualStudio.WebHost.Request.Process() + 0x17b bytes 
        WebDev.WebHost40.dll!Microsoft.VisualStudio.WebHost.Host.ProcessRequest(Microsoft.VisualStudio.WebHost.Connection conn) + 0x6c bytes<br>
        [Appdomain Transition]<br>
        WebDev.WebHost40.dll!Microsoft.VisualStudio.WebHost.Server.OnSocketAccept(object acceptedSocket) + 0x83 bytes<br>
        mscorlib.dll!System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(object state) + 0x2d bytes 
        mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool ignoreSyncCtx) + 0xb0 bytes<br>
        mscorlib.dll!System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() + 0x5a bytes 
        mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() + 0x147 bytes<br>
        mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() + 0x2d bytes<br>
        [Native to Managed Transition]  </p>
  </blockquote>
  
  <p>Edit: I solved the answer to my problem. I needed to surround the user control with an ajax update panel and have a asyncpostbacktrigger wired to the Search button click event. I also surround the usercontrol with another panel and set that panel's visibility to true/false from code behind. </p>
  
  <p>updated code:</p>
  
  <pre><code>&lt;asp:UpdatePanel ID="UpdatePanel2" runat="server"&gt;
      &lt;ContentTemplate&gt;
          &lt;asp:Panel ID="pnlGrid" Visible="false" runat="server"&gt;   
              &lt;uc1:ContactsGridUserControl ID="ContactsGridUserControl1" runat="server" UpdateMode="Conditional"/&gt;
          &lt;/asp:Panel&gt;
      &lt;/ContentTemplate&gt;
      &lt;Triggers&gt;
          &lt;asp:AsyncPostBackTrigger ControlID="btnSearch" EventName="Click" /&gt;
      &lt;/Triggers&gt;
  </code></pre>
  
  <p></p>