Question

I've created very simple web site for the test purposes. Only one master page and one content page.

My content page looks like this:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div id="TestDiv1">bla bla</div>
    <div id="TestDiv2">ble ble</div>
</asp:Content>

Now, based on some condition I would like to show/hide a given div. So I am trying to reach one of those divs by Controls collection, like this:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      ContentPlaceHolder myContent = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
      myContent.FindControl("TestDiv1").Visible = false; //this is not working 
    }
}

    }
}

But the above example is not working. None of the two div control exists in the myContent.Controls collection. If I place for example a TextBox on my content page, I can reach it through Controls.

So what should I do to be able to access the div control?

Was it helpful?

Solution

your divs are HTMLcontrols, try to add them the tag runat="server"

<div id="TestDiv1" runat="server">bla bla</div>

That should solve your issue.

OTHER TIPS

You need to set runat="server" on the divs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top