2

I wonder, why do I have to include runat="server" to server elements in ASP.NET when runat only has one option and it is required for server elements. If not added it keeps telling me "missing a required attribute 'runat'".

Am I missing something here?

2 Answers2

1

The runat="server" is there to let ASP.NET know which parts of your HTML is controlled server-side and which parts are not.

Please note that even standard HTML elements like <table> can have a runat="server", which then exposes it to your code.

Timothy Walters
  • 16,866
  • 2
  • 41
  • 49
  • I understand that, but server controls MUST have it, then why add it? it should be by default and no need to keep nagging me about it. For HTML as it is optional, it makes sense.. –  Jul 16 '13 at 01:44
  • Except all those server controls are entirely valid HTML. They may or may not be known element names, but how else do you expect ASP.NET to know? The `runat` directive is how ASP.NET can quickly and easily find all elements that it needs to parse/evaluate/replace. – Timothy Walters Jul 18 '13 at 02:04
0

Here is a previous StackOverflow question that has input from some people at Microsoft as to why the runat="server" tag is explicitly necessary for server tags, while causing errors if omitted.

Why does ASP.NET webforms need the Runat=“Server” attribute?

Community
  • 1
  • 1
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80