2
votes

I went through this thread but couldn't understand much. I am very new to ASP/HTML/Server-side programming.

I tried running this code on a .aspx file:

<form id="form1" action="Default.aspx">
    <div>
        <asp:Label ID="lblName"></asp:Label>

    </div>
</form>

And I got an error when I tried to use this in the CodeFile:

protected void Page_Load(object sender, EventArgs e)
    {
        lblName.Text = "123";
    }

"lblName does not exist".

But if I use runat="server" attribute with the label, then this code works.

Also, is there any concept of nesting the runat attribute. e.g, If I specify runat=server for the form above, will all my controls inside the form automatically be configured to run at server? How does this attribute work?

In which case would I be required to specify runat=server for the and for the tag? How does the server-side know that the label is inside the form if I don't have a form object at server side? Or am I missing something?

2

2 Answers

2
votes

Any element marked with runat="server" lets the framework know that this will be a control on the server side. This article has more details:

1
votes

Nope, there is no such nesting available in ASP.NET, you have to specify "runat" to every control that you want to use in code behind and that is part of ASP.NET web library.

Because ASP.NET can only recognize difference between client side tag (the html that runs on browser) and server side tag with help of "runat"