Why do I get compile error when I skip the ending slash in the asp.net markup file

  • Thread starter Thread starter tony.johansson
  • Start date Start date
T

tony.johansson

Hello!

Below is two files copied the first one is the aspx markup file and the last
one in the code behind .cs file
All this works well.
But if I for example remove the ending / in the row below I get compile
error.
<asp:CheckBox ID="chkPicture" runat="server" Text="Add the default Picture"
/>

I have asked this question before and you have answered that the C# compiler
doesn't care about the HTML syntax.
So I hope to get an answer to my question.

<!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>Greeting Card Maker</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<!-- Here are the controls_ -->
Choose a background color: <br />
<asp:DropDownList ID="lstBackColor" runat="server" Width="194px"
Height="22px" />
<br /> <br />
Choose a font: <br />
<asp:DropDownList ID="lstFontName" runat="server" Width="194px"
Height="22px" />
<br /> <br />
Specify a numeric font size: <br />
<asp:TextBox ID="txtFontSize" runat="server" />
<br /> <br />
Choose a border style: <br />
<asp:RadioButtonList ID="lstBorder" runat="server" Width="177px"
Height="59px" />
<br /> <br />
<asp:CheckBox ID="chkPicture" runat="server" Text="Add the default
Picture" />
<br /> <br />
Enter the greeting text below: <br />
<asp:TextBox ID="txtGreeting" runat="server" Width="240px"
Height="85px" TextMode="MultiLine" />
<br /> <br />
<asp:Button ID="cmdUpdate" onclick="cmdUpdate_Click" runat="server"
Width="71px" Height="24px" Text="Update" />
</div>

<asp:Panel ID="pnlCard" runat="server" Height="481px"
HorizontalAlign="Center"
Width="339px" BorderStyle="None">
<br />&nbsp;
<asp:Label ID="lblGreeting" runat="server" Text="Label"
Width="256px" Height="150px" />
<br /> <br /> <br />
<asp:Image ID="imgDefault" runat="server" Width="212px"
Height="160px" />
</asp:Panel>
</form>
</body>
</html>

//Here is the code behind file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lstBackColor.Items.Add("White");
lstBackColor.Items.Add("Red");
lstBackColor.Items.Add("Green");
lstBackColor.Items.Add("Blue");
lstBackColor.Items.Add("Yellow");

lstFontName.Items.Add("Times New Roman");
lstFontName.Items.Add("Arial");
lstFontName.Items.Add("Verdena");
lstFontName.Items.Add("Tohoma");

ListItem item = new ListItem();
item.Text = BorderStyle.None.ToString();
item.Value = ((int)BorderStyle.None).ToString();
lstBorder.Items.Add(item);

item = new ListItem();
item.Text = BorderStyle.Double.ToString();
item.Value = ((int)BorderStyle.Double).ToString();
lstBorder.Items.Add(item);

item = new ListItem();
item.Text = BorderStyle.Solid.ToString();
item.Value = ((int)BorderStyle.Solid).ToString();
lstBorder.Items.Add(item);

lstBorder.SelectedIndex = 0;
imgDefault.ImageUrl = "Desert.jpg";
}
}
protected void cmdUpdate_Click(object sender, EventArgs e)
{
pnlCard.BackColor = Color.FromName(lstBackColor.SelectedItem.Text);
lblGreeting.Font.Name = lstFontName.SelectedItem.Text;

if (Int32.Parse(txtFontSize.Text) > 0)
{
lblGreeting.Font.Size =
FontUnit.Point(Int32.Parse(txtFontSize.Text));
}

int borderValue = Int32.Parse(lstBorder.SelectedItem.Value);
pnlCard.BorderStyle = (BorderStyle)borderValue;



if (chkPicture.Checked)
imgDefault.Visible = true;
else
imgDefault.Visible = false;

lblGreeting.Text = txtGreeting.Text;
}
}
 
Hello!

Below is two files copied the first one is the aspx markup file and the last
one in the code behind .cs file
All this works well.
But if I for example remove the ending / in the row below I get compile
error.
<asp:CheckBox ID="chkPicture" runat="server" Text="Add the default Picture"
/>

I have asked this question before and you have answered that the C# compiler
doesn't care about the HTML syntax.

A parser error will occur if the ASP.NET markup is not well-formed.
Removing the slash from the closing tag in ASP.NET markup will cause a
parser error

<asp:TextBox runat="server" id="txt" >

Removing the slash from the closing tag in HTML markup will not cause
a parser error.

<input id="txt" type="text" >

Your parser error is not occuring because there is an error in the
HTML markup. The parser error occurs because there is an error in the
ASP.NET markup.

regards
A.G.
 
Good explined!!

//Tony

"Registered User" skrev i meddelandet

Hello!

Below is two files copied the first one is the aspx markup file and the
last
one in the code behind .cs file
All this works well.
But if I for example remove the ending / in the row below I get compile
error.
<asp:CheckBox ID="chkPicture" runat="server" Text="Add the default Picture"
/>

I have asked this question before and you have answered that the C#
compiler
doesn't care about the HTML syntax.

A parser error will occur if the ASP.NET markup is not well-formed.
Removing the slash from the closing tag in ASP.NET markup will cause a
parser error

<asp:TextBox runat="server" id="txt" >

Removing the slash from the closing tag in HTML markup will not cause
a parser error.

<input id="txt" type="text" >

Your parser error is not occuring because there is an error in the
HTML markup. The parser error occurs because there is an error in the
ASP.NET markup.

regards
A.G.
 
The line you are talking about (<asp:CheckBox ID="chkPicture" runat="server" Text="Add the default Picture" />) is NOT an html tag, but an asp.net server tag.

Most browsers are not that picky about the well-formedness of html tags. But according to W3C standards, we should close our html tags properly with the fortward slash such as <br /> and <img ..... />.
 
Below is two files copied the first one is the aspx markup file and the
last one in the code behind .cs file
All this works well.
But if I for example remove the ending / in the row below I get compile
error.
<asp:CheckBox ID="chkPicture" runat="server" Text="Add the default
Picture" />

I have asked this question before and you have answered that the C#
compiler doesn't care about the HTML syntax.
So I hope to get an answer to my question.

Since the above is not HTML then ...

Arne
 
The line you are talking about (<asp:CheckBox ID="chkPicture" runat="server" Text="Add the default Picture" />) is NOT an html tag, but an asp.net server tag.

Most browsers are not that picky about the well-formedness of html tags. But according to W3C standards, we should close our html tags properly with the fortward slash such as<br /> and<img ..... />.

If it is XHTML.

<something /> in HTML (at least in pre-5) is very much not a recommendation.

Arne
 
Arne Vajhøj said:
If it is XHTML.

<something /> in HTML (at least in pre-5) is very much not a
recommendation.

Huh? They've been recommending that for YEARS.
 
Huh? They've been recommending that for YEARS.

Where?

HTML pre-5 is SGML and "<something />" in HTML actually means
the same as "<something />>" in XHTML.

Most HTML renderers are very forgiving and try to guess
the authors intent instead of applying the SGML rules, so
it may work, but it is not the standard.

Arne
 
Back
Top