jre247
jre247

Reputation: 1887

how to show hidden div from codebehind c#

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:

{InnerText = '((System.Web.UI.HtmlControls.HtmlContainerControl)(divGrid)).InnerText' threw an exception of type 'System.Web.HttpException'}

Here's the code I'm using that's causing me this error:

markup:

<div id="divGrid" runat="server">
    <uc1:ContactsGridUserControl ID="ContactsGridUserControl1" runat="server" UpdateMode="Conditional"/>      
</div> 

<asp:Button ID="btnSearch" runat="server" Text="Search" Font-Size="Small" Height="30px" 
                Width="67px" ClientIDMode="Static" onclick="btnSearch_Click"></asp:Button>

code behind:

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 && txtbxFirstName.Text.Length > 0) ||
            (txtbxLastName.Text != null && txtbxLastName.Text.Length > 0) ||
            (txtbxPhoneNum.Text != null && txtbxPhoneNum.Text.Length > 0) ||
            (txtbxCity.Text != null && txtbxCity.Text.Length > 0) ||
            (txtbxZipCode.Text != null && txtbxZipCode.Text.Length > 0) ||
            (txtDate1.Text != null && txtDate1.Text.Length > 0) ||
            (txtDate2.Text != null && txtDate2.Text.Length > 0) ||
            (txtbxCompanyName.Text != null && txtbxCompanyName.Text.Length > 0))
        {
            return true;

        }
        else
        {
            return false;
        }
    }
    catch (Exception ex)
    {
        lblError.Text = "Exception: " + ex.ToString();
        return false;
    }
}

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)

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
System.Web.dll!System.Web.UI.WebControls.Button.RaisePostBackEvent(string eventArgument) + 0x71 bytes
System.Web.dll!System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(string eventArgument) + 0xb bytes
System.Web.dll!System.Web.UI.Page.RaisePostBackEvent(System.Web.UI.IPostBackEventHandler sourceControl, string eventArgument) + 0xe bytes
System.Web.dll!System.Web.UI.Page.RaisePostBackEvent(System.Collections.Specialized.NameValueCollection postData) + 0x25 bytes
System.Web.dll!System.Web.UI.Page.ProcessRequestMain(bool includeStagesBeforeAsyncPoint, bool includeStagesAfterAsyncPoint) + 0x15bc bytes
System.Web.dll!System.Web.UI.Page.ProcessRequest(bool includeStagesBeforeAsyncPoint, bool includeStagesAfterAsyncPoint) + 0x8d bytes
System.Web.dll!System.Web.UI.Page.ProcessRequest() + 0x4f bytes System.Web.dll!System.Web.UI.Page.ProcessRequestWithNoAssert(System.Web.HttpContext context) + 0x16 bytes
System.Web.dll!System.Web.UI.Page.ProcessRequest(System.Web.HttpContext context) + 0x32 bytes
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
System.Web.dll!System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(System.Web.HttpContext context, System.AsyncCallback cb, object extraData) + 0xf8 bytes
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
System.Web.dll!System.Web.HttpRuntime.ProcessRequest(System.Web.HttpWorkerRequest wr) + 0x47 bytes
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
[Appdomain Transition]
WebDev.WebHost40.dll!Microsoft.VisualStudio.WebHost.Server.OnSocketAccept(object acceptedSocket) + 0x83 bytes
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
mscorlib.dll!System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() + 0x5a bytes mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() + 0x147 bytes
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() + 0x2d bytes
[Native to Managed Transition]

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.

updated code:

<asp:UpdatePanel ID="UpdatePanel2" runat="server">
    <ContentTemplate>
        <asp:Panel ID="pnlGrid" Visible="false" runat="server">   
            <uc1:ContactsGridUserControl ID="ContactsGridUserControl1" runat="server" UpdateMode="Conditional"/>
        </asp:Panel>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="btnSearch" EventName="Click" />
    </Triggers>

Upvotes: 2

Views: 42175

Answers (4)

I actually encounter this problem. I used divControl.Visible = false; on Page_Load event, it was solved buy mystically that code distrupts my page format. So at last resort i tried divControl.Style.Add("visibility", "hidden"); on Page_Load event divControl.Style.Add("visibility", "visible"); on a desired method it is solved and not distrupt page format.

Upvotes: 1

jre247
jre247

Reputation: 1887

I figured out the answer to my question. I had to surround the user control with an update panel and set the trigger to a click event of the Search button. I also surrounded the usercontrol with a panel and simply set visible to true/false.

Upvotes: 1

Mike Marshall
Mike Marshall

Reputation: 7850

I believe the base class for all ASP.NET WebForms controls is HtmlControl which has a Visible property. Would this work?:

divGrid.Visible = true; 

Upvotes: 4

Austin Thompson
Austin Thompson

Reputation: 2281

If I remember right I had a similar problem a while back and it was due to processing at some point not working if the control was not visible at page load (or some point at startup). I don't quite remember the details right now.

Ideas to work around:

  • use jquery to hide it at document ready
  • make the position of the element off screen when "hidden" and move it back to where it goes when you want to display it

If none of that works please post the full stack trace.

Upvotes: 0

Related Questions