Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET closing tag

When I use autocompletion in VisualStudio 2010 within my .aspx application, there are different default completions at closing control tags:

<asp:CheckBox /> 
<asp:Label></asp:Label>

Is there a explaination for this behaviour?

<asp:CheckBox></asp:CheckBox>
<asp:Label />

Wouldn't be invalid.

like image 543
Impostor Avatar asked Jun 14 '16 10:06

Impostor


2 Answers

This is because ASP.NET's Label control is decorated with the ParseChildrenAttribute, with ParseChildren(false) while CheckBox isn't.

You can support the same behavior whith your custom controls, for example, with the following code, Visual Studio will behave like Label if you use MyControl in the web form editor:

[ParseChildren(false)]
public class MyControl : WebControl
{
    ...
}
like image 145
Simon Mourier Avatar answered Sep 20 '22 08:09

Simon Mourier


The label closing is like that

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

because usually we type something between

<asp:Label runat="server" ID="lblOne">better start programming now</asp:Label>

that is not the case for checkbox, that we type inside of it

<asp:CheckBox runat="server" Text="enable" ID="cbOne" />

We have on both elements the Text field, why on the one we prefer to write out side... Look at this example, on Label, or On other similar controls the text that we may have to write may include characters that are not allowed inside the Text Property, maybe a complex css style or what ever... The check box from the other side is only include a small text (yes, not, something like that)

<asp:Label ID="lblLabel" runat="server">
    This is a <b>"label"</b>
    <br />And one more line
</asp:Label>

and more example that compiles

<asp:Label ID="lblLabel" runat="server">
    This is a <b>"label"</b>
    <br />And one more line
    <asp:Literal runat="server" ID="ltrOneMore">One more Control Inside</asp:Literal>
</asp:Label>


---but this is not compile--

<asp:Label ID="lblLabel2" runat="server" 
    Text="This is a <b>"label"</b>
    <br /> and one more line" 
    />

At the final end is a decision that the makes make - maybe we need to ask them for the real real reason.

Now this is also not compile

<asp:CheckBox runat="server" ID="cbMyChbx">one<asp:CheckBox>

check box when is render on page is use two controls, one input and one label, so they maybe need to help user to understand that the text is not going on the input control.

like image 39
Aristos Avatar answered Sep 21 '22 08:09

Aristos