locked
Happy New Year: ASP.NET User Control vs Server control? RRS feed

  • Question

  • User619554136 posted

    Hi, Happy new year to all,

    could anybody please explain the differences between ASP.NET User Controls VS Server Controls.

    I guess, Server controls are available as Assembly. if this is only the difference then, we can as well make an assembly with only user controls. I could not understand in this. Please explain.

    thanks, Dutt

    Thursday, December 31, 2009 12:35 PM

Answers

  • User-158764254 posted

    UserControls are a combination of ascx markup and code-behind.  This make it very easy to implement the UI portion of the usercontrol as it is standard markup file very much like a page.

    A ServerControl on the other hand is created entirely in code.  There is no markup file to hold html/asp.net markup. So all UI elements must be added to the control programatically.

    I've been known to start with a usercontrol to model a complex control and then convert it to a pure ServerControl once i'm satisified with it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 31, 2009 1:27 PM

All replies

  • User-158764254 posted

    UserControls are a combination of ascx markup and code-behind.  This make it very easy to implement the UI portion of the usercontrol as it is standard markup file very much like a page.

    A ServerControl on the other hand is created entirely in code.  There is no markup file to hold html/asp.net markup. So all UI elements must be added to the control programatically.

    I've been known to start with a usercontrol to model a complex control and then convert it to a pure ServerControl once i'm satisified with it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 31, 2009 1:27 PM
  • User619554136 posted

    Hi,

    Sorry for the delay. really thanks for your answer. I have 2 more questions.

    I've been known to start with a usercontrol to model a complex control and then convert it to a pure ServerControl once i'm satisified with it.

    Q1) can you please let me know how do you convert the markup from user control to the code for server control. Do you append all the markup to TextWriter and pass it to RenderControl?

    Q2) I would like to create a server control which need to render some HTML code whereever we use the server control. and also it should render some code in <HEAD></HEAD> section. how do we render some code in body and some code in head?

    Example following highlighted lines 1,2, 3,4 should be generated automatically at rendering time at whereever we use the control. few lines at Header and few lines at body where every we droped the control.

        1. <%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
        <html>
          <head>
            2. <link rel="stylesheet" type="text/css" href="theme.css" />
          </head>
          <body>
            3. <my:control1 run="server" id="mycontrol1">
            4. </my:control>
          <body>
        </html>

    Monday, January 4, 2010 8:11 AM
  • User-158764254 posted

    Q1) can you please let me know how do you convert the markup from user control to the code for server control. Do you append all the markup to TextWriter and pass it to RenderControl?
     

    For simple html markup, you can write it out directly using the TextWriter.  you can also create what's referred to as a composite control too.  A composite control is a combination of other server controls that you add to your custom controls child controls collection.

    For example:

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

     

    Q2) I would like to create a server control which need to render some HTML code whereever we use the server control. and also it should render some code in <HEAD></HEAD> section. how do we render some code in body and some code in head?

    If your pages use a head section that is marked as runat=server, then you can add new controls directly to that head control from your custom server control.

    For example:

    http://www.edream.org/BlogArticle.aspx?RecordID=112

    If you do end up relying on a runat=server head control on the page, then you may want to throw a nice clear error if the header control is not found.  That way, if somebody drops your control on a page that does not use a runat=server head, they will know exactly what went wrong.

    This is what the asp.net themes feature does:

    http://odetocode.com/Blogs/scott/archive/2007/03/26/the-head-tag-in-asp-net-2-0.aspx

     

     

    Monday, January 4, 2010 5:00 PM