I have a div element:
I want to control this element from the code-behind page an
Give ID
and attribute runat='server'
as :
<div class="tab-pane active" id="portlet_tab1" runat="server">
//somecode Codebehind:
Access at code behind
Control Test = Page.FindControl("portlet_tab1");
Test.Style.Add("display", "none");
or
portlet_tab1.Style.Add("display", "none");
You have make div as server control using following code,
<div class="tab-pane active" id="portlet_tab1" runat="server">
then this div will be accessible in code behind.
id + runat="server" leads to accessible at the server
If you want to find the control from code behind you have to use runat="server"
attribute on control. And then you can use Control.FindControl
.
<div class="tab-pane active" id="portlet_tab1" runat="server">
Control myControl1 = FindControl("portlet_tab1");
if(myControl1!=null)
{
//do stuff
}
If you use runat server and your control is inside the ContentPlaceHolder
you have to know the ctrl name would not be portlet_tab1 anymore. It will render with the ctrl00 format.
Something like: #ctl00_ContentPlaceHolderMain_portlet_tab1. You will have to modify name if you use jquery.
You can also do it using jQuery on client side without using the runat-server attribute:
<script type='text/javascript'>
$("#portlet_tab1").removeClass("Active");
</script>
@CarlosLanderas is correct depending on where you've placed the DIV control. The DIV by the way is not technically an ASP control, which is why you cannot find it directly like other controls. But the best way around this is to turn it into an ASP control.
Use asp:Panel instead. It is rendered into a <div>
tag anyway...
<asp:Panel id="divSubmitted" runat="server" style="text-align:center" visible="false">
<asp:Label ID="labSubmitted" runat="server" Text="Roll Call Submitted"></asp:Label>
</asp:Panel>
And in code behind, simply find the Panel control as per normal...
Panel DivCtl1 = (Panel)gvRollCall.FooterRow.FindControl("divSubmitted");
if (DivCtl1 != null)
DivCtl1.Visible = true;
Please note that I've used FooterRow, as my "psuedo div" is inside the footer row of a Gridview control.
Good coding!
you'll need to cast it to an HtmlControl in order to access the Style property.
HtmlControl control = (HtmlControl)Page.FindControl("portlet_tab1"); control.Style.Add("display","none");