测试1

   1using System;
   2using System.Data;
   3using System.Collections;
   4using System.Collections.Generic;
   5using System.Text;
   6using System.Web.UI;
   7using System.Web.UI.WebControls;
   8using System.ComponentModel;
   9using System.Collections.Specialized;
  10
  11namespace Necas.Web.iExaming
  12{
  13    /// <summary>
  14    /// 继承自GridView
  15    /// </summary>

  16    [
  17    ToolboxData(@"<{0}:GridView runat='server'></{0}:GridView>"),
  18    ParseChildren(true),
  19    PersistChildren(false)
  20    ]
  21    public class GridView : System.Web.UI.WebControls.GridView, IPostBackDataHandler
  22    {
  23        public GridView()
  24        {
  25            this.PreRender += new EventHandler(GridView_PreRender);
  26            this.AllowPaging = true;
  27            this.AllowSorting = true;
  28            Width = Unit.Percentage(100);
  29            CssClass="GridView";
  30            CellPadding = 3;
  31            AutoGenerateColumns = false;
  32        }

  33
  34        private void GridView_PreRender(object sender, EventArgs e)
  35        {
  36            改变通过CheckBox选中的行的样式 注册客户端脚本
  49
  50            自动列宽调整
  75
  76            固定行、列后使页面保存滚动条的位置信息
  94        }

  95
  96        /// <summary>
  97        /// Render
  98        /// </summary>
  99        /// <param name="writer"></param>

 100        protected override void Render(HtmlTextWriter writer)
 101        {
 102            为了固定行、列要 用div包围GridView 
部分
 124
 125            base.Render(writer);
 126
 127            为了固定行、列要 用div包围GridView 
部分

 134        }

 135
 136        如果固定行、列的话,用于获取滚动条的位置,要继承IPostBackDataHandler接口
 151
 152        公共属性
 176
 177        只可以访问的公共属性
 194
 195        私有变量
 205
 206
 207
 208
 209
 210        protected override void OnPageIndexChanging(GridViewPageEventArgs e)
 211        {
 212            PageIndex = 0;
 213            OnDataBindPaged();
 214            OnMultGridviewDataBindPaged(this);
 215        }

 216        protected override void OnSorting(GridViewSortEventArgs e)
 217        {
 218            if (ViewState["SortDirection"== null)
 219            {
 220                ViewState["SortDirection"= "asc";
 221            }

 222            else
 223            {
 224                if (!ViewState["SortExpression"].ToString().Equals(e.SortExpression))
 225                {
 226                    ViewState["SortDirection"= "asc";
 227                }

 228                else if (ViewState["SortDirection"].ToString().Equals("asc"))
 229                {
 230                    ViewState["SortDirection"= "desc";
 231                }

 232                else
 233                {
 234                    ViewState["SortDirection"= "asc";
 235                }

 236            }

 237            ViewState["SortExpression"= e.SortExpression;
 238            _PageIndex = 0;
 239            OnDataBindPaged();
 240            OnMultGridviewDataBindPaged(this);
 241        }

 242        protected override void OnRowCommand(GridViewCommandEventArgs e)
 243        {
 244            if (e.CommandName.Equals("Page"))
 245            {
 246                _PageIndex += PageIndex;
 247                if (e.CommandArgument.Equals("First")) _PageIndex = 0;
 248                else if (e.CommandArgument.Equals("Prev")) _PageIndex--;
 249                else if (e.CommandArgument.Equals("Next")) _PageIndex++;
 250                else if (e.CommandArgument.Equals("Last")) _PageIndex = _PageCount - 1;
 251                else _PageIndex = Int16.Parse(e.CommandArgument.ToString()) - 1;
 252            }

 253            base.OnRowCommand(e);
 254        }

 255        protected override void OnRowDataBound(GridViewRowEventArgs e)
 256        {
 257            base.OnRowDataBound(e);
 258            e.Row.Attributes.Add("id", e.Row.ClientID);
 259
 260            if (e.Row.RowType == DataControlRowType.DataRow)
 261            {
 262                鼠标经过行时变化行的样式
 296
 297                EllipsisRow设置
 332                // GridViewRow的每个TableCell
 333                for (int i = 0; i < e.Row.Cells.Count; i++)
 334                {
 335                    每行复选框的全选与取消全选 DataRow部分
 345                }

 346
 347                改变通过CheckBox选中的行的样式
 400            }

 401            else if (e.Row.RowType == DataControlRowType.Header)
 402            {
 403                // GridViewRow的每个TableCell
 404                for (int i = 0; i < e.Row.Cells.Count; i++)
 405                {
 406                    排序时在标题处标明是升序还是降序
 463                }

 464            }

 465
 466            /***********************************************************/
 467            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
 468            {
 469                固定行、列 DataRow和Header部分
 530            }

 531        }

 532
 533        private  string DBToHtmlFlagTextBox(string sourcestr)
 534        {
 535            sourcestr = sourcestr.TrimEnd();
 536            sourcestr = System.Web.HttpUtility.HtmlDecode(sourcestr);
 537            sourcestr = sourcestr.Replace("▲→◆""\r\n");
 538            return sourcestr;
 539        }

 540        protected override void InitializePager(GridViewRow row, int columnSpan, PagedDataSource pagedDataSource)
 541        {
 542            Table PageTable = new Table();
 543            TableRow PageTableRow = new TableRow();
 544            TableCell tc = new TableCell();
 545            tc.Style.Add("text-align""left");
 546            int PageCur = _PageIndex + PageIndex;
 547
 548            添加首页,上一页按钮
 585
 586            显示数字分页按钮
 622
 623            添加下一页,尾页按钮
 660
 661            PageTableRow.Cells.Add(tc);
 662            TableCell tcr = new TableCell();
 663
 664            当前页数/总页数 (显示总记录数)
 679
 680            PageTable.Rows.Add(PageTableRow);
 681            PageTable.Style.Add("width""100%");
 682
 683            row.Controls.AddAt(0new TableCell());
 684            row.Cells[0].ColumnSpan = Columns.Count;
 685            row.Cells[0].Controls.AddAt(0, PageTable);
 686        }

 687
 688        protected virtual void OnDataBindPaged()
 689        {
 690            if (Events != null)
 691            {
 692                DataBindPagedEventHandler eh = (DataBindPagedEventHandler)Events[DataBindPagedEvent];
 693                MultGridviewDataBindPagedEventHandler eh2 = (MultGridviewDataBindPagedEventHandler)Events[MultGridviewDataBindPagedEvent];
 694                if (eh != null)
 695                
 696                    eh(); 
 697                }

 698                else if (eh2 != null)
 699                { }
 700                else DataBind2();
 701            }

 702        }

 703        protected virtual void OnMultGridviewDataBindPaged(object sender)
 704        {
 705            if (Events != null)
 706            {
 707                MultGridviewDataBindPagedEventHandler eh2 = (MultGridviewDataBindPagedEventHandler)Events[MultGridviewDataBindPagedEvent];
 708                DataBindPagedEventHandler eh = (DataBindPagedEventHandler)Events[DataBindPagedEvent];
 709                if (eh2 != null) eh2(sender);
 710                else if (eh != null)
 711                { }
 712                else DataBind2();
 713            }

 714        }

 715
 716        private void DataBind2()
 717        {
 718            if (!String.IsNullOrEmpty(DataSourceID))
 719            {
 720                PageIndex += _PageIndex;
 721                _PageIndex = 0;
 722                DataBind();
 723                return;
 724            }

 725
 726            if (DataSource == nullreturn;
 727
 728            DataView dv = new DataView();
 729            if (DataSource is DataView)
 730                dv = DataSource as DataView;
 731            else
 732                dv = (DataSource as DataTable).DefaultView;
 733
 734            if (ViewState["SortDirection"!= null && ViewState["SortExpression"!= null)
 735            {
 736                dv.Sort = ViewState["SortExpression"].ToString() + " " + ViewState["SortDirection"].ToString();
 737            }

 738            this.DataSource = dv;
 739            DataBind();
 740        }

 741       
 742        //add by chenj start 2009/02/05
 743        /// <summary>
 744        /// 将GridView中选中的行高亮显示
 745        /// </summary>
 746        /// <param name="selectedKey">string from viewstate</param>
 747        /// <param name="rowKey">the key of the table</param>

 748        public void SetSelectedRow(String selectedKey, String rowKey)
 749        {
 750            DataTable dt = new DataTable();
 751            if (DataSource is DataTable)
 752                dt = DataSource as DataTable;
 753            else
 754                dt = (DataSource as DataView).Table;
 755            this.SelectedIndex = -1;
 756            if (selectedKey != null)
 757            {
 758                string SelectID = selectedKey.Trim();
 759                for (int i = 0; i < dt.Rows.Count; i++)
 760                {
 761                    if (dt.Rows[i][rowKey].ToString().Trim() == SelectID)
 762                    {
 763                        this.SelectedIndex = i;
 764                        break;
 765                    }

 766                }

 767            }

 768        }

 769        /// <summary>
 770        /// 将GridView中选中的行高亮显示,两个参数决定选中
 771        /// </summary>
 772        /// <param name="selectedKey">string from viewstate</param>
 773        /// <param name="rowKey">the key of the table</param>

 774        public void SetSelectedRow(String[] selectedKey, String[] rowKey)
 775        {
 776            DataTable dt = new DataTable();
 777            if (DataSource is DataTable)
 778                dt = DataSource as DataTable;
 779            else
 780                dt = (DataSource as DataView).Table;
 781
 782            this.SelectedIndex = -1;
 783
 784            if (selectedKey != null || rowKey != null)
 785            {
 786                if (selectedKey.Length != rowKey.Length)
 787                    return;
 788                for (int i = 0; i < dt.Rows.Count; i++)
 789                {
 790                    for (int j = 0; j < selectedKey.Length; j++)
 791                    {
 792                        if (dt.Rows[i][rowKey[j].ToString().Trim()].ToString().Trim() != Convert.ToString(selectedKey[j]).Trim())
 793                        {
 794                            break;
 795                        }

 796                        if (j == selectedKey.Length-1)
 797                        {
 798                            this.SelectedIndex = i;
 799                            return;
 800                        }

 801                    }

 802                }

 803            }

 804        }

 805        //add by chenj end 2009/02/05
 806
 807        /// <summary>
 808        /// GridView中,其DataSource中指定的列的相等时,合并Gridview中指定的对应列
 809        /// </summary>
 810        /// <param name="ColumnName">DataSource中指定列名</param>
 811        /// <param name="RowIndex">Gridview中指定列的index</param>

 812        public void SetRowSpan(String ColumnName, int[] RowIndex)
 813        {
 814            DataTable dt = new DataTable();
 815            if (DataSource is DataTable)
 816                dt = DataSource as DataTable;
 817            else
 818                dt = (DataSource as DataView).Table;
 819
 820            if (dt.Rows.Count < 2return;
 821
 822            int row = 0;
 823            for (int i = 1; i < dt.Rows.Count; i++)
 824            {
 825                if (Convert.ToString(dt.Rows[i][ColumnName]) != Convert.ToString(dt.Rows[i - 1][ColumnName]))
 826                {
 827                    row = i;
 828                }

 829                else
 830                {
 831                    for (int j = 0; j < RowIndex.Length;j++ )
 832                    {
 833                        if (this.Rows[row].Cells[RowIndex[j]].RowSpan == 0)
 834                        {
 835                            this.Rows[row].Cells[RowIndex[j]].RowSpan++;
 836                        }

 837                        this.Rows[row].Cells[RowIndex[j]].RowSpan++;
 838                        this.Rows[i].Cells[RowIndex[j]].Visible = false;
 839                    }

 840                }

 841            }

 842        }

 843
 844
 845        /// <summary>
 846        /// GridView中,其DataSource中指定的列的相等时,合并Gridview中指定的对应列
 847        /// </summary>
 848        /// <param name="ColumnName">DataSource中指定列名</param>
 849        /// <param name="RowIndex">Gridview中指定列的index</param>

 850        public void SetRowSpan(String ColumnName, int RowIndex)
 851        {
 852            DataTable dt = new DataTable();
 853            if (DataSource is DataTable)
 854                dt = DataSource as DataTable;
 855            else
 856                dt = (DataSource as DataView).Table;
 857
 858            if (dt.Rows.Count < 2return;
 859
 860            int row = 0;
 861            for (int i = 1; i < dt.Rows.Count; i++)
 862            {
 863                if (Convert.ToInt32(dt.Rows[i][ColumnName]) != Convert.ToInt32(dt.Rows[i - 1][ColumnName]))
 864                {
 865                    row = i;
 866                }

 867                else
 868                {
 869                    if (this.Rows[row].Cells[RowIndex].RowSpan == 0)
 870                    {
 871                        this.Rows[row].Cells[RowIndex].RowSpan++;
 872                    }

 873                    this.Rows[row].Cells[RowIndex].RowSpan++;
 874                    this.Rows[i].Cells[RowIndex].Visible = false;
 875                }

 876            }

 877        }

 878        
 879        公共属性
1174
1175        // add by jialiang
1176        protected override object SaveViewState()
1177        {
1178            object[] ret = new object[2];
1179            ret[0= base.SaveViewState();
1180            if (ViewState["ImageUrl"!= null)
1181                ret[1= ViewState["ImageUrl"];
1182            return ret;
1183
1184        }

1185
1186        protected override void LoadViewState(object savedState)
1187        {
1188            object[] stateArray = (object[])savedState;
1189            base.LoadViewState(stateArray[0]);
1190            if (null != stateArray[1])
1191            {
1192                int field = GetSortField();
1193                if (field >= 0)
1194                {
1195                    Image img = new Image();
1196                    img.ImageUrl = (string)stateArray[1];
1197                    if (this.HeaderRow != null)
1198                    {
1199                        this.HeaderRow.Cells[field].Controls.Add(img);
1200                    }

1201                }

1202            }

1203        }

1204        
1205        /// <summary>
1206        /// 得到排序列的列索引
1207        /// </summary>

1208        private int GetSortField()
1209        {
1210            int i = 0;
1211            foreach (DataControlField field in this.Columns)
1212            {
1213                if (field.SortExpression == this.SortField)
1214                {
1215                    return i;
1216                }

1217                i++;
1218            }

1219            return -1;
1220        }

1221        // end add by jialiang
1222        private ArrayList cachedSelectedIndices;
1223
1224    }

1225}

1226
posted @ 2009-05-31 15:56  N0Game  阅读(228)  评论(0编辑  收藏  举报