Manu
Manu

Reputation: 418

How to select a div element in the code-behind page?

I have a div element:

<div class="tab-pane active" id="portlet_tab1">

I want to control this element from the code-behind page and remove the class "active"

NOTE:

The div is inside this ContentPlaceHolder:

<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server" ID="Content1">  

Upvotes: 15

Views: 145319

Answers (6)

Hudson
Hudson

Reputation: 869

id + runat="server" leads to accessible at the server

Upvotes: 1

Fandango68
Fandango68

Reputation: 4868

@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!

Upvotes: 3

JJ_Coder4Hire
JJ_Coder4Hire

Reputation: 4891

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");

Upvotes: 9

Carlos Landeras
Carlos Landeras

Reputation: 11063

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>

Upvotes: 27

Deep Sharma
Deep Sharma

Reputation: 3473

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"); 

Upvotes: -1

Rajesh Subramanian
Rajesh Subramanian

Reputation: 6490

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.

Upvotes: 1

Related Questions