整合重复代码,生成自定义的列(组件)整合重复代码,生成自定义的datagrid(组件)若要详

描述

代码

/////////checkboxcolumn.cs////////////////
using System;
using System.Web;
using System.Web.UI.WebControls; 

namespace MSDN
{
    /// 
    /// CheckBoxColumn 的摘要说明。
    /// 
    public class CheckBoxColumn : DataGridColumn 
    {
        private string m_allText;
        public CheckBoxColumn()
        {
            CheckBoxAllText="全选";
    
            
        }

        
        
        public string CheckBoxAllText 
        {
            get {return m_allText;}
            set {m_allText = value;}
        }



        public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
        {
            base.InitializeCell (cell, columnIndex, itemType);
            if (itemType==ListItemType.Header)
            {
                CheckBox chk=new CheckBox();
                Label l=new Label();
                l.Text=CheckBoxAllText;
                chk.Attributes.Add("onclick","javascript:SelectAllCheckboxes(this);");
                cell.Controls.Add(chk);
                cell.Controls.Add(l);
            }
            if (itemType==ListItemType.Item || itemType==ListItemType.AlternatingItem)
            {
                CheckBox chk=new CheckBox();
                //chk.CheckedChanged +=new EventHandler(chk_CheckedChanged);
                chk.AutoPostBack=false;
                chk.Attributes.Add("onclick","javascript:SelectOneRow(this);");
                cell.Controls.Add(chk);
            }
        }



        private void chk_CheckedChanged(object sender, EventArgs e)
        {
            //((DataGridItem)((CheckBox)sender).Parent.NamingContainer).ite
        }
    }
}


/////////////SuperDataGrid.cs///////////////
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Text;



namespace MSDN
{
    public class SuperDataGrid : System.Web.UI.WebControls.DataGrid
    {
            
        private int m_CreatePageTimes  = 0;
        private Color m_mouseover=Color.Empty;
        private Color m_mouseout=Color.Empty;
        private static DataView m_dataview ;
        public SuperDataGrid():base()    
        {
            AllowPaging = true;
            PageIndexChanged+=new DataGridPageChangedEventHandler(HyperDataGrid_PageIndexChanged);
            ItemCommand +=new DataGridCommandEventHandler(HyperDataGrid_ItemCommand);
            ItemCreated +=new DataGridItemEventHandler(HyperDataGrid_ItemCreated);
            ItemDataBound +=new DataGridItemEventHandler(HyperDataGrid_ItemDataBound);
            SortCommand +=new DataGridSortCommandEventHandler(HyperDataGrid_SortCommand);
            
            }

        public void SetCheckBoxClintScript()
        {    
            Page.RegisterClientScriptBlock("SelectOneRow",RegisterScript(Color.Pink ));
        }
    
        string RegisterScript(Color color)
        {
            StringBuilder str=new StringBuilder();
            str.Append("");
            return str.ToString();
        }

        

            

        //declare dataview
        public DataView m_DataView
        {
            get{return m_dataview;}
            set{m_dataview=value;}
        }

        public Color OnMouseOverColor
        {
            get{return m_mouseover;}
            set{m_mouseover=value;}
        }

        public Color OnMouseOutColor
        {
            get{return m_mouseout;}
            set{m_mouseout=value;}
        }

        public event EventHandler UpdateView;//fire databinding event
        public event EventHandler InsertView;
        private void OnUpdateView()
        {
            if (UpdateView != null)
                UpdateView(this, EventArgs.Empty); 
        }

        private void OnInsertView()
        {
            if (InsertView != null)
                InsertView(this, EventArgs.Empty); 
        }

        private void HyperDataGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            DataGrid detGrid = (DataGrid) source;
            detGrid.CurrentPageIndex = e.NewPageIndex;

            m_CreatePageTimes=0;
            OnUpdateView();
        }

        private void HyperDataGrid_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName=="alpha")
            {
                
                ViewState["alpha"]=e.CommandArgument;
                m_CreatePageTimes=0;
                CurrentPageIndex=0;
                OnUpdateView();
            }

            if (e.CommandName=="Cancle")
            {
                ShowFooter=false;
            }
            if (e.CommandName=="Insert")
            {
                OnInsertView();
                ShowFooter=false;
            }
        }

        private void HyperDataGrid_ItemCreated(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType==ListItemType.Header)
            {
                //排序
                foreach(TableCell c in e.Item.Cells)
                {
                    
                    if (c.Controls.Count >0 && c.Controls[0].GetType().ToString()=="System.Web.UI.WebControls.DataGridLinkButton")
                    {
                        if (((LinkButton)c.Controls[0]).Text==(string)ViewState["Sort"])
                        {
                            Label l=new Label();
                            l.Font.Name="Webdings";
                            if ((string)ViewState["orderby"]=="asc")
                                l.Text="5";
                            else
                                l.Text="6";
                            c.Controls.Add(l);
                        }
                    }
                }
            }

            if (e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
            {
                //System.Drawing.Color col=Color.FromArgb((int)e.Item.BackColor.R,(int)e.Item.BackColor.G,(int)e.Item.BackColor.B)
            
                //string str=e.Item.BackColor;
                if (OnMouseOverColor==Color.Empty)
                    OnMouseOverColor=SelectedItemStyle.BackColor;
                if (OnMouseOutColor==Color.Empty)
                    OnMouseOutColor=ItemStyle.BackColor;
            /*    
                e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='pink'");//"+OnMouseOverColor+"
                e.Item.Attributes.Add("onmouseout","this.style.backgroundColor='#"+ConvertHex(OnMouseOutColor)+"'");//"+OnMouseOutColor+"
    */

            }
    
            if (e.Item.ItemType==ListItemType.Footer)
            {
                int i=0;LinkButton lb;
                foreach(TableCell c in e.Item.Cells)
                {

                    if (i==0) 
                        c.Controls.Add(new CheckBox());
                    else if (i==e.Item.Cells.Count-2)
                    {
                        lb=new LinkButton();
                        lb.Text="插入";
                        lb.CommandName="Insert";
                        c.Controls.Add(lb);
                    }
                    else if (i==e.Item.Cells.Count-1)
                    {
                        lb=new LinkButton();
                        lb.Text="取消";
                        lb.CommandName="Cancle";
                        c.Controls.Add(lb);
                    }
                    else    
                        c.Controls.Add(new TextBox());
                    i++;
                }
            }

            if (e.Item.ItemType==ListItemType.Pager)
            {
                if (m_CreatePageTimes==0)
                {
                    e.Item.Cells[0].Controls.Clear();
                    LinkButton l;
                    
                    for (int i=65;i



    
        WebForm4
        
        
        
        
    
    
        
        
             
            " DataKeyField="CustomerID" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BorderStyle="None" BorderWidth="1px" BackColor="White" BorderColor="#CC9966" CellPadding="4" OnMouseOutColor="White" OnMouseOverColor="255, 204, 102">
                
                
                
                
                
                
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                
            
        
    



///////////调用组件的后台代码.cs///////////
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.Web.UI.HtmlControls;

namespace DataGridDemoCS
{
    /// 
    /// WebForm4 的摘要说明。
    /// 
    public class WebForm4 : System.Web.UI.Page
    {
        protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
        protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
        protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
        protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
        protected System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
        protected System.Data.SqlClient.SqlConnection sqlConnection1;
        protected DataGridDemoCS.dsCustomers dsCustomers1;
        protected MSDN.SuperDataGrid SuperDataGrid1;
        protected System.Data.DataView dv;


        //protected HyperDataGrid HyperDataGrid2;//HyperDataGrid HyperDataGrid1;
    
        private void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            if (!Page.IsPostBack)
            {
                
                bindData();
            }
        }

        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// 
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// 
        private void InitializeComponent()
        {    
            this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
            this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
            this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
            this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
            this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
            this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
            this.dsCustomers1 = new DataGridDemoCS.dsCustomers();
            this.dv = new System.Data.DataView();
            ((System.ComponentModel.ISupportInitialize)(this.dsCustomers1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dv)).BeginInit();
            this.SuperDataGrid1.UpdateView += new System.EventHandler(this.SuperDataGrid1_UpdateView);
            // 
            // sqlDataAdapter1
            // 
            this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
            this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
            this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
            this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
                                                                                                      new System.Data.Common.DataTableMapping("Table", "Customers", new System.Data.Common.DataColumnMapping[] {
                                                                                                                                                                                                                   new System.Data.Common.DataColumnMapping("CustomerID", "CustomerID"),
                                                                                                                                                                                                                   new System.Data.Common.DataColumnMapping("CompanyName", "CompanyName"),
                                                                                                                                                                                                                   new System.Data.Common.DataColumnMapping("ContactName", "ContactName"),
                                                                                                                                                                                                                   new System.Data.Common.DataColumnMapping("ContactTitle", "ContactTitle"),
                                                                                                                                                                                                                   new System.Data.Common.DataColumnMapping("Address", "Address"),
                                                                                                                                                                                                                   new System.Data.Common.DataColumnMapping("City", "City"),
                                                                                                                                                                                                                   new System.Data.Common.DataColumnMapping("Region", "Region"),
                                                                                                                                                                                                                   new System.Data.Common.DataColumnMapping("PostalCode", "PostalCode"),
                                                                                                                                                                                                                   new System.Data.Common.DataColumnMapping("Country", "Country"),
                                                                                                                                                                                                                   new System.Data.Common.DataColumnMapping("Phone", "Phone"),
                                                                                                                                                                                                                   new System.Data.Common.DataColumnMapping("Fax", "Fax")})});
            this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
            // 
            // sqlDeleteCommand1
            // 
            this.sqlDeleteCommand1.CommandText = @"DELETE FROM Customers WHERE (CustomerID = @Original_CustomerID) AND (Address = @Original_Address OR @Original_Address IS NULL AND Address IS NULL) AND (City = @Original_City OR @Original_City IS NULL AND City IS NULL) AND (CompanyName = @Original_CompanyName) AND (ContactName = @Original_ContactName OR @Original_ContactName IS NULL AND ContactName IS NULL) AND (ContactTitle = @Original_ContactTitle OR @Original_ContactTitle IS NULL AND ContactTitle IS NULL) AND (Country = @Original_Country OR @Original_Country IS NULL AND Country IS NULL) AND (Fax = @Original_Fax OR @Original_Fax IS NULL AND Fax IS NULL) AND (Phone = @Original_Phone OR @Original_Phone IS NULL AND Phone IS NULL) AND (PostalCode = @Original_PostalCode OR @Original_PostalCode IS NULL AND PostalCode IS NULL) AND (Region = @Original_Region OR @Original_Region IS NULL AND Region IS NULL)";
            this.sqlDeleteCommand1.Connection = this.sqlConnection1;
            this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CustomerID", System.Data.SqlDbType.NVarChar, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerID", System.Data.DataRowVersion.Original, null));
            this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Address", System.Data.SqlDbType.NVarChar, 60, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Address", System.Data.DataRowVersion.Original, null));
            this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_City", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "City", System.Data.DataRowVersion.Original, null));
            this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CompanyName", System.Data.SqlDbType.NVarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CompanyName", System.Data.DataRowVersion.Original, null));
            this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ContactName", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ContactName", System.Data.DataRowVersion.Original, null));
            this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ContactTitle", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ContactTitle", System.Data.DataRowVersion.Original, null));
            this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Country", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Country", System.Data.DataRowVersion.Original, null));
            this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Fax", System.Data.SqlDbType.NVarChar, 24, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Fax", System.Data.DataRowVersion.Original, null));
            this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Phone", System.Data.SqlDbType.NVarChar, 24, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Phone", System.Data.DataRowVersion.Original, null));
            this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_PostalCode", System.Data.SqlDbType.NVarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "PostalCode", System.Data.DataRowVersion.Original, null));
            this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Region", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Region", System.Data.DataRowVersion.Original, null));
            // 
            // sqlConnection1
            // 
            this.sqlConnection1.ConnectionString = "workstation id=\"FANCY-FAMILY\";packet size=4096;user id=sa;data source=\"FANCY-FAMI" +
                "LY\";persist security info=False;initial catalog=Northwind";
            // 
            // sqlInsertCommand1
            // 
            this.sqlInsertCommand1.CommandText = @"INSERT INTO Customers(CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax) VALUES (@CustomerID, @CompanyName, @ContactName, @ContactTitle, @Address, @City, @Region, @PostalCode, @Country, @Phone, @Fax); SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax FROM Customers WHERE (CustomerID = @CustomerID)";
            this.sqlInsertCommand1.Connection = this.sqlConnection1;
            this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("<span style="COL
Published At
Categories with Web编程
Tagged with
comments powered by Disqus