简体   繁体   中英

Why it's necessary the label asp using ASP.NET?

When I program in PHP, I don't modify the template, but when I use ASP.NET (C#), I must put the asp label for the fields I will use in the server. For exampe: <asp:Button ...> .

Why Microsoft implements this dirty solution? Theorically, the server will receive all POSTs/GETs from the client, with the asp label or not.

It is not necessary the usage of the asp.net controls. Actually, you have the opportunity to use html controls, when you don't need the asp.net controls. It depends solely, on what you control would hold and how you will use it. For instance, if you want to declare a label, with a constant text value, you could use an html label

<label>LabelName</label>

On the other hand if you want to declare a label, whose value will be change, when one or more events will be fired, then you have to use the asp.net controls

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

Then you can access in the server side the value of this label as labelID.Text .

Last but not least, you can update the value of an html label using javascript in the client side, when again one event or more events will be fired. In order to achiene this, you have to declare it like below:

<label id="labelId">LabelName</label>

Then using javascript

var label = document.getElementById("labelId"); 

you could select this label and you can access it's value as

label.innerHTML

or more easily using JQuery,

var label = $("#labelId");
var value = label.val();

So it depends on what you want to do. That will lead you to select the proper control. You don't have in any way to select all the time asp.net controls or hmlt controls.

Well using word like dirty is too much to say when you doesn't understand that what you are doing in ASP.NET. in PHP, you write HTML and manipulate output HTML from php code. In ASP.NET you write a code that write HTML afterwards. Basically your should read more about difference in two platform. To explain it a bit ASP.NET can be written using PHP.

ASP.NET provide asp tags, not just for label but for Grids as well, when you use them one liner tag you often get complex HTML as output, and these output HTML is modified based on browser settings. Also, ASP.NET have Standard HTML tags you are free to use them at will. Having ASP tag with runat=server you actually use them in Code Behind, if not, just do the dirty PHP trick to put inline PHP in HTML.

So, read about it, and you find it make sense to have that dirty solution .

You don't have to. you use asp prefix for asp.net specific controls. Check this:

<asp:Label Text="Test text" runat="server"/>

<span runat="server">Text=Test text</span>

Both will generate same markup, but Label has more properties which might be used(Localization one of the benifits)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM