SIGN UP MEMBER LOGIN:    
ARTICLE

Using DataList Control in Web Forms

Posted by Jason Zadroga Articles | Visual C# January 12, 2001
This example uses a DataList which contains LinkButton controls, which allows a user to navigate through a list of data.
Reader Level:

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);

            }

        }

    }
}

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor