Search code examples
asp.netservercontrol

Asp.net: create a new server control. Where to start?


I would like to create a Server control that get some parameters and render a form or a gridview. My goal is to create a library for standard CRUD operations . A server control like this:

<asp:myservercontrol database="mydatabase" table="mytable" ... othersparameter typeofcontrol="gridview or listview" >

I need an help: where to start ? I've create a new "ServerControl" ?

EDIT: to be more precisely, in my old Winform .NET applications, i used to create a "template" form (with toolbar, buttons, grid), then, with very little code, i "bind" that with database table. I would like to recreate this sort of template/server control in ASP.NET.

Thanks


Solution

  • I would start by reading this: https://msdn.microsoft.com/en-us/library/fxh7k08z(v=vs.140).aspx

    There are a set of server controls that are standard with ASP.NET Web Forms and you can either chose to use one of those as a starting point or start totally from scratch.

    You essentially create a C# (or VB.NET) code file and define a class that inherits from a base control type and then override the RenderContents method

    Here is an example of a custom control:

    namespace WebApplication1
    {
        [DefaultProperty("Text")]
        [ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
        public class WebCustomControl1 : WebControl
        {
            [Bindable(true)]
            [Category("Appearance")]
            [DefaultValue("")]
            [Localizable(true)]
            public string Text
            {
                get
                {
                    String s = (String)ViewState["Text"];
                    return ((s == null) ? String.Empty : s);
                }
    
                set
                {
                    ViewState["Text"] = value;
                }
            }
    
            protected override void RenderContents(HtmlTextWriter output)
            {
                output.Write(Text);
            }
        }
    }