Hello all, I am very very new to ASP.NET - just started looking into it a few days ago - and I got a book out to get the basics of APS.NET, Visual studio (which I haven't installed as yet) and C# (the book is ASP.NET 3.5 a beginners guide by William B Sanders). As I flicked through the first examples of code, I have noticed something and I am not sure whether it's me being silly, a mistake, or how things are. Let's have a look at a few of these examples.
1)this aspx file is part of a larger example to demonstrate variable types (which in this case, it is not my concern by the way):

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ConVar.aspx.cs"
Inherits="ConVar" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Constants and Variables</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Label ID="YourAge" runat="server" Text="19" />
            </div>
        </form>
    </body>
</html>

So, the asp tag seems to auto close but I swear I'd seen somewhere else that it has to be matched by an equivalent closing tag like </asp:Label>. Can anybody shed some light on this for me please?

2)Second one is far more interesting. Again it is part of a larger example to demonstrate basic comparison:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SimpleIf.aspx.cs"
Inherits="SimpleIf" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Simple Conditional</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="TextBox1" runat="server"/>
                Number 1
            <p/>
            <asp:TextBox ID="TextBox2" runat="server"/>
                Number 2
            <p/>
            <asp:Button ID="Compare" runat="server" Text="Compare" onclick="DoCompare" />
            <p/>
            </div>
        </form>
    </body>
</html>

Now, in this example there are </p> tags coming out of nowhere with no matching opening tag, and this left me rather puzzled: is it a mistake or is it the way things work in visual studio or ASP.NET?I trus tomebody can give me a hand to understand this
thanks

For the first question related to the label control, it looks correct to me. The label control doesn't require and end tag. Many asp.net controls only require a single tag. Others that contain addition controls within them will require a starting and ending tag.

With regard to your second example, <p/>, that looks incorrect. It's seems as if the book is representing a paragraph element. This has nothing to do with asp.net. I don't believe that the HTML would validate. Asp.net allows you to have both asp.net and HTML elements on the same page.

Thanks JorgeM, yes the p tag business looked wrong to me too, but being really new to ASP.NET and visual I thought I'd ask. So with regard to opening and closing tags in controls, presumably the IDE is clever enough to determine which one needs a closing tag and which not?

Yes, if you drag and drop the controls using visual studio, it will create them accordingly. If you create them manually some controls will allow you to create an opening and closing, while others require it. If there is a mistake with the control, you'll most defintely see an error like "malformed ... " at run time.

OK cool thanks. Other than relying on the IDE, is there a list detailing what tags need a closing tag and which ones don't? A bit like the HTML doc type

Yes. The source of all this information is on MSDN. Link to the web controls page.

You may find the documentation to be a bit overwhelming though, but it's the most extensive source as far as I know.

That's useful thanks, I had a quick look at it now. There is something strange though - OK perhaps because I am very new to asp.net - but tags like a textbox for the sake of argument is an auto close tag according to the MSDN library, and yet, I have seen instances of a textbox with a proper closing tag: is that a mistake? Visual didn't complain about it

Yes, i can understand the confusion. asp.net does accept some of the controls in both scenarios. For example, this is valid in asp.net and both render as an input element without error at runtime.

<asp:TextBox runat="server"></asp:TextBox>
<asp:TextBox runat="server" />

asp.net will not throw an error.

Ah, OK thanks for clarifying that, so I suppose there isn't a rule as such as to when asp.net accept what.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.