How to hide a div from code (c#)

C#asp.net

C# Problem Overview


I have a div element on my page that I wish to show/hide based on a session value in my code-behind. How can I do this?

C# Solutions


Solution 1 - C#

Give the div "runat="server" and an id and you can reference it in your code behind.

<div runat="server" id="theDiv">

In code behind:

{
    theDiv.Visible = false;
}

Solution 2 - C#

if your div has the runat set to server, you surely can do a myDiv.Visible = false in your Page_PreRender event for example.

if you need help on using the session, have a look in msdn, it's very easy.

Solution 3 - C#

one fast and simple way is to make the div as

<div runat="server" id="MyDiv"></div>

and on code behind you set MyDiv.Visible=false

Solution 4 - C#

In the Html

<div id="AssignUniqueId" runat="server">.....BLAH......<div/>

In the code

public void Page_Load(object source, Event Args e)
{

   if(Session["Something"] == "ShowDiv")
      AssignUniqueId.Visible = true;
    else
      AssignUniqueID.Visible = false;
}

Solution 5 - C#

Try this. Your markup:

<div id="MyId" runat="server">some content</div>

.. and in aspx.cs file:

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["someSessionVal"].ToString() == "some value")
    {
        MyId.Visible = true;
    }
    else
    {
        MyId.Visible = false;
    }
}

Solution 6 - C#

Give the div "runat="server" and an id and you can reference it in your code behind.

<div runat="server" id="theDiv">

In code behind:

{
    theDiv.Visible = false;
}

In Designer.cs page:

 protected global::System.Web.UI.HtmlControls.HtmlGenericControl theDiv;

Solution 7 - C#

work with you apply runat="server" in your div section...

<div runat="server" id="hideid">

On your button click event:

 protected void btnSubmit_Click(object sender, EventArgs e)
    {
      hideid.Visible = false;
    }

Solution 8 - C#

u can also try from yours design

    <div <%=If(True = True, "style='display: none;'", "")%> >True</div>
<div <%=If(True = False, "style='display: none;'", "")%> >False</div>
<div <%=If(Session.Item("NameExist") IsNot Nothing, "style='display: none;'", "")%> >NameExist</div>
<div <%=If(Session.Item("NameNotExist") IsNot Nothing, "style='display: none;'", "")%> >NameNotExist</div>

Output html

    <div style='display: none;' > True</div>
<div  >False</div>
<div style='display: none;' >NameExist</div>
<div  >NameNotExist</div>

Solution 9 - C#

In code behind:

{
    yourDiv.Visible = false;
}

Solution 10 - C#

The above answers are fine but I would add to be sure the div is defined in the designer.cs file. This doesn't always happen when adding a div to the .aspx file. Not sure why but there are threads concerning this issue in this forum. Eg:

protected global::System.Web.UI.HtmlControls.HtmlGenericControl theDiv;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnjanaView Question on Stackoverflow
Solution 1 - C#BazzzView Answer on Stackoverflow
Solution 2 - C#Davide PirasView Answer on Stackoverflow
Solution 3 - C#AristosView Answer on Stackoverflow
Solution 4 - C#D-BarView Answer on Stackoverflow
Solution 5 - C#Mark RobinsonView Answer on Stackoverflow
Solution 6 - C#user2922221View Answer on Stackoverflow
Solution 7 - C#Pratham4950View Answer on Stackoverflow
Solution 8 - C#Norbert ZiemniakView Answer on Stackoverflow
Solution 9 - C#Abdus Salam AzadView Answer on Stackoverflow
Solution 10 - C#Gary HuckaboneView Answer on Stackoverflow