How to hide a div from code (c#)

前端 未结 9 1549
迷失自我
迷失自我 2020-12-01 03:38

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?

相关标签:
9条回答
  • 2020-12-01 03:51

    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.

    0 讨论(0)
  • 2020-12-01 03:52

    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;
    
    0 讨论(0)
  • 2020-12-01 03:54

    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>
    
    0 讨论(0)
  • 2020-12-01 03:56

    In code behind:

    {
        yourDiv.Visible = false;
    }
    
    0 讨论(0)
  • 2020-12-01 04:04

    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;
    }
    
    0 讨论(0)
  • 2020-12-01 04:12

    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;
    }
    
    0 讨论(0)
提交回复
热议问题