Search code examples
c#asp.netinline-code

ASP.NET content incode id


I recently started using Masterpages, the thing is I would like to add text in code to an asp:Content tag.

So my content page markup code is:

<%@ Page Language="C#" MasterPageFile="~/Template.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ASP_Test_WebApp.Default" %>
<asp:Content id="TEST" ContentPlaceHolderID="Main" Runat="Server" />

So now I would like to add Contents to the "TEST" id incode.

But my in code doesn't recognize TEST. If I don't use a masterpage and I give an id to a tag my in code reconigzes it, but now that I started using masterpages it doesn't.

What am I doing wrong?


Solution

  • Content tags don't have any UI on their own, you need to add controls inside them that you can then address in your code e.g.

    <%@ Page Language="C#" MasterPageFile="~/Template.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ASP_Test_WebApp.Default" %> 
    <asp:Content id="TEST" ContentPlaceHolderID="Main" Runat="Server" >
        <asp:label runat="server" id="MyLabel"/>
    </asp:content>
    
    public partial class Default: System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MyLabel.Text = "StackOverflow rocks!"
        }
    }