Anjana
Anjana

Reputation: 1453

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?

Upvotes: 78

Views: 278630

Answers (11)

Prasad Kothe
Prasad Kothe

Reputation: 1

It can be hidden using jquery. The list to be hidden/shown can be retrieved from the MVC controller/code-behind using an ajax call.

Upvotes: -1

Gary Huckabone
Gary Huckabone

Reputation: 412

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;

Upvotes: 1

Norbert Ziemniak
Norbert Ziemniak

Reputation: 478

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>

Upvotes: 2

user2922221
user2922221

Reputation:

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;

Upvotes: 5

Abdus Salam Azad
Abdus Salam Azad

Reputation: 5502

In code behind:

{
    yourDiv.Visible = false;
}

Upvotes: 0

Pratham4950
Pratham4950

Reputation: 93

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

Upvotes: 3

Mark Robinson
Mark Robinson

Reputation: 13278

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

Upvotes: 7

Bazzz
Bazzz

Reputation: 26937

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

Upvotes: 202

Aristos
Aristos

Reputation: 66641

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

Upvotes: 9

D-Bar
D-Bar

Reputation: 185

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

Upvotes: 8

Davide Piras
Davide Piras

Reputation: 44595

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.

Upvotes: 12

Related Questions