locked
Special Custom Control Idea RRS feed

  • Question

  • User1753774914 posted

    Hi,


    I have the idea to develop a control (I call it "MileStone Control") which looks like this:


    It is the basis to create my "MileStone Week Calendar"

    In the label control the order number is assigned, the different buttons execute code to modify the db (for example pass the milestone to the next workingday) and then rebind the gridview.


    When loading the page the different milestone control per weekday and milestonetype are created, filled with data and assigned to the corresponding gridviewcell.

    This is how it looks today (just boring red (overdue) labels, no modification for a milestone is possible on this page)



    Can someone bring me on the right way to accomplish this? I never developed a custom server control before.

    What I already developed is a custom user control but here I need multiple instances on a page.



    Monday, October 19, 2009 12:56 PM

Answers

  • User-2106054853 posted

    Hi,

    Is it what you need?

        public partial class _Default : System.Web.UI.Page
        {
            static Random r = new Random();
            protected void Page_Load(object sender, EventArgs e)
            {
               if (!IsPostBack)
               {
                   List<MyData> list = new List<MyData>();
                   MyData md = new MyData();
                   for (int i = 0; i < 3; i++)
                   {
                       md.Data.Add(r.Next(1000, 5000).ToString());
                   }

                   MyData md2 = new MyData();
                   for (int i = 0; i < 4; i++)
                   {
                       md2.Data.Add(r.Next(1000, 5000).ToString());
                   }

                   list.Add(md);
                   list.Add(md2);
                   this.GridView1.DataSource = list;
                   this.GridView1.DataBind();
                }
            }
        }
        public class MyData {

            public List<string> Data { get; set; }

            public MyData() {
                Data = new List<string>();
            }
        }

        public class MyControl : WebControl {
            public IEnumerable Data { get; set; }
            private List<string> _list
            {
                get
                {
                    var i = this.ViewState["_list"];
                    if (i != null)
                    {
                        return (List<string>)i;
                    }
                    return new List<string>();
                }
                set
                {
                    this.ViewState["_list"] = value;
                }
            }
            private int _currentindex
            {
                get
                {
                    var i = this.ViewState["_currentindex"];
                    if (i != null)
                    {
                        return Convert.ToInt32(i);
                    }
                    return 0;
                }
                set
                {
                    this.ViewState["_currentindex"] = value;
                }
            }
            Label _lb = new Label();
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                LinkButton lbprevious = new LinkButton();
                lbprevious.Click += new EventHandler(lbprevious_Click);
                lbprevious.Text = "Previous";
               
                LinkButton lbnext = new LinkButton();
                lbnext.Click += new EventHandler(lbnext_Click);
                lbnext.Text = "Next";
                this.Controls.Add(lbprevious);
                this.Controls.Add(_lb);
                this.Controls.Add(lbnext);
            }
            protected override void OnDataBinding(EventArgs e)
            {
                base.OnDataBinding(e);
                List<string> list = new List<string>();
                foreach (var item in Data) {
                    list.Add(item.ToString());
                }
                _list = list;
            }
            void lbnext_Click(object sender, EventArgs e)
            {
              
                _currentindex=_currentindex+1<_list.Count-1?_currentindex+1:_list.Count-1;
             
            }

            void lbprevious_Click(object sender, EventArgs e)
            {
            
                _currentindex=_currentindex-1>0?_currentindex-1:0;
              
            }
            protected override void OnPreRender(EventArgs e)
            {
                base.OnPreRender(e);
                this._lb.Text = _list[_currentindex];
            }
        }

     

    <asp:GridView AutoGenerateColumns="false" ID="GridView1" runat="server">
            <Columns>
            <asp:TemplateField HeaderText="Test Control">
            <ItemTemplate>
            <cc1:MyControl Data='<%#Eval("Data") %>' ID="MyControl1" runat="server" />
            </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="All Data">
            <ItemTemplate>
                <asp:Repeater DataSource= '<%#Eval("Data") %>' ID="Repeater1" runat="server">
                <ItemTemplate>
                <%#Container.DataItem %>,
                </ItemTemplate>
                </asp:Repeater>
            </ItemTemplate>
            </asp:TemplateField>
            </Columns>
            </asp:GridView>

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 21, 2009 2:35 AM

All replies

  • User-2106054853 posted

    Hi,

    Is it what you need?

        public partial class _Default : System.Web.UI.Page
        {
            static Random r = new Random();
            protected void Page_Load(object sender, EventArgs e)
            {
               if (!IsPostBack)
               {
                   List<MyData> list = new List<MyData>();
                   MyData md = new MyData();
                   for (int i = 0; i < 3; i++)
                   {
                       md.Data.Add(r.Next(1000, 5000).ToString());
                   }

                   MyData md2 = new MyData();
                   for (int i = 0; i < 4; i++)
                   {
                       md2.Data.Add(r.Next(1000, 5000).ToString());
                   }

                   list.Add(md);
                   list.Add(md2);
                   this.GridView1.DataSource = list;
                   this.GridView1.DataBind();
                }
            }
        }
        public class MyData {

            public List<string> Data { get; set; }

            public MyData() {
                Data = new List<string>();
            }
        }

        public class MyControl : WebControl {
            public IEnumerable Data { get; set; }
            private List<string> _list
            {
                get
                {
                    var i = this.ViewState["_list"];
                    if (i != null)
                    {
                        return (List<string>)i;
                    }
                    return new List<string>();
                }
                set
                {
                    this.ViewState["_list"] = value;
                }
            }
            private int _currentindex
            {
                get
                {
                    var i = this.ViewState["_currentindex"];
                    if (i != null)
                    {
                        return Convert.ToInt32(i);
                    }
                    return 0;
                }
                set
                {
                    this.ViewState["_currentindex"] = value;
                }
            }
            Label _lb = new Label();
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                LinkButton lbprevious = new LinkButton();
                lbprevious.Click += new EventHandler(lbprevious_Click);
                lbprevious.Text = "Previous";
               
                LinkButton lbnext = new LinkButton();
                lbnext.Click += new EventHandler(lbnext_Click);
                lbnext.Text = "Next";
                this.Controls.Add(lbprevious);
                this.Controls.Add(_lb);
                this.Controls.Add(lbnext);
            }
            protected override void OnDataBinding(EventArgs e)
            {
                base.OnDataBinding(e);
                List<string> list = new List<string>();
                foreach (var item in Data) {
                    list.Add(item.ToString());
                }
                _list = list;
            }
            void lbnext_Click(object sender, EventArgs e)
            {
              
                _currentindex=_currentindex+1<_list.Count-1?_currentindex+1:_list.Count-1;
             
            }

            void lbprevious_Click(object sender, EventArgs e)
            {
            
                _currentindex=_currentindex-1>0?_currentindex-1:0;
              
            }
            protected override void OnPreRender(EventArgs e)
            {
                base.OnPreRender(e);
                this._lb.Text = _list[_currentindex];
            }
        }

     

    <asp:GridView AutoGenerateColumns="false" ID="GridView1" runat="server">
            <Columns>
            <asp:TemplateField HeaderText="Test Control">
            <ItemTemplate>
            <cc1:MyControl Data='<%#Eval("Data") %>' ID="MyControl1" runat="server" />
            </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="All Data">
            <ItemTemplate>
                <asp:Repeater DataSource= '<%#Eval("Data") %>' ID="Repeater1" runat="server">
                <ItemTemplate>
                <%#Container.DataItem %>,
                </ItemTemplate>
                </asp:Repeater>
            </ItemTemplate>
            </asp:TemplateField>
            </Columns>
            </asp:GridView>

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 21, 2009 2:35 AM
  • User1753774914 posted

    Thank you very very much! On the first look this is fantatsic and just the help I needed.

    Monday, October 26, 2009 10:09 AM
  • User1753774914 posted

    Hi,

    This worked great, but now I need to go further...

    I'd like to define overridable methods on the button click events which I then can override on the page.

     

    Can you help me to achive that?

    Friday, December 4, 2009 12:45 PM