Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access asp:content from code behind

Ok, I am an experienced web developer but sometimes ASP.Net is tricking me. I have a master page in asp.net. Then I have a page based on that master page (home.aspx). Now in home.aspx.cs I want to access the asp:content controls to add controls programmatically.

Aspx looks like this:

<asp:Content ID="leftCol" ContentPlaceHolderID="cphLeftCol" Runat="Server">
  <asp:PlaceHolder ID="phLeftCol" runat="server">
  </asp:PlaceHolder>
</asp:Content>

I would expect that I can reference "leftCol" from my code behind. But it's unknown there. For testing I added my own placeholder "phLeftCol". I can reference that without issues.

Is there something I don't see?

like image 806
newtogit Avatar asked Sep 29 '10 19:09

newtogit


1 Answers

You can't access the asp:Content control directly from your code behind. A content control is not added to the control heirarchy at runtime so it is not accessible from the code behind to add controls to at runtime. To add controls to it at runtime, you need to add another container control to the content control and add the controls to that (as you did with the placeholder control).

See this MSDN article for more information.

like image 195
ben f. Avatar answered Nov 16 '22 02:11

ben f.