Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

asp.net

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:

  • Div doesn't contain the runat="server" property.

  • This is not the master page file but this is another file named "AssignImages.aspx" and it contains ContentPlaceHolder.

The div is inside this ContentPlaceHolder:

<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server" ID="Content1">  
like image 523
Manu Avatar asked Oct 09 '13 06:10

Manu


1 Answers

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>
like image 66
Carlos Landeras Avatar answered Oct 28 '22 21:10

Carlos Landeras