Question

I have seen this code to create a Login control, I guess instead of writing this code, we can use an .ascx file to create this control. Can someone explains the difference of these two approaches. thanks. source:http://www.joe-stevens.com/2010/04/16/creating-a-composite-server-control-with-asp-net/

 [ToolboxData("<{0}:Login runat=server></{0}:Login>")]
 public class Login : CompositeControl
 {
   private TextBox txtUsername = new TextBox();
   private TextBox txtPassword = new TextBox();
  private Button btnLogin = new Button();

protected override void CreateChildControls()
{
    txtUsername.ID = "txtUsername";
    txtPassword.ID = "txtPassword";
    txtPassword.TextMode = TextBoxMode.Password;
    btnLogin.ID = "btnLogin";
    btnLogin.Text = "Login";

    Controls.Add(txtUsername);
    Controls.Add(txtPassword);
    Controls.Add(btnLogin);

    base.CreateChildControls();
}
}
Was it helpful?

Solution

Custom Server Controls typically serve a general purpose (beyond your application) and are typically built as a library to be shared across multiple applications. These are typically implemented and sold by third party vendors like Telerik, Infragistics, etc. User Controls are very centric to your application. You cannot share them across multiple applications (there may be some hacks, but generally not intended for that purpose). These are not built as libraries but as user interface components included in your application. Custom Server Controls don't provide designer support for building them but User Controls have designer support. So your example can be implemented both as a user control and custom server control depending upon what your intentions are for that control. That being said writing custom server controls is non-trivial. Here was a official description from MS http://msdn.microsoft.com/en-us/library/aa719735.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top