Monday, 11 April 2011

[DOT NET]Dynamic Repeater Control Binding


Below class are used for Dynamically add columns in a repeater control
public enum Alignment
    {
        Left,
        Center,
        Right
    }
    public class ReportGrid
    {
        internal Repeater oPerent
        {
            get;
            set;
        }
        public ReportGrid(ref Repeater _Repeater)
        {
            oPerent = _Repeater;
            ReportBinders = new List<ReportBinder>();
        }
        public void DataBind()
        {
            oPerent.HeaderTemplate = new RepeaterTemplate(ListItemType.Header, ReportBinders);
            oPerent.ItemTemplate = new RepeaterTemplate(ListItemType.Item, ReportBinders);
            oPerent.AlternatingItemTemplate = new RepeaterTemplate(ListItemType.AlternatingItem, ReportBinders);
            oPerent.FooterTemplate = new RepeaterTemplate(ListItemType.Footer, ReportBinders);
            oPerent.DataSource = DataSource;
            oPerent.DataBind();
        }
        public List<ReportBinder> ReportBinders
        {
            get;
            set;
        }
        public Object DataSource
        {
            get;
            set;
        }
    }
    public class ReportBinder
    {
        public ReportBinder()
        {
            Width = 100;
            Alignment = Alignment.Left;
        }
        public String DataField
        {
            get;
            set;
        }
        public String ColumnName
        {
            get;
            set;
        }
        public String DataFormat
        {
            get;
            set;
        }
        public Int32 Width
        {
            get;
            set;
        }
        public Alignment Alignment
        {
            get;
            set;
        }
    }
    public class RepeaterTemplate : System.Web.UI.ITemplate
    {
        System.Web.UI.WebControls.ListItemType templateType;
        List<ReportBinder> _ReportBinder;
        List<Label> _item1;
        public RepeaterTemplate(System.Web.UI.WebControls.ListItemType type,List<ReportBinder> ReportBinder)
        {
            templateType = type;
            _ReportBinder = ReportBinder;
        }
        public void InstantiateIn(System.Web.UI.Control container)
        {
            PlaceHolder ph = new PlaceHolder();
            _item1 = new List<Label>();
            foreach (ReportBinder rb in _ReportBinder)
            {
                Label item1 = new Label();
                item1.ID = rb.DataField;
                _item1.Add(item1);
            }

            switch (templateType)
            {
                case ListItemType.Header:
                    ph.Controls.Add(new LiteralControl("<table class=\"RepDisplay\">"));
                    ph.Controls.Add(new LiteralControl("<tr>"));
                    foreach (ReportBinder rb in _ReportBinder)
                    {
                        string Ali = "";
                        switch (rb.Alignment)
                        {
                            case Alignment.Left:
                                Ali = "text-align:left";
                                break;
                            case Alignment.Center:
                                Ali = "text-align:center";
                                break;
                            case Alignment.Right:
                                Ali = "text-align:right";
                                break;
                        }
                        ph.Controls.Add(new LiteralControl("<th style=\"width:" + rb.Width + " px;" + Ali + "\">" + rb.ColumnName + "</th>"));
                    }
                    ph.Controls.Add(new LiteralControl("</tr>"));
                    break;
                case ListItemType.Item:
                    ph.Controls.Add(new LiteralControl("<tr class=\"oddrow\">"));
                    foreach (Label bl in _item1)
                    {
                        ph.Controls.Add(new LiteralControl("<td>"));
                        ph.Controls.Add(bl);
                        ph.Controls.Add(new LiteralControl("</td>"));
                    }
                    ph.Controls.Add(new LiteralControl("</tr>"));
                    ph.DataBinding += new EventHandler(Item_DataBinding);
                    break;
                case ListItemType.AlternatingItem:
                    ph.Controls.Add(new LiteralControl("<tr class=\"evenrow\">"));
                    foreach (Label bl in _item1)
                    {
                        ph.Controls.Add(new LiteralControl("<td>"));
                        ph.Controls.Add(bl);
                        ph.Controls.Add(new LiteralControl("</td>"));
                    }
                    ph.Controls.Add(new LiteralControl("</tr>"));
                    ph.DataBinding += new EventHandler(Item_DataBinding);
                    break;
                case ListItemType.Footer:
                    ph.Controls.Add(new LiteralControl("</table>"));
                    break;
            }
            container.Controls.Add(ph);
        }
        void Item_DataBinding(object sender, System.EventArgs e)
        {
            PlaceHolder ph = (PlaceHolder)sender;
            RepeaterItem ri = (RepeaterItem)ph.NamingContainer;
            for (int i = 0; i < _ReportBinder.Count;i++ )
            {
                Object item1Value;
                if(_ReportBinder[i].DataFormat !=null && _ReportBinder[i].DataFormat!=string.Empty)
                    item1Value = (Object)DataBinder.Eval(ri.DataItem, _ReportBinder[i].DataField, _ReportBinder[i].DataFormat);
                else
                    item1Value = (Object)DataBinder.Eval(ri.DataItem, _ReportBinder[i].DataField);
                   
                ((Label)ph.FindControl(_ReportBinder[i].DataField)).Text = Convert.ToString(item1Value);
            }
            //Int32 item1Value = (Int32)DataBinder.Eval(ri.DataItem, "CategoryID");
            //String item2Value = (String)DataBinder.Eval(ri.DataItem, "CategoryName");
            //((Label)ph.FindControl("item1")).Text = item1Value.ToString();
            //((Label)ph.FindControl("item2")).Text = item2Value;
        }
    }

Uses:

<div style="overflow: scroll; height: 500px;">
                            <asp:Repeater ID="RepDisplay" runat="server">
                            </asp:Repeater>
                        </div>

In CS file:

ReportGrid oReportGrid = new ReportGrid(ref RepDisplay);
        for (int Icol = 0; Icol < dtData.Columns.Count; Icol++)
        {
            DataRow dr = fnMetadataByField(dtData.Columns[Icol].ColumnName);
            oReportGrid.ReportBinders.Add(new ReportBinder()
            {
                ColumnName = dr["DSPLNAME"].ToString(),
                DataField = dtData.Columns[Icol].ColumnName,
                Alignment = dr["DATATYPE"].ToString() == "2" ? Alignment.Right : Alignment.Left,
                DataFormat = dr["DATATYPE"].ToString() == "3" ? "{0:dd-MMM-yyyy}" : null,
                Width = Convert.ToInt32(dr["WIDTH"].ToString())
            });
        }
        oReportGrid.DataSource = pds;
        oReportGrid.DataBind();

Posted By: Mr. Palash Paul



2 comments: