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



7 comments:

  1. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    rpa training in bangalore | best rpa training in bangalore
    RPA training in bangalore | rpa courses in bangalore

    ReplyDelete
  2. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.
    I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
    Java training in Chennai
    Java training in Bangalore
    Java online training
    Java training in Pune






    ReplyDelete
  3. I would like to thank you for your nicely written post, its informative and your writing style encouraged me to read it till end. Thanks
    I would like to thank you for your nicely written post, its informative and your writing style encouraged me to read it till end. Thanks

    ReplyDelete
  4. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
    AWS Course Interview Questions and Answers for Freshers | AWS Interviews Questions and Answers for Devops
    AWS Interview questions and answers for Sysops |AWS Interview Question and Answers BlogSpot

    ReplyDelete
  5. These things are very important, good think so - I think so too... Houston Embroidery Service

    ReplyDelete