X is missing a required attribute 'runat', but runat has one option only - asp.net

I wonder, why do I have to include runat="server" to server elements in ASP.NET when runat only has one option and it is required for server elements. If not added it keeps telling me "missing a required attribute 'runat'".
Am I missing something here?

The runat="server" is there to let ASP.NET know which parts of your HTML is controlled server-side and which parts are not.
Please note that even standard HTML elements like <table> can have a runat="server", which then exposes it to your code.

Here is a previous StackOverflow question that has input from some people at Microsoft as to why the runat="server" tag is explicitly necessary for server tags, while causing errors if omitted.
Why does ASP.NET webforms need the Runat=“Server” attribute?

Related

When working in Visual Studio, can the ' asp: ' portion of an element be omitted?

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.
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
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.

Is this asp compiled somehow?

I have an aspx document (I know nothing about asp, .net, aspx, nada). It is a normal html table structure for the most part, but there are strings of asp that seem to be inserting some sort of dynamic content. They are in the form:
<asp:Image ID="imgTopImage" runat="server" ImageUrl="~/Images/topbar.jpg" />
<asp:Label ID="lblStyleCaption" runat="server" CssClass="label_caption" Text="Theme: " Visible="false" />
<asp:DropDownList ID="dropStyles" Width="150" runat="server" AutoPostBack="true" />
It seems that whenever I delete one of these——something as innocuous as, say, the line with the asp:Image tag, which I would think should just remove the image, when I load the page I get run-time errors. It's very particular. My question is, is this compiled somehow, which is making it so fragile. Even just changing the topbar.jpg to something.png gives me an error. Do I need to track down the original files this was compiled from, or is this normal server-side asp(x?) that I'm just somehow else goofing up my changes to?
ASPX pages are compiled, and those tags refer to objects that are known to the server, so removing them could cause errors.
First, some basics in layman's terms
Tags that begin with ASP: (Example, <ASP:Button id="btnSubmit" runat="Server" Text="Click Me" />)
are not standard html buttons. They are server controls. When generating the html that goes out to the browser, the ASP.NET runtime looks at the server controls and creates the appropriate content depending on the browser visiting the page.
In the case of the Button control, it's usually a standard html button, but the runtime also generates the JavaScript and such to handle the button's server-side click event.
Why you're probably seeing errors when you remove a control:
Quite often, there's server-side code that's written that accesses these controls. For example, the developer may have decided to change the Text or the Visible property due to some event.
If this is the case, and you remove the <asp:Button> tag, then there will be server-side code that references an object that no longer exists in the aspx page, hence the errors.
More at these links on Server Controls:
http://www.w3schools.com/aspnet/aspnet_controls.asp
(Actually, this older one is better for a new-to-asp.net developer: http://msdn.microsoft.com/en-us/library/zsyt68f1(VS.71).aspx
http://support.microsoft.com/kb/306459
I'd also recommend taking some time watching basic videos or going through the tutorials at http://www.asp.net/get-started
I just noticed this in your question:
Even just changing the topbar.jpg to something.png gives me an error.
That is a bit odd, but I know of at least one way it could happen...
Generally, Visual Studio will give you a warning (and not an error) if you include a relative URL to an image or a linked page that doesn't exist. The warning shouldn't block you from compiling. However, Visual Studio does have a setting that tells it to treat warnings as errors. That will block it from compiling. Here's how that would be set up:
from Project Settings> Configuration Properties select the build
setting and change the “treat warnings as errors” settings to true.
If you wish to NOT treat warnings as errors, simply change the setting to false.

Globalization difference in inline tags in ASP.NET

Is there any advantage/downfalls between using the inline write tag instead of the resource tag? Example:
<%=Resources.Site.SampleString %>
The resources tag (expression tag) as seen in any MSDN example:
<asp:Literal id="Literal1" runat="server" text="<%$ Resources:Site, SampleString %>" />
I find the first option far easier to use, and it has IntelliSense, but maybe it won't function the same?
These methods will function exactly the same. The latter simply calls first one; there is a reason why strongly-typed resources access code is being generated in the background. Therefore you can use whatever method you please.
By the way, there is also another way - by using meta:resourcekey attribute. So you should be able to write:
<asp:Literal id="Literal1" runat="server"
meta:resourcekey="SampleString" text="Default one" />
and it all should work exactly the same.
EDIT on implicit Localization.
What I forgot to mention, is that with meta:resourcekey certain conditions have to be met. The conditions are:
Values are taken from App_LocalResources, therefore related resource file need to exist
Related resource file name must be pagename.resx, for example: Default.aspx.resx, Default.aspx.es.resx
The resource file must contain keys in form of resourcekey.propertyname, for example SampleString.Text, SampleString.ID (although I wouldn't localize control ID's)
Because of how resources are generated, the key mentioned above must exist in invariant resource file (Default.aspx.resx), or it won't be localized.
I realised after some time that the <%=Resources.Site.SampleString %> method does not have designer support (which is understandable). This doesn't bother me, but it is a difference (for future readers).
So if you need or want designer support, the second option would be necessary.

Attribute "runat" is not a valid attribute. Did you mean "content" or "target"? How to solve this asp.net validation error

see here this error - http://validator.w3.org/check?verbose=1&uri=http%3A%2F%2Fwww.sitecore.net%2F
runat="server" is asp.net server control syntax. It must not come in html. It is interpreted by ASP.NET. you must remove this attribute.
Possible Reason for this:
1. I think the template is dynamically created. the developer make static site and cut copy paste on server side to make it dynamic but use control as response.write and forgot to remove runat="server" because it must be html content in response.write.
NOTE: No ASP.NET server control gives runat="server" in HTML. It is hardcoded in your code. remove this from both anchor and image tag.
Er... remove the attributes? They aren't valid HTML, and they're only meaningful when interpreted by ASP.NET.
Your pages should not be rendered with runat="server", so something's definitely going wrong here. What does the part of your aspx look like, that corresponds to one of the elements that is giving this validation error?

Label Text Property and entities

The following asp label fails to be displayed in the browser, can someone please
tell me what I am doing wrong. I expect to see the value <abc> but instead
I get nothing.
<asp:Label ID="Label1" runat="server" Text="<abc>"></asp:Label>
By the way, I realize that I can accomplish the same thing doing the following:
<asp:label id="Message1" runat="server"> <abc> </asp:Label>
But that is not really what I am asking for, what I would like to know is if using a string such as "<abc>" in an attribute value for an asp elements is allowed or not. In other words, is this an ASP.Net bug or is this behavior by design and if it’s by design what’s the reason for such design?
Thank you very much.
Believe it or not, but you can include entities without escaping them, thus:
<asp:Label runat="server" ID="myLabel" Text="<abc>" />
This will render an <abc> tag.
Edit: OK, sorry, you want to display the brackets, not make a tag, of course..
Using entity references in the Text attribute will give the same result - an (invisible) <abc> tag - because they are translated when the tag is parsed server-side. What you must do is:
<asp:Label runat="server" ID="myLabel" Text="&lt;abc&gt;" />
This will give the desired result - the & entity reference will render an ampersand to the client. Followed by lt;, the result is a correct client-side entity reference (<). Which will render as <.
To answer you questions explicitly: Yes, using entity references in ASP.NET attributes is (obviously) OK, since it's an XML format. This is not really a 'decision' on Microsoft's part (and certainly not a bug) - i's simply XML.
The trick is realizing when the entity references are parsed (when the tag is parsed on the server), and what the resulting text is, which is what will be sent to the client.
Yes it's allowed of course. Label control's purpose is to show text and markup to client. And it's really useful I think. injected code is your responsibility.
The asp.net aspx parser will unescape the "<" and ">" to "<" and ">". It will generate something like this method:
[DebuggerNonUserCode]
private Label __BuildControlLabel1()
{
Label __ctrl = new Label();
base.Label1 = __ctrl;
__ctrl.ApplyStyleSheetSkin(this);
__ctrl.ID = "Label1";
__ctrl.Text = "<abc>";
return __ctrl;
}
If you wanted to write it in the text property you could double escape like "&lt;", but it is probably easier just to write it between start and end tags like you mention.
<asp:Label ...><abc></asp:Label>.

Resources