Search code examples
asp.nethtmlvisual-web-developer-2010

User control vs Server control


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?


Solution

  • 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