Using DataList Control in Web Forms


Description 

This example uses a DataList which contains LinkButton controls, which allows a user to navigate through a list of data. The example uses various DataList events to dynamically create the data from each row in the DataList and responds to user interaction within the control. 

About Source Code 

Create a new ASP.NET project using VS.NET, then drag a DataList control on the web form. Rest of the code is simple. Just follow the source for which events you need to implement. 

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Xml;

using System.Xml.Xsl;

 

namespace WebBrowse

{

    /// <summary>

    /// Summary description for WebForm1.

    /// </summary>

    public class WebForm1 : System.Web.UI.Page

    {

        //this is the DataList Item on the WebForm

        protected System.Web.UI.WebControls.DataList WebDataList;

 

        private void Page_Load(object sender, System.EventArgs e)

        {

            //initialize GUI settings 

            if (!this.IsPostBack)

            {

                DMSDataList.DataSource = BuildDataTable(SomeXMLNode);

                DMSDataList.DataBind();

            }

        }

 

        #region Web Form Designer generated code

        override protected void OnInit(EventArgs e)

        {

            //

            // CODEGEN: This call is required by the ASP.NET Web Form Designer.

            //

            InitializeComponent();

            base.OnInit(e);

        }

 

        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InitializeComponent()

        {

            this.DMSDataList.ItemCreated += new System.Web.UI.WebControls.DataListItemEventHandler

            (this.DMSDataList_ItemCreated);

            this.DMSDataList.ItemCommand += new System.Web.UI.WebControls.DataListCommandEventHandler

            (this.DMSDataList_ItemCommand);

            this.DMSDataList.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler

            (this.DMSDataList_ItemDataBound);

            this.DMSDataList.SelectedIndexChanged += new System.EventHandler

            (this.DMSDataList_SelectedIndexChanged);

            this.Load += new System.EventHandler(this.Page_Load); 

        }

        #endregion

 

        private ICollection BuildDataTable(string xmlElement)

        {

            DataTable dt = new DataTable();

            dt.Columns.Add(new DataColumn("ObjectName", typeof(string)));

 

            //create a row for each item the incoming XML string

            NameTable nt = new NameTable();

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);

            XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);

 

            //Create the reader

            XmlTextReader readerXML = new XmlTextReader(dmsObjects, XmlNodeType.Element, context);

 

            while (readerXML.Read())

            {

                if (readerXML.Name == xmlElement)

                {

                    DataRow dr = dt.NewRow();

 

                    //set the object id in first column

                    string objectName = readerXML.ReadString();

 

                    dr[0] = objectName; //set the object name in second column

                    dt.Rows.Add(dr);

                }

            }

 

            DataView dv = new DataView(dt);

            return dv;

        }

 

        private void WebDataList_ItemCreated(object sender,

        System.Web.UI.WebControls.DataListItemEventArgs  e)

        {

            //based on the item set the attributes

            switch (e.Item.ItemType)

            {

                case System.Web.UI.WebControls.ListItemType.Item:

                    e.Item.BackColor = Color.White;

                    e.Item.CssClass = "rowList";

                    break;

                case System.Web.UI.WebControls.ListItemType.AlternatingItem:

                    e.Item.BackColor = Color.White;

                    e.Item.CssClass = "rowList";

                    break;

                case System.Web.UI.WebControls.ListItemType.SelectedItem:

                    e.Item.BackColor = Color.White;

                    e.Item.CssClass = "rowList";

                    break;

                default: //code used for the setting the header

                    e.Item.BackColor = Color.FromArgb(23, 72, 127);

                    e.Item.CssClass = "headerList";

                    break;

            }

        }

 

        private void WebDataList_SelectedIndexChanged(object sender, System.EventArgs e)

        {

        }

 

        private void WebDataList_ItemCommand(object source,

        System.Web.UI.WebControls.DataListCommandEventArgs e)

        {

            //build the XML based on the selection of the user 

            XmlNode xmlInput = GetMoreXML(((LinkButton)e.CommandSource).Text);

 

            DMSDataList.DataSource = BuildDataTable(xmlInput);

            DMSDataList.DataBind();

        }

 

        private void WebDataList_ItemDataBound(object sender,

        System.Web.UI.WebControls.DataListItemEventArgs e)

        {

            //set all the correct information for each DMS object

            DataRowView rowView = (DataRowView)e.Item.DataItem;

            if (rowView != null)

            {

                Array itemArray = rowView.Row.ItemArray;

 

                //set the properties for each item

                LinkButton linkBtn = (LinkButton)e.Item.FindControl("linkItem");

                linkBtn.Text = (string)itemArray.GetValue(0);

            }

        }

    }
}

Up Next
    Ebook Download
    View all
    Learn
    View all