How To Bind Custom Object To Build Dynamic CheckBoxList

ASPX Page

<%@ Page Language="C#" AutoEventWireup="true" %>

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="InvestorDataFeedTest" %>

<script runat="server">
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        
        if (!IsPostBack)
        {
            
            CBMaterialItems.DataSource = PrintedMaterialLogic.GetPrintedMaterialCBDataTable();
            CBMaterialItems.DataValueField = "MaterialID";
            CBMaterialItems.DataTextField = "MaterialName";
            CBMaterialItems.DataBind();

        }
        

    }

  
</script>

<html>
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:CheckBoxList ID="CBMaterialItems" runat="server" AutoPostBack="True" />
    </div>
    </form>
</body>
</html>

PrintedMaterialLogic.cs

using System;
using System.Collections.Generic;
using System.Web;

using System.Web.UI;
using System.Xml;
using System.Data;
using System.Reflection;
using System.Net;
using System.IO;
using System.Collections.Specialized;

namespace InvestorDataFeedTest
{
    public class PrintedMaterialLogic
    {

        public PrintedMaterialLogic() { }


        // Public Methods (Business Logic Layer)
        #region public static DataTable GetPrintedMaterialCBDataTable()
        //[DataObjectMethod(DataObjectMethodType.Select)]
        public static DataTable GetPrintedMaterialCBDataTable()
        {

            // Create an object to return
            DataTable datatable = new DataTable();

            try
            {
                List<PrintedMaterialData> entireList = new List<PrintedMaterialData>();

                entireList = GetPrintedMaterialCBData();

                datatable = ListToDataTable(entireList);
            }
            catch (Exception ex)
            {
                // Send error email
                BLL.SendErrorEmail("Get Printed Material CheckBox DataTable Error", "", ex);
            }


            return datatable;

        }
        #endregion    







        // Private Methods (Data Access Layer)
        #region public static List<PrintedMaterialData> GetPrintedMaterialCBData()
        public static List<PrintedMaterialData> GetPrintedMaterialCBData()
        {
            // Create object to return
            List<PrintedMaterialData> list = new List<PrintedMaterialData>();

            // Check cache for value first           
            Page page = (Page)HttpContext.Current.CurrentHandler;
            string cacheKey = "GetPrintedMaterialCBData";

            if (page.Cache[cacheKey] != null)
            {
                // Return cached value if it exists
                return (List<PrintedMaterialData>)page.Cache[cacheKey];
            }
            else
            {
                
                // Fill cache



                // Get XML
                string url = "http://xml.site.net/irxmlclient.asp?compid=343432&reqtype=informationrequest";
                string data = GetScreenScrape(url);

                XmlDocument xmlDoc = new XmlDocument();              
                xmlDoc.LoadXml(data);              


                // Get nodes
                XmlNodeList xmlNodes = xmlDoc.DocumentElement.SelectNodes("/IRXML/InformationRequest/InfoRequestMaterials/InfoRequestMaterial");
                // For each xml node, create the corresponding objects
                foreach (XmlNode xmlNode in xmlNodes)
                {

                    XmlElement elt = (XmlElement)xmlNode;

                    PrintedMaterialData node = PrintedMaterialCBDataCreate(xmlNode);

                    // If node was created, then add to our list
                    if (node != null) { list.Add(node); }
                }
            }


            return list;


        }
        #endregion
        #region public static PrintedMaterialData PrintedMaterialCBDataCreate(XmlNode xmlNode)
        public static PrintedMaterialData PrintedMaterialCBDataCreate(XmlNode xmlNode)
        {
            // Create an object to return
            PrintedMaterialData item = new PrintedMaterialData();

            if (xmlNode != null)
            {
                
                XmlNode materialNameNode = xmlNode.SelectSingleNode("MaterialName");                   
                XmlAttribute materialIDAttribute = xmlNode.Attributes["MaterialID"];

                item.MaterialName = (materialNameNode == null) ? "" : materialNameNode.InnerText.Trim();
                item.MaterialID = (materialIDAttribute == null) ? "" : materialIDAttribute.InnerText.Trim();     

                
                return item;
            }

            return null;
        }
        #endregion



        




        // Methods / Functions already in .COM Solution
        #region public static string GetScreenScrape(string url)
        public static string GetScreenScrape(string url)
        {
            // Create the WebRequest for the URL we are using
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)";
            // Get the stream from the returned web response
            StreamReader stream = new StreamReader(req.GetResponse().GetResponseStream());

            //experiment try reading entire thing at once
            string html = stream.ReadToEnd();


            // Finished with the stream so close it now
            stream.Close();

            return html;

        }
        #endregion
        #region private static DataTable ListToDataTable<T>(List<T> list)
        private static DataTable ListToDataTable<T>(List<T> list)
        {
            DataTable dt = new DataTable();

            foreach (PropertyInfo info in typeof(T).GetProperties())
            {
                dt.Columns.Add(new DataColumn(info.Name, info.PropertyType));
            }
            foreach (T t in list)
            {
                DataRow row = dt.NewRow();
                foreach (PropertyInfo info in typeof(T).GetProperties())
                {
                    row[info.Name] = info.GetValue(t, null);
                }
                dt.Rows.Add(row);
            }
            return dt;
        }
        #endregion




    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s