DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解 >> DataGrid控件實現無刷新增加刪除等操作
DataGrid控件實現無刷新增加刪除等操作
編輯:AJAX詳解     
現在的網絡上最流行的最火的技術就是AJax了,不刷新技術,給客戶帶來的最新體驗。google等在一些大的網站上已經都應用了。在平時開發的時候要做到DataGrid控件不刷新有點麻煩,我就把我的實現寫成了一個控件(現在 MS在Atlas裡面也實現了,不過在vs.Net2003裡面還是很麻煩)
 下面是實現效果:

可以實現增加,刪除,全選擇,顯示行號等功能
主要代碼:
  1         #region 用戶數據源或者VIEwState創建控件層次結構
  2         protected virtual void CreateControlHIErarchy(bool useDataSource)
  3         {
  4             IEnumerable dataSource = null ;
  5 
  6             int rowCount = 0;
  7             int columnCount = 0;
  8 
  9             if(useDataSource)
 10             {
 11                 dataSource = GetDataSource();
 12             }
 13             else
 14             {
 15                 dataSource = new object[(int)VIEwState["RowCount"]];
 16             }
 17 
 18             if( dataSource != null)
 19             {
 20                 Table table = new Table();
 21 
 22 
 23                 #region 增加表的樣式 
 24                 table.Attributes.Add("Cellpadding","0");
 25                 table.Attributes.Add("Cellspacing","0");
 26                 table.Attributes.Add("border","0");
 27                 table.Attributes.Add("style",this._TableStyle);
 28                 table.ID = this.UniqueID+"Table";
 29                 table.Width = this.Height;
 30                 table.Width = this.Width;
 31                 
 32                 #endregion
 33 
 34                 TableRowCollection rows = table.Rows;
 35 
 36                 //增加控件 
 37                 Controls.Add(table);
 38 
 39                 bool createdHeader = false;
 40 
 41                 //顯示行號
 42                 int iCount = 1;
 43 
 44                 //如果為空行就只加表頭
 45                 System.Data.DataVIEw dv = (System.Data.DataVIEw)dataSource;
 46 
 47                 if(dv.Table.Rows.Count==0)
 48                 {
 49                     #region 增加表頭
 50                     if(createdHeader == false)
 51                     {
 52                         TableRow headerRow = new TableRow();
 53 
 54                         //表頭樣式
 55                         headerRow.Attributes.Add("bgcolor",_TableHeadBGColor);
 56 
 57                         TableCellCollection headerCells = headerRow.Cells;
 58 
 59                         columnCount = dv.Table.Columns.Count;
 60                     
 61                         //是否顯示每行前的索引值
 62                         if(_ShowIndex)
 63                         {
 64 
 65                             TableCell cell = new TableCell();
 66                             cell.Attributes.Add("style","width:20px;border-left:1 #FFFFFF solid;border-top:1 #FFFFFF solid;border-bottom:1 #d8d8d8 solid;border-right:1 #d8d8d8 solid;");
 67                             cell.Wrap = _wrap;
 68                             cell.Attributes.Add("align","center");
 69                             cell.Text = " ";
 70                             headerCells.Add(cell);
 71                         }
 72 
 73                         for(int i=0;i<columnCount;i++)
 74                         {
 75                             //TableHeaderCell cell = new TableHeaderCell();
 76                             TableCell cell = new TableCell();
 77                             cell.Attributes.Add("style",this._TableTHeadCellStyle);
 78                             cell.Attributes.Add("align","center");
 79                             cell.Wrap = _wrap;
 80                             if(this._EnableColumnWith)
 81                             {
 82 
 83                                 cell.Width = this._ColumnWith[i];
 84                             }
 85 
 86                             if(_CheckBoxVisible)    //顯示CheckBox 
 87                             {
 88                                 if(i == this._ColumnNum)
 89                                 {
 90                                     cell.Text = """+this.UniqueID+"checkAll\" onclick=\"chooseallCheckBox(this,'"+this.UniqueID+"HBCheckAll');\" src=\""+ImgSrcURL+"\">";//加入全選圖片
 91                                 }
 92                                 else
 93                                 {
 94                                     cell.Text = _ColumnNames[i];
 95                                 }
 96                             }//沒有全選
 97                             else
 98                             {
 99                                 cell.Text = _ColumnNames[i];
100                             }
101                             
102                             headerCells.Add(cell);
103                         }
104                         createdHeader = true;
105                         rows.Add(headerRow);
106                         
107                     }
108                     #endregion
109                 }
110                 else
111                 {
112                     for(int h=0;h<dv.Table.Rows.Count;h++)
113                     {
114                         
115                         #region 增加表頭
116                         if(createdHeader == false)
117                         {
118                             TableRow headerRow = new TableRow();
119 
120                             //表頭樣式
121                             headerRow.Attributes.Add("bgcolor",_TableHeadBGColor);
122 
123                             TableCellCollection headerCells = headerRow.Cells;
124                         
125 
126                             //ColumnCount
127                             if(useDataSource)
128                             {
129                                 
130                                 columnCount = dv.Table.Columns.Count;
131                             }
132                             else
133                             {
134                                 columnCount = (int)VIEwState["ColumnCount"];
135                             }
136 
137                     
138                             //是否顯示每行前的索引值
139                             if(_ShowIndex)
140                             {
141 
142                                 TableCell cell = new TableCell();
143                                 cell.Attributes.Add("style","width:20px;border-left:1 #FFFFFF solid;border-top:1 #FFFFFF solid;border-bottom:1 #d8d8d8 solid;border-right:1 #d8d8d8 solid;");
144                                 cell.Wrap = _wrap;
145                                 cell.Attributes.Add("align","center");
146                                 cell.Text = " ";
147                                 headerCells.Add(cell);
148                             }
149 
150                             for(int i=0;i<columnCount;i++)
151                             {
152                                 //TableHeaderCell cell = new TableHeaderCell();
153                                 TableCell cell = new TableCell();
154                                 cell.Attributes.Add("style",this._TableTHeadCellStyle);
155                                 cell.Attributes.Add("align","center");
156                                 cell.Wrap = _wrap;
157 
158                                 if(this._EnableColumnWith)
159                                 {
160 
161                                     cell.Width = this._ColumnWith[i];
162                                 }
163 
164                                 if(_CheckBoxVisible)    //顯示CheckBox 
165                                 {
166                                     if(i == this._ColumnNum)
167                                     {
168                                         cell.Text = """+this.UniqueID+"checkAll\" onclick=\"chooseallCheckBox(this,'"+this.UniqueID+"HBCheckAll');\" src=\""+ImgSrcURL+"\">";//加入全選圖片
169                                     }
170                                     else
171                                     {
172                                         if(useDataSource)
173                                         {
174                                             cell.Text = _ColumnNames[i];// dv.Table.Rows[h][i].ToString();
175                                         }
176                                     }
177                                 }//沒有全選
178                                 else
179                                 {
180                                     if(useDataSource)
181                                     {
182                                         cell.Text = _ColumnNames[i];
183                                     }
184                                 }
185                             
186                                 headerCells.Add(cell);
187                             }
188                             createdHeader = true;
189                             rows.Add(headerRow);
190                         
191                         }
192                         #endregion
193 
194                         #region 增加行
195                 
196                         TableRow row = new TableRow();
197                         TableCellCollection cells = row.Cells;
198                         //增加行的樣式
199                         row.Attributes.Add("style",_TableTBodyRowStyle);
200 
201                         //是否顯示每行前面的索引號
202                         if(_ShowIndex)
203                         {
204 
205                             TableCell cell = new TableCell();
206                             cell.Attributes.Add("style","BORDER-RIGHT:#d8d8d8 1px solid;BORDER-TOP:#ffffff 1px solid;BORDER-LEFT:#ffffff 1px solid;WIDTH:20px;BORDER-BOTTOM:#d8d8d8 1px solid");
207                             cell.Wrap = _wrap;
208                             cell.Attributes.Add("bgcolor",this._TableHeadBGColor);
209                             cell.Attributes.Add("align","center");
210                             cell.Text=iCount.ToString();
211                             cells.Add(cell);
212                             iCount++;
213                         }
214 
215                         for(int i=0;i<columnCount;i++)
216                         {
217                             TableCell cell = new TableCell();
218                             cell.Attributes.Add("style",this._TableTBodyCellStyle);
219 
220                             if(this._EnableColumnWith)
221                             {
222                                 cell.Width = this._ColumnWith[i];
223                             }
224 
225 
226                             if(_CheckBoxVisible)    //顯示CheckBox 
227                             {
228                                 if(useDataSource)
229                                 {
230 
231                                     if(i == this._ColumnNum)
232                                     {
233                                         cell.Text = "";
234                                     }
235                                     else
236                                     {
237                                         cell.Text = dv.Table.Rows[h][i].ToString();
238                                     }
239                                     //增加單擊事件
240                                     if(_ClickItem.EnableClick)    //是否啟用單擊事件
241                                     {
242                                         if(_ClickItem.ClickColumnItem!=-1 &&i ==_ClickItem.ClickColumnItem)
243                                         {                                            
244                                             cell.Attributes.Add("onclick","OnClickColumn();");
245                                             cell.ID = dv.Table.Rows[h][i].ToString();
246                                         }
247                                     }
248                                 }
249                             }
250                             else
251                             {
252                                 if(useDataSource)
253                                 {
254                                     
255                                     cell.Text = dv.Table.Rows[h][i].ToString();
256                                 }
257                             }
258                             cells.Add(cell);
259                         
260                         }
261 
262                         rows.Add(row);
263                         rowCount ++;
264                         #endregion
265 
266                         if(useDataSource)
267                         {
268                             VIEwState["RowCount"] = rowCount;
269                             VIEwState["ColumnCount"] = columnCount;
270                         }
271                     }
272                 }
273 
274             }
275         }
276         #endregion
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved