ASP.NET - How to get to the Div control in ContentPlaceHolder?

I created a very simple website for testing. Only one main page and one content page.

My content page is as follows:

<%@ 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 this div. Therefore, I am trying to get to one of these divs using the Controls collection, for example:

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 does not work. None of the two div controls exist in the collection myContent.Controls. If I put, for example, a TextBox on my content page, I can contact it through the controls.

So what should I do to have access to the div control?

+3
2

divs HTMLcontrols, runat = "server"

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

.

+11

runat="server" divs.

+4

Source: https://habr.com/ru/post/1774495/


All Articles