Archive, ASP.NET

Runat Server

As an ASP.NET developer you have undoubtedly seen controls with their runat property set to the value server. Other than being a required property, what does that property really mean?

ASP.NET is what is referred to as a server-side technology. What that means is that portions of your ASP.NET application will be ran on a web server before even reaching your users web browser.

The way the web server knows which portions of your ASPX to execute and which portions to leave as-is is determined by the runat property. Any control with runat=»server» is preprocessed by the ASP.NET runtime, and any control lacking it is left alone. Worth pointing out is that any control, even regular HTML tags can be ran at the server.

	<div id="divExample" runat="server" />

With a normal div tag executed on the server, we can programmatically control that element from C# or VB.NET before its HTML is sent across the Internet. So, for example, you could make this particular div hidden by modifying its CSS styling on your Page_Load event.

	divExample.Style["display"] = "none";

This particular technique is useful for cleaning up the rendered HTML, and also allows for easy JavaScript manipulation of elements. In ASP.NET MVC this can also be an extremely powerful way to make up for lost functionality, as any control dependent upon ViewState is unavailable.

About Jason

Джейсон является опытным предпринимателем и разработчиком программного обеспечения, квалифицированным в области лидерства, мобильной разработки, синхронизации данных и архитектуры SaaS. Он получил степень бакалавра наук (B.S.) в области компьютерных наук в Университете штата Арканзас.
View all posts by Jason →

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *