How to hide a div from code (c#)

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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;
}

Method 2

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.

Method 3

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

Method 4

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;
}

Method 5

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;
    }
}

Method 6

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;

Method 7

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;
    }

Method 8

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>

Method 9

In code behind:

{
    yourDiv.Visible = false;
}

Method 10

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;


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x