简体   繁体   中英

How to hide User Control div from aspx page?

In my user control i have below controls :

.ascx Page:

<div id="div1" runat="server">
     <asp:Label ID="Lblname" Text="Name" runat="server"></asp:Label>
</div>
<div id="div2" runat="server">
    <asp:TextBox ID="txtvalue" runat="server"></asp:TextBox>
</div>
<div id="div3" runat="server">
    <asp:Button ID="btnClick" runat="server" Text="Click" />
</div>

aspx page How to hide div1, div2 & div3 using c# or javascript

There is no need of javascript. You can easily put an id="YourId" and runat="server" . You can also add a Visible=False as default (or the other way if you want it to be visible as default). In .cs page, you can define and change the Visible value as you wish. So let's say you want it visible as a default only for administrator users, and you check in database if the user is an administrator with a true/false value (a bit value). then in PageLoad() you can perform a check like this:

if(LoggedUser.IsAdmin==true)
{
div1.Visible=true;
}
else 
{
div1.Visible=false;
}

You page renders html from your user controls as well as from master pages.

If you want to hide your elements then simply use this

$("#YouHTMLElement").attr("style","display:none");

One important thing: you need to specify clientidmode="static" on your divs because IDs of server controls changes when your html renders.

Updated: In C#, you need to do this

  div1.Style.Add("display", "none");

AC# implementation

You should be able to reference your divs by their IDs in the codebehind since you have runat set to server .

So, assuming you want this to occur on the page loading, to your Page_Load event, add:

div2.Visible = false;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM