Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Selectable GridView with WaitBox

, 16 Oct 2008
This article describes how to implement a selectable GridView control in ASP.NET.
WaterGrid.zip
WaterGrid
App_Code
NewsItems.xss
App_Data
Bin
AjaxControlToolkit.dll
Images
bar_bk.gif
bar_line.gif
bg_nav.gif
Thumbs.db
PreRequisites
AjaxControlToolkit.dll
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.


using System;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web;

/// <summary>
/// An XML data object that lets us read/write from an XML source
/// </summary>
[DataObject(true)]
public abstract class TodoXmlDataObject
{
    // the web site physical root
    private string _rootPath;
    private string _name;
    private DataSet _ds;

    public TodoXmlDataObject()
    {
    }

    public TodoXmlDataObject(string name)
    {
        _name = name;
    }

    public TodoXmlDataObject(string rootPath, string name)
    {
        _rootPath = rootPath;
        _name = name;
    }

    private DataSet DataSet
    {
        get
        {
            if (_ds == null)
            {
                _ds = new DataSet();
                _ds.ReadXmlSchema(XsdFile);
                LoadData();
            }
            return _ds;
        }
    }

    private DataTable Table
    {
        get { return DataSet.Tables[_name]; }
    }

    /// <summary>
    /// Figures out the physical root of the website.
    /// </summary>
    protected string RootPath
    {
        get
        {
            if (_rootPath != null)
            {
                return _rootPath;
            }
            else if (HttpContext.Current != null)
            {
                return HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath);
            }
            else
            {
                throw new ApplicationException("Can't find server root path.");
            }
        }
    }

    protected abstract bool CanReadStream
    {
        get;
    }

    protected virtual bool CanWriteStream
    {
        get { return true; }
    }

    protected abstract Stream XmlStream
    {
        get;
    }


    private string XsdFile
    {
        get { return Path.Combine(RootPath, String.Format(@"App_Code\{0}.xsd", _name)); }
    }

    [DataObjectMethod(DataObjectMethodType.Select)]
    public DataView Select()
    {
        Table.DefaultView.Sort = "Date";
        return Table.DefaultView;
    }

    [DataObjectMethod(DataObjectMethodType.Insert)]
    public int Insert(string Title, string Description, string Date)
    {
        DataRow dr = Table.NewRow();

        dr["Title"] = Title;
        dr["Description"] = Description;
        dr["Date"] = Date;
        Table.Rows.Add(dr);
        SaveChanges();
        return 1;
    }

    [DataObjectMethod(DataObjectMethodType.Update)]
    public virtual int Update(string Title, string Category, int Original_ItemID)
    {
        DataRow[] rows = Table.Select(String.Format("ItemID={0}", Original_ItemID));

        if (rows.Length > 0)
        {
            DataRow dr = rows[0];
            dr.BeginEdit();
            dr["Title"] = Title;
            dr["Category"] = Category;
            dr.EndEdit();
            SaveChanges();
            return 1;
        }
        return 0;
    }

    protected virtual void LoadData()
    {
        if (CanReadStream)
        {
            _ds.ReadXml(XmlStream, XmlReadMode.IgnoreSchema);
        }
    }

    protected virtual void SaveChanges()
    {
        DataSet.AcceptChanges();
        if (CanWriteStream)
        {
            XmlStream.Flush();
            DataSet.WriteXml(XmlStream, XmlWriteMode.IgnoreSchema);
        }
        else
        {
            throw new ApplicationException("Can't write to XML stream.");
        }
    }

    [DataObjectMethod(DataObjectMethodType.Delete)]
    public virtual int Delete(int Original_ItemID)
    {
        DataRow[] rows = Table.Select(String.Format("ItemID={0}", Original_ItemID));

        if (rows.Length > 0)
        {
            Table.Rows.Remove(rows[0]);
            SaveChanges();
            return 1;
        }
        return 0;
    }
}

/// <summary>
/// A version of the data source that reads and writes from the local XML file.
/// </summary>
public class FileTodoXmlDataObject : TodoXmlDataObject
{
    private Stream _stream;
    private string _name;

    public FileTodoXmlDataObject(string name)
        : base(name)
    {
        _name = name;
    }

    protected override bool CanReadStream
    {
        get { return File.Exists(XmlFile); }
    }

    protected override Stream XmlStream
    {
        get
        {
            if (_stream == null)
            {
                _stream = new FileStream(XmlFile, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read);
            }
            return _stream;
        }
    }

    private string XmlFile
    {
        get { return Path.Combine(RootPath, String.Format(@"App_Data\{0}.xml", _name)); }
    }

    protected override void LoadData()
    {
        XmlStream.Seek(0, SeekOrigin.Begin);
        base.LoadData();
    }

    protected override void SaveChanges()
    {
        XmlStream.Position = 0;
        XmlStream.SetLength(0);
        base.SaveChanges();
        XmlStream.Flush();
    }
}

/// <summary>
/// A version of the XML source that reads from the local file originally but then 
/// writes all deltas to the session state.
/// </summary>
public class SessionTodoXmlDataObject : FileTodoXmlDataObject
{
    private string _name;

    public SessionTodoXmlDataObject(string name)
        : base(name)
    {
        _name = name;
    }

    protected override Stream XmlStream
    {
        get
        {
            if (HttpContext.Current != null)
            {
                Stream sessionStream = HttpContext.Current.Session[_name] as Stream;

                if (sessionStream == null)
                {
                    if (CanReadStream && base.XmlStream != null)
                    {
                        Stream baseStream = base.XmlStream;

                        sessionStream = new MemoryStream((int)baseStream.Length);
                        byte[] data = new byte[baseStream.Length];
                        baseStream.Read(data, 0, (int)baseStream.Length);
                        sessionStream.Write(data, 0, data.Length);
                        sessionStream.Seek(0, SeekOrigin.Begin);
                    }
                    else
                    {
                        sessionStream = new MemoryStream(256);
                    }
                    HttpContext.Current.Session[_name] = sessionStream;
                }
                return sessionStream;
            }

            throw new ApplicationException("Can't get to session object to load state");
        }
    }
}

public class NewsItemsDataObject : SessionTodoXmlDataObject
{
    public NewsItemsDataObject() : base("NewsItems") { }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Iwona Les
Software Developer (Junior) Artaker Computersysteme GmbH
Austria Austria
I've began programming at the Warsaw University of Technolog. Currently I'm trying hardly;) to finish a master program at Vienna University of Technology and at the same time I'm working as Software Developer.
I'm interested in Web Development, technologies like AJAX, Silverlight, Semantic Web etc.

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 16 Oct 2008
Article Copyright 2008 by Iwona Les
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid