Domanda

Situation: I have been creating webpages in HTML5/CSS3 & Javascript using Sublime 2 text editor for a year, however a college course now requires me to use Asp.Net and Visual Studio 2010. I do not use the designer because I am proficient at doing things by hand, however I find that writing asp: inside every element is time consuming and causes syntax errors when applied to some HTML 5 tags and not others.

Example HTML 5: <button id="btn" type="submit" value="Button"/>

Example Asp.net: <asp:Button ID="Button1" runat="server" Text="Button" />

Question: Can the asp: portion be omitted without effecting anything or is it required for IIS or the C# back-end functionality? What about runat="server" can that be omitted? Google has come up dry regarding my inquiry, so any help is appreciated.

È stato utile?

Soluzione

you simply cannot remove either of the two but hear me out why, because I have a feeling you are not familiar with ASP and therefor are mistaking the meaning of the asp: and the runat="server" syntax.

first: runat="server" this property on an element, tells the the compiler that this is actually a server side control so a <button/> is not the same as an <button runat="server"/> the first one is pure html, while the second one is a control, which can be bound to on the server side. .Net will give it a clientID (not to be mistaken by the ID you have to give it yourself).

second: asp: this is a prefix, on certain elements, that tells the compiler these are ASP controls (the default controls given by the ASP.net framework). These include Buttons, TextBoxes, DropDownLists, ... do not mistake 1 of these with a html element.

an <asp:Button id="myAspButton" runat="server"/> is not the same as a <button id="myHtmlButton"/>

the first, is a server side control, which can be bound to (see it's runat="server" attribute), and this control renders to the browser as a <input type="submit"/> for example.

you could alter the rendering of the asp.net button class to make it return something entirely differnt if you wish.

and you are also not limited to using asp.net classes. you can create your own controls, and put them in a custom created library you could give those your own prefix.

if I created such a custom control, I could register a prefix for it in the web.config file, and thus I could create a custom button extending from the original one (but with a default label in front...

<myc:CustomButton ID="myButton" Text="myButton" Label="myLabel" runat="server"/>

which could render into:

<label>myLabel</label>
<button ID="*******">myButton</button>

the asterisks are symbolizing the Unique ID it will get from the .net framework

if you want to know more on custom controls, or extending default controls here is a step by step explanation to create custom controls, or extend from a TextBox control. it also shows how you add a custom prefix for your controls (in the this case 'cc') you can find more info here

Altri suggerimenti

The runat="server" part is required to tell .NET that it will have to render a button there (which will contain .NET specific ID for processing upon POST). Not too familiar with web forms (I started with MVC), but I would assume that the asp: part is to help distinguish between server controls and standard HTML markup.

Why not try removing it and if it breaks something, then you know it's needed. For instance if the button doesn't show up after removing it, then obviously the .NET markup parser needs it to be there in order to know that it is a place holder for a server control.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top