When is aspat not using runat = "server"?

When you do not need to use runat="server" in ASP.NET?

EDIT: Thanks for all the answers, but I really thought about runat="server" inside the <asp: tag.

+4
source share
6 answers

Use the runat=server attribute when you are using ASP.NET controls and / or you need programmatic access to these controls in your code.

HTML controls do not require this attribute. This is useful if you have any HTML element, such as <span> <div> , or <table> , if you want to access them in code.

 <asp:Label runat="server" id="foo" /> <div runat="server" id="bar /> ... foo.Text = "Hello Label"; foo.Attributes["class"] = "baz"; 
+3
source

You need to use runat="server" for any control that needs to be parsed as a server control.

Any element with runat="server" will be analyzed in the server control in the page archiarity. Everything else will be processed as plain text and placed in the LiteralControl in the page hierarchy.

The exception is elements that are not real elements, but special tags in another server tag, such as ContentTemplate tags. They do not need runat="server" because the containing control will parse them.

+1
source

If you do not want ASP.NET on the server side to display us a variable on the server side.

Generally speaking, you do not use it when you do not need to manipulate the server-side DOM element, for example. which are used for layout purposes only.

0
source

Without runat = "server", there would also be no other way to make html server-side controls. This seems like a strange thing because you cannot do runat = "client".

Thus, in summation, you cannot leave it on any ASP.Net control ever, and it was probably easiets and the cleanest way to find all server-side controls for developers who created ASP.Net Web forms.

source: http://mikeschinkel.com/blog/whyrunatserverforaspnetpart2/

0
source

The runat = "server" tag indicates that the code contained in the script block will be executed on the server (and not on the client). When executed, ASP.NET will create server objects containing this code, as well as an instance of the Page class, to contain the controls defined inside the page as instances of their specified type (System.Web.UI.WebControls.Textbox, for example). This server object will be called upon user request and will execute code in response to events.

0
source

Create runtime control

I need one label at runtime, that time is not needed runat = "Server" is not required

Example

 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Label lblNew = new Label(); lblNew.ID ="lblnew"; lblNew.Text ="Test"; } } 

this code creates a runtime label on the page load event

0
source

All Articles