Custom Repeater Control?

Last post 06-18-2007 4:50 AM by Allen Chen – MSFT. 1 replies.

Sort Posts:

  • Custom Repeater Control?

    06-13-2007, 5:36 PM
    • Member
      17 point Member
    • Jalias
    • Member since 06-14-2006, 3:58 PM
    • Posts 11

    I have a user control that I want to be able to plug into a variety of web applications.  All of the applications have the same UI  and code.  The UI is a repeater with a hyperlink and a div:

     <asp:repeater id="MyItems" runat="server">
    <itemtemplate>
    <h2><a id="title" href="\wa\home\details.aspx" runat="server"></a></h2>
    <div id="summary" runat="server"></div>
    </itemtemplate>
    </asp:repeater>

     We simply fill the repeater with data from a database and do the databinding in the code behind.

    I was thinking I would create a custom control so I could easily plug this into all of the different sites using it, but I am not experienced with this.  Is there a way for me to create a custom repeater control that would include the UI and databinding? 

    Is there a better way to achieve this? For instance, should I just create a web project with this user control and reference this in the web applications that need to utilize the user control? (This really seems more like a hack than a solution.  My preference is to create something with the UI and code that I can just plug in the pages.

    Your feedback is appreciated.

    JM
     

  • Re: Custom Repeater Control?

    06-18-2007, 4:50 AM
    Answer

    Hi:

      To test, you can first write code below in the same project of your web site:

        public class RepeaterTemplate : ITemplate
        {
            public RepeaterTemplate()
            {
            }

            //A variable to hold the type of ListItemType.
            ListItemType _templateType;


            //Constructor where we define the template type and column name.
            public RepeaterTemplate(ListItemType type)
            {
                //Stores the template type.
                _templateType = type;
            }
            void ITemplate.InstantiateIn(System.Web.UI.Control container)
            {
                switch (_templateType)
                {
                    case ListItemType.Item:
                        Literal l = new Literal();
                        l.Text = @"<h2><a id='title' href='\wa\home\details.aspx' runat='server'>Hello</a></h2>
    <div id='summary' runat='server'>It's invisible if here's no text so I added it</div>";
                        container.Controls.Add(l);
                        break;
                }
            }
        }
      public class CustomRepeater:Repeater
        {
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                this.ItemTemplate = new RepeaterTemplate(ListItemType.Item);
            }
        }

      protected void Page_Load(object sender, EventArgs e)
        { 
            CustomRepeater r = new CustomRepeater();
            this.form1.Controls.Add(r);
            r.DataSourceID = "SqlDataSource1";
            r.DataBind();
     
        }

     

    If code above works, you can put these code(bold one) into a ClassLibrary project and build a .dll. Then in each project you can simply add a reference from this dll file and use it as mentioned above.

    If it doesn't work, please inform us.

    Regards

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (2 items)