views:

62

answers:

1

I'm pretty new to web programming, reading a book on ASP.NET, and I notice the author does this;

<asp:ScriptManager ID="ScriptManager1" runat="server"/>

Instead of what Visual Studio does, which is this;

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

Is the first way of doing it an acceptable and multi-browser compatible shortcut?

+5  A: 

Either way is acceptable, and it doesn't affect browser output, because the asp:ScriptManager tag is processed on the server-side.

That being said, it's a matter of preference which style you use. There are some tags that have nested tags which require you to use the second style.

However, if that is not the case, then semantically, the first style is known as a self-closing element and has the same semantic meaning in ASP.NET as the second syntax.

I would recommending learning the distinction between server-side tags and client-side tags, because while similar in syntax, they can have very different results (for example, self-closing script tags in browsers typically don't work when using third-party libraries).

casperOne
Excellent, thank you.
cc0
I agree that self-closing tags are fine for tags processed server-side. However, if you are new to web programming, heed the fact that this is not true for client-side tags in all browsers. Some (IE7) require a separate closing tag.
DOK
@DOK <br /> for one works fine in IE7
Rune FS
@Rune FS, it works well on all browsers under all doctypes, but it is parsed as `<br> </>` when unrecognized. Try it with `<br/>` (notice no spaces) and see which browsers parse it correctly. Also, try something like `<script src="test.js"/>` to see which browsers parse that correctly.
voyager
@voyager Im not stating that all browsers parse valid HTML correctly just pointing out that the generality of DOKs statement isn't correct.
Rune FS