⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 superdatagrid.cs

📁 asp.net技术内幕的书配源码
💻 CS
📖 第 1 页 / 共 3 页
字号:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;

namespace Superexpert {



    //*********************************************************************
    //
    // SuperDataGrid Class
    //
    // The SuperDataGrid control supports sorting, paging, editing, 
    // and deleting records through its
    // EnableSorting, EnablePaging, EnableEditing, and EnableDeleting 
    // properties.
    //
    // See the SuperDataGrid.aspx file for
    // samples of using this control.
    //
    // This control is from the book ASP.NET Unleashed written by 
    // Stephhen Walther and published by SAMS publishing.
    //
    //*********************************************************************

    public class SuperDataGrid : WebControl, IPostBackDataHandler, IPostBackEventHandler {

        private bool _enableSorting = true;
        private bool _enableEditing = true;
        private bool _enableDeleting = true;
        private bool _enablePaging = true;
        
        private DataTable _dataGridData = new DataTable();
        private DataView  _dataGridView = new DataView();
        private DataTable _dataGridSchema = new DataTable();
        private Hashtable colEditValues = new Hashtable();
        
        private bool _editClicked = false;
        private bool _updateClicked = false;
        private bool _deleteClicked = false;
        private int _deleteItemIndex = -1;

        private TableStyle _tableStyle = new TableStyle();
        private TableItemStyle _headerStyle = new TableItemStyle();
        private TableItemStyle _itemStyle = new TableItemStyle();
        private TableItemStyle _alternatingItemStyle = new TableItemStyle();
        private TableItemStyle _pagerStyle = new TableItemStyle();

        private string _commandText = String.Empty;
        private int _pageSize = 10;
        private string _prevText = "<";
        private string _nextText = ">";
        private string _pagerText = "Page {0} of {1}";

        private string _errorMessage = String.Empty;


        //*********************************************************************
        //
        // ConnectionString Property
        //
        // The Connection String for the database. Notice that this must
        // be a valid SQL connection string since the SuperDataGrid uses
        // the SqlClient classes.
        //
        //*********************************************************************

        public string ConnectionString {
            get { 
                if (ViewState["ConnectionString"] == null)
                    return String.Empty;
                else
                    return (string)ViewState["ConnectionString"];
            }
            set { ViewState["ConnectionString"] = value; }
        }
    
    

        //*********************************************************************
        //
        // TableName Property
        //
        // The name of the database table to retrieve the data from. Instead
        // of using this property, you can use the CommandText property and
        // specify your own SELECT statement (with parameters).
        //
        //*********************************************************************

        public string TableName {
            get { 
                if (ViewState["TableName"] == null)
                    return String.Empty;
                else
                    return (string)ViewState["TableName"];
            }
            set { ViewState["TableName"] = value; }
        }    
    
        

        //*********************************************************************
        //
        // CommandText Property
        //
        // The CommandText property enables you to specify a SELECT statement
        // to use when retrieving database data.
        //
        //*********************************************************************

        public string CommandText {
            get { 
                if (ViewState["CommandText"] == null)
                    return String.Empty;
                else
                    return (string)ViewState["CommandText"];
            }
            set { ViewState["CommandText"] = value; }
        }    


        //*********************************************************************
        //
        // TableStyle Property
        //
        // The TableStyle object applied to the SuperDataGrid table. Use
        // this property to control the formatting of the main table.
        //
        //*********************************************************************

        public TableStyle TableStyle {
            get { return _tableStyle; }
        }



        //*********************************************************************
        //
        // HeaderStyle Property
        //
        // Use this property to control the formatting of the Header row.
        //
        //*********************************************************************

        public TableItemStyle HeaderStyle {
            get { return _headerStyle; }
        }



        //*********************************************************************
        //
        // ItemStyle Property
        //
        // Use this property to control the formatting of each row.
        //
        //*********************************************************************

        public TableItemStyle ItemStyle {
            get { return _itemStyle; }
        }
        


        //*********************************************************************
        //
        // AlternatingItemStyle Property
        //
        // Use this property to control the formatting of every other row.
        //
        //*********************************************************************

        public TableItemStyle AlternatingItemStyle {
            get { return _alternatingItemStyle; }
        }
                


        //*********************************************************************
        //
        // PagerStyle Property
        //
        // Use this property to control the formatting of the pager row.
        //
        //*********************************************************************

        public TableItemStyle PagerStyle {
            get { return _pagerStyle; }
        }


        //*********************************************************************
        //
        // SortColumn Property
        //
        // The column to sort on.
        //
        //*********************************************************************

        public string SortColumn {
            get { 
                if (ViewState["SortColumn"] == null)
                    return String.Empty;
                else
                    return (string)ViewState["SortColumn"];
            }
            set { ViewState["SortColumn"] = value; }
        }    



        //*********************************************************************
        //
        // SortOrder Property
        //
        // The Order to sort by (for example, ASC or DESC).
        //
        //*********************************************************************

        public string SortOrder {
            get { 
                if (ViewState["SortOrder"] == null)
                    return "ASC";
                else
                    return (string)ViewState["SortOrder"];
            }
            set { ViewState["SortOrder"] = value; }
        }    



        //*********************************************************************
        //
        // CurrentPageIndex Property
        //
        // The current page of records to display.
        //
        //*********************************************************************

        public int CurrentPageIndex {
            get { 
                if (ViewState["CurrentPageIndex"] == null)
                    return 1;
                else
                    return (int)ViewState["CurrentPageIndex"];
            }
            set { ViewState["CurrentPageIndex"] = value; }
        }    



        //*********************************************************************
        //
        // EnableSorting Property
        //
        // When true, columns can be sorted.
        //
        //*********************************************************************

        public bool EnableSorting {
            get { return _enableSorting; }
            set { _enableSorting = value; }
        }
        



        //*********************************************************************
        //
        // EnableEditing Property
        //
        // When true, rows can be edited.
        //
        //*********************************************************************

        public bool EnableEditing {
            get { return _enableEditing; }
            set { _enableEditing = value; }
        }
        

        //*********************************************************************
        //
        // EnableDeleting Property
        //
        // When true, rows can be deleted.
        //
        //*********************************************************************

        public bool EnableDeleting {
            get { return _enableDeleting; }
            set { _enableDeleting = value; }
        }




        //*********************************************************************
        //
        // PageSize Property
        //
        // The number of rows to display per page when paging is enabled.
        //
        //*********************************************************************

        public int PageSize {
            get { return _pageSize; }
            set { _pageSize = value; }
        }



        //*********************************************************************
        //
        // EditItemIndex Property
        //
        // The current row selected for editing.
        //
        //*********************************************************************

        public int EditItemIndex {
            get { 
                if (ViewState["EditItemIndex"] == null)
                    return -1;
                else
                    return (int)ViewState["EditItemIndex"];
            }
            set { ViewState["EditItemIndex"] = value; }
        }    



        //*********************************************************************
        //
        // LoadPostData Method
        //
        // When editing is enabled, and data is submitted, this method
        // updates the data stored in the colEditValues collection.
        //
        //*********************************************************************

        public bool LoadPostData(String postDataKey, NameValueCollection values) {
            foreach (string key in values.Keys)
                colEditValues[key] = values[key];
           return false;
        }


        //*********************************************************************
        //
        // RaisePostDataChangedEvent Method
        //
        // Required by the PostBackDataHandler interface.
        //
        //*********************************************************************

        public void RaisePostDataChangedEvent() {
        }


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -