4

I'm trying to understand html and asp.net.

It seems (please do correct me if I'm wrong!) that the code I write on my aspx pages on my web project are not all html. Rather, they're a code which is run by some "ASP.Net" compiler when a user makes an html request from their url (So thanks to runat server the "compiler" understands non-html tags such as <asp). Then – the "compiler" uses this code to create an html page on the fly. For example – this converts a Button to an input.

Is this true? If so, then, what is a user control?

ispiro
  • 26,556
  • 38
  • 136
  • 291

3 Answers3

4

ASP.NET has server side controls (Such as buttons, hyperlinks, gridviews etc). These all generate server side events (Button1_OnClick) which can be handled by C#/VB.NET code.

<asp:Button ID="btnCopyText" runat="server" /> // Calls the server side btnCopyText code

public void btnCopyText_Click(object sender, EventArgs e) {

}  

HTML has it's own controls which are rendered by the client's browser (client side).

 <button onclick="copyText()">Copy Text</button> // Calls the copyText() function (Client Side)

The ASP.NET Button gets rendered at the server side and then gets passed down to the client as a standard HTML Button (Shown above).

A custom user control is an ASP.NET Server Side control that is created by the programmer, it may contain several ASP.NET Server Side Controls such as a GridView and a button). An example would be, when the button is clicked it reloads the data in the GridView. This provides a way for an ASP.NET developer to write one custom control and reuse it when needed rather than write boilerplate code (repeating the same code).

http://msdn.microsoft.com/en-us/library/y6wb1a0e.aspx

Darren
  • 68,902
  • 24
  • 138
  • 144
  • Thanks. But I thought that's what custom server controls were. So it seems that they're both just built out of basic server control (and html...). The difference being just in some way of how exactly they're implemented. – ispiro Jun 13 '12 at 11:27
2

Yes it is true all asp.net control renders as html but runat server tell its a server control.

A user control is a kind of composite control that works much like an ASP.NET Web page—you can add existing Web server controls and markup to a user control, and define properties and methods for the control. You can then embed them in ASP.NET Web pages, where they act as a unit

Fraz Sundal
  • 10,288
  • 22
  • 81
  • 132
1

asp.net is programming tool that helps you generate html code fast and easy, the final goal is the full html page with all the rest components that needs to show like scripts, images, ajax calls etc.

the user control, is like an object that render also html, but you can use it many times, in many different pages, or on the same page.

Eg a user control may render the image and the details of a product, then if you use it inside a repeater you can render all the products of your database with one only control, and a loop.

that the code I write on my aspx pages on my web project are not all html.

What you write on your aspx page is actually programming code that asp.net parse and render the final html page. Asp.net is allow you to have inside the code and html and other text, and you separate the part that must be parse from the rest text using the asp.net special tags and declarations.

Aristos
  • 66,005
  • 16
  • 114
  • 150