GridView.cs 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042
  1. //
  2. // System.Web.UI.WebControls.GridView.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System;
  30. using System.Collections;
  31. using System.Collections.Specialized;
  32. using System.ComponentModel;
  33. using System.Web.UI;
  34. using System.Security.Permissions;
  35. using System.Text;
  36. using System.IO;
  37. using System.Reflection;
  38. namespace System.Web.UI.WebControls
  39. {
  40. [SupportsEventValidation]
  41. [DesignerAttribute ("System.Web.UI.Design.WebControls.GridViewDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  42. [ControlValuePropertyAttribute ("SelectedValue")]
  43. [DefaultEventAttribute ("SelectedIndexChanged")]
  44. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  45. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  46. public class GridView: CompositeDataBoundControl, ICallbackEventHandler, ICallbackContainer, IPostBackEventHandler, IPostBackContainer
  47. {
  48. Table table;
  49. GridViewRowCollection rows;
  50. GridViewRow bottomPagerRow;
  51. GridViewRow topPagerRow;
  52. IOrderedDictionary currentEditRowKeys;
  53. IOrderedDictionary currentEditNewValues;
  54. IOrderedDictionary currentEditOldValues;
  55. ITemplate pagerTemplate;
  56. ITemplate emptyDataTemplate;
  57. PropertyDescriptor[] cachedKeyProperties;
  58. // View state
  59. DataControlFieldCollection columns;
  60. PagerSettings pagerSettings;
  61. TableItemStyle alternatingRowStyle;
  62. TableItemStyle editRowStyle;
  63. TableItemStyle emptyDataRowStyle;
  64. TableItemStyle footerStyle;
  65. TableItemStyle headerStyle;
  66. TableItemStyle pagerStyle;
  67. TableItemStyle rowStyle;
  68. TableItemStyle selectedRowStyle;
  69. ArrayList _dataKeyArrayList;
  70. DataKeyArray keys;
  71. DataKey oldEditValues;
  72. AutoGeneratedFieldProperties[] autoFieldProperties;
  73. string [] dataKeyNames = null;
  74. readonly string[] emptyKeys = new string[0];
  75. private static readonly object PageIndexChangedEvent = new object();
  76. private static readonly object PageIndexChangingEvent = new object();
  77. private static readonly object RowCancelingEditEvent = new object();
  78. private static readonly object RowCommandEvent = new object();
  79. private static readonly object RowCreatedEvent = new object();
  80. private static readonly object RowDataBoundEvent = new object();
  81. private static readonly object RowDeletedEvent = new object();
  82. private static readonly object RowDeletingEvent = new object();
  83. private static readonly object RowEditingEvent = new object();
  84. private static readonly object RowUpdatedEvent = new object();
  85. private static readonly object RowUpdatingEvent = new object();
  86. private static readonly object SelectedIndexChangedEvent = new object();
  87. private static readonly object SelectedIndexChangingEvent = new object();
  88. private static readonly object SortedEvent = new object();
  89. private static readonly object SortingEvent = new object();
  90. // Control state
  91. int pageIndex;
  92. int selectedIndex = -1;
  93. int editIndex = -1;
  94. SortDirection sortDirection = SortDirection.Ascending;
  95. string sortExpression;
  96. public GridView ()
  97. {
  98. }
  99. public event EventHandler PageIndexChanged {
  100. add { Events.AddHandler (PageIndexChangedEvent, value); }
  101. remove { Events.RemoveHandler (PageIndexChangedEvent, value); }
  102. }
  103. public event GridViewPageEventHandler PageIndexChanging {
  104. add { Events.AddHandler (PageIndexChangingEvent, value); }
  105. remove { Events.RemoveHandler (PageIndexChangingEvent, value); }
  106. }
  107. public event GridViewCancelEditEventHandler RowCancelingEdit {
  108. add { Events.AddHandler (RowCancelingEditEvent, value); }
  109. remove { Events.RemoveHandler (RowCancelingEditEvent, value); }
  110. }
  111. public event GridViewCommandEventHandler RowCommand {
  112. add { Events.AddHandler (RowCommandEvent, value); }
  113. remove { Events.RemoveHandler (RowCommandEvent, value); }
  114. }
  115. public event GridViewRowEventHandler RowCreated {
  116. add { Events.AddHandler (RowCreatedEvent, value); }
  117. remove { Events.RemoveHandler (RowCreatedEvent, value); }
  118. }
  119. public event GridViewRowEventHandler RowDataBound {
  120. add { Events.AddHandler (RowDataBoundEvent, value); }
  121. remove { Events.RemoveHandler (RowDataBoundEvent, value); }
  122. }
  123. public event GridViewDeletedEventHandler RowDeleted {
  124. add { Events.AddHandler (RowDeletedEvent, value); }
  125. remove { Events.RemoveHandler (RowDeletedEvent, value); }
  126. }
  127. public event GridViewDeleteEventHandler RowDeleting {
  128. add { Events.AddHandler (RowDeletingEvent, value); }
  129. remove { Events.RemoveHandler (RowDeletingEvent, value); }
  130. }
  131. public event GridViewEditEventHandler RowEditing {
  132. add { Events.AddHandler (RowEditingEvent, value); }
  133. remove { Events.RemoveHandler (RowEditingEvent, value); }
  134. }
  135. public event GridViewUpdatedEventHandler RowUpdated {
  136. add { Events.AddHandler (RowUpdatedEvent, value); }
  137. remove { Events.RemoveHandler (RowUpdatedEvent, value); }
  138. }
  139. public event GridViewUpdateEventHandler RowUpdating {
  140. add { Events.AddHandler (RowUpdatingEvent, value); }
  141. remove { Events.RemoveHandler (RowUpdatingEvent, value); }
  142. }
  143. public event EventHandler SelectedIndexChanged {
  144. add { Events.AddHandler (SelectedIndexChangedEvent, value); }
  145. remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
  146. }
  147. public event GridViewSelectEventHandler SelectedIndexChanging {
  148. add { Events.AddHandler (SelectedIndexChangingEvent, value); }
  149. remove { Events.RemoveHandler (SelectedIndexChangingEvent, value); }
  150. }
  151. public event EventHandler Sorted {
  152. add { Events.AddHandler (SortedEvent, value); }
  153. remove { Events.RemoveHandler (SortedEvent, value); }
  154. }
  155. public event GridViewSortEventHandler Sorting {
  156. add { Events.AddHandler (SortingEvent, value); }
  157. remove { Events.RemoveHandler (SortingEvent, value); }
  158. }
  159. protected virtual void OnPageIndexChanged (EventArgs e)
  160. {
  161. if (Events != null) {
  162. EventHandler eh = (EventHandler) Events [PageIndexChangedEvent];
  163. if (eh != null) eh (this, e);
  164. }
  165. }
  166. protected virtual void OnPageIndexChanging (GridViewPageEventArgs e)
  167. {
  168. if (Events != null) {
  169. GridViewPageEventHandler eh = (GridViewPageEventHandler) Events [PageIndexChangingEvent];
  170. if (eh != null) eh (this, e);
  171. }
  172. }
  173. protected virtual void OnRowCancelingEdit (GridViewCancelEditEventArgs e)
  174. {
  175. if (Events != null) {
  176. GridViewCancelEditEventHandler eh = (GridViewCancelEditEventHandler) Events [RowCancelingEditEvent];
  177. if (eh != null) eh (this, e);
  178. }
  179. }
  180. protected virtual void OnRowCommand (GridViewCommandEventArgs e)
  181. {
  182. if (Events != null) {
  183. GridViewCommandEventHandler eh = (GridViewCommandEventHandler) Events [RowCommandEvent];
  184. if (eh != null) eh (this, e);
  185. }
  186. }
  187. protected virtual void OnRowCreated (GridViewRowEventArgs e)
  188. {
  189. if (Events != null) {
  190. GridViewRowEventHandler eh = (GridViewRowEventHandler) Events [RowCreatedEvent];
  191. if (eh != null) eh (this, e);
  192. }
  193. }
  194. protected virtual void OnRowDataBound (GridViewRowEventArgs e)
  195. {
  196. if (Events != null) {
  197. GridViewRowEventHandler eh = (GridViewRowEventHandler) Events [RowDataBoundEvent];
  198. if (eh != null) eh (this, e);
  199. }
  200. }
  201. protected virtual void OnRowDeleted (GridViewDeletedEventArgs e)
  202. {
  203. if (Events != null) {
  204. GridViewDeletedEventHandler eh = (GridViewDeletedEventHandler) Events [RowDeletedEvent];
  205. if (eh != null) eh (this, e);
  206. }
  207. }
  208. protected virtual void OnRowDeleting (GridViewDeleteEventArgs e)
  209. {
  210. if (Events != null) {
  211. GridViewDeleteEventHandler eh = (GridViewDeleteEventHandler) Events [RowDeletingEvent];
  212. if (eh != null) eh (this, e);
  213. }
  214. }
  215. protected virtual void OnRowEditing (GridViewEditEventArgs e)
  216. {
  217. if (Events != null) {
  218. GridViewEditEventHandler eh = (GridViewEditEventHandler) Events [RowEditingEvent];
  219. if (eh != null) eh (this, e);
  220. }
  221. }
  222. protected virtual void OnRowUpdated (GridViewUpdatedEventArgs e)
  223. {
  224. if (Events != null) {
  225. GridViewUpdatedEventHandler eh = (GridViewUpdatedEventHandler) Events [RowUpdatedEvent];
  226. if (eh != null) eh (this, e);
  227. }
  228. }
  229. protected virtual void OnRowUpdating (GridViewUpdateEventArgs e)
  230. {
  231. if (Events != null) {
  232. GridViewUpdateEventHandler eh = (GridViewUpdateEventHandler) Events [RowUpdatingEvent];
  233. if (eh != null) eh (this, e);
  234. }
  235. }
  236. protected virtual void OnSelectedIndexChanged (EventArgs e)
  237. {
  238. if (Events != null) {
  239. EventHandler eh = (EventHandler) Events [SelectedIndexChangedEvent];
  240. if (eh != null) eh (this, e);
  241. }
  242. }
  243. protected virtual void OnSelectedIndexChanging (GridViewSelectEventArgs e)
  244. {
  245. if (Events != null) {
  246. GridViewSelectEventHandler eh = (GridViewSelectEventHandler) Events [SelectedIndexChangingEvent];
  247. if (eh != null) eh (this, e);
  248. }
  249. }
  250. protected virtual void OnSorted (EventArgs e)
  251. {
  252. if (Events != null) {
  253. EventHandler eh = (EventHandler) Events [SortedEvent];
  254. if (eh != null) eh (this, e);
  255. }
  256. }
  257. protected virtual void OnSorting (GridViewSortEventArgs e)
  258. {
  259. if (Events != null) {
  260. GridViewSortEventHandler eh = (GridViewSortEventHandler) Events [SortingEvent];
  261. if (eh != null) eh (this, e);
  262. }
  263. }
  264. [WebCategoryAttribute ("Paging")]
  265. [DefaultValueAttribute (false)]
  266. public virtual bool AllowPaging {
  267. get {
  268. object ob = ViewState ["AllowPaging"];
  269. if (ob != null) return (bool) ob;
  270. return false;
  271. }
  272. set {
  273. ViewState ["AllowPaging"] = value;
  274. RequireBinding ();
  275. }
  276. }
  277. [WebCategoryAttribute ("Behavior")]
  278. [DefaultValueAttribute (false)]
  279. public virtual bool AllowSorting {
  280. get {
  281. object ob = ViewState ["AllowSorting"];
  282. if (ob != null) return (bool) ob;
  283. return false;
  284. }
  285. set {
  286. ViewState ["AllowSorting"] = value;
  287. RequireBinding ();
  288. }
  289. }
  290. [WebCategoryAttribute ("Styles")]
  291. [PersistenceMode (PersistenceMode.InnerProperty)]
  292. [NotifyParentProperty (true)]
  293. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  294. public TableItemStyle AlternatingRowStyle {
  295. get {
  296. if (alternatingRowStyle == null) {
  297. alternatingRowStyle = new TableItemStyle ();
  298. if (IsTrackingViewState)
  299. alternatingRowStyle.TrackViewState();
  300. }
  301. return alternatingRowStyle;
  302. }
  303. }
  304. [WebCategoryAttribute ("Behavior")]
  305. [DefaultValueAttribute (false)]
  306. public virtual bool AutoGenerateEditButton {
  307. get {
  308. object ob = ViewState ["AutoGenerateEditButton"];
  309. if (ob != null) return (bool) ob;
  310. return false;
  311. }
  312. set {
  313. ViewState ["AutoGenerateEditButton"] = value;
  314. RequireBinding ();
  315. }
  316. }
  317. [WebCategoryAttribute ("Behavior")]
  318. [DefaultValueAttribute (false)]
  319. public virtual bool AutoGenerateDeleteButton {
  320. get {
  321. object ob = ViewState ["AutoGenerateDeleteButton"];
  322. if (ob != null) return (bool) ob;
  323. return false;
  324. }
  325. set {
  326. ViewState ["AutoGenerateDeleteButton"] = value;
  327. RequireBinding ();
  328. }
  329. }
  330. [WebCategoryAttribute ("Behavior")]
  331. [DefaultValueAttribute (false)]
  332. public virtual bool AutoGenerateSelectButton {
  333. get {
  334. object ob = ViewState ["AutoGenerateSelectButton"];
  335. if (ob != null) return (bool) ob;
  336. return false;
  337. }
  338. set {
  339. ViewState ["AutoGenerateSelectButton"] = value;
  340. RequireBinding ();
  341. }
  342. }
  343. [WebCategoryAttribute ("Behavior")]
  344. [DefaultValueAttribute (true)]
  345. public virtual bool AutoGenerateColumns {
  346. get {
  347. object ob = ViewState ["AutoGenerateColumns"];
  348. if (ob != null) return (bool) ob;
  349. return true;
  350. }
  351. set {
  352. ViewState ["AutoGenerateColumns"] = value;
  353. RequireBinding ();
  354. }
  355. }
  356. [UrlPropertyAttribute]
  357. [WebCategoryAttribute ("Appearance")]
  358. [DefaultValueAttribute ("")]
  359. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  360. public virtual string BackImageUrl {
  361. get {
  362. if (ControlStyleCreated)
  363. return ((TableStyle) ControlStyle).BackImageUrl;
  364. return String.Empty;
  365. }
  366. set {
  367. ((TableStyle) ControlStyle).BackImageUrl = value;
  368. }
  369. }
  370. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  371. [BrowsableAttribute (false)]
  372. public virtual GridViewRow BottomPagerRow {
  373. get {
  374. EnsureDataBound ();
  375. return bottomPagerRow;
  376. }
  377. }
  378. [WebCategoryAttribute ("Accessibility")]
  379. [DefaultValueAttribute ("")]
  380. [LocalizableAttribute (true)]
  381. public virtual string Caption {
  382. get {
  383. object ob = ViewState ["Caption"];
  384. if (ob != null) return (string) ob;
  385. return string.Empty;
  386. }
  387. set {
  388. ViewState ["Caption"] = value;
  389. RequireBinding ();
  390. }
  391. }
  392. [WebCategoryAttribute ("Accessibility")]
  393. [DefaultValueAttribute (TableCaptionAlign.NotSet)]
  394. public virtual TableCaptionAlign CaptionAlign
  395. {
  396. get {
  397. object o = ViewState ["CaptionAlign"];
  398. if(o != null) return (TableCaptionAlign) o;
  399. return TableCaptionAlign.NotSet;
  400. }
  401. set {
  402. ViewState ["CaptionAlign"] = value;
  403. RequireBinding ();
  404. }
  405. }
  406. [WebCategoryAttribute ("Layout")]
  407. [DefaultValueAttribute (-1)]
  408. public virtual int CellPadding
  409. {
  410. get {
  411. if (ControlStyleCreated)
  412. return ((TableStyle) ControlStyle).CellPadding;
  413. return -1;
  414. }
  415. set {
  416. ((TableStyle) ControlStyle).CellPadding = value;
  417. }
  418. }
  419. [WebCategoryAttribute ("Layout")]
  420. [DefaultValueAttribute (0)]
  421. public virtual int CellSpacing
  422. {
  423. get {
  424. if (ControlStyleCreated)
  425. return ((TableStyle) ControlStyle).CellSpacing;
  426. return 0;
  427. }
  428. set {
  429. ((TableStyle) ControlStyle).CellSpacing = value;
  430. }
  431. }
  432. [EditorAttribute ("System.Web.UI.Design.WebControls.DataControlFieldTypeEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  433. [MergablePropertyAttribute (false)]
  434. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  435. [DefaultValueAttribute (null)]
  436. [WebCategoryAttribute ("Misc")]
  437. public virtual DataControlFieldCollection Columns {
  438. get {
  439. if (columns == null) {
  440. columns = new DataControlFieldCollection ();
  441. columns.FieldsChanged += new EventHandler (OnFieldsChanged);
  442. if (IsTrackingViewState)
  443. ((IStateManager)columns).TrackViewState ();
  444. }
  445. return columns;
  446. }
  447. }
  448. [DefaultValueAttribute (null)]
  449. [WebCategoryAttribute ("Data")]
  450. [TypeConverter (typeof(StringArrayConverter))]
  451. [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  452. public virtual string[] DataKeyNames
  453. {
  454. get {
  455. if (dataKeyNames != null)
  456. return dataKeyNames;
  457. return emptyKeys;
  458. }
  459. set {
  460. dataKeyNames = value;
  461. RequireBinding ();
  462. }
  463. }
  464. ArrayList DataKeyArrayList {
  465. get {
  466. if (_dataKeyArrayList == null) {
  467. _dataKeyArrayList = new ArrayList ();
  468. }
  469. return _dataKeyArrayList;
  470. }
  471. }
  472. [BrowsableAttribute (false)]
  473. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  474. public virtual DataKeyArray DataKeys {
  475. get {
  476. if (keys == null) {
  477. keys = new DataKeyArray (DataKeyArrayList);
  478. if (IsTrackingViewState)
  479. ((IStateManager) keys).TrackViewState ();
  480. }
  481. return keys;
  482. }
  483. }
  484. DataKey OldEditValues {
  485. get {
  486. if (oldEditValues == null) {
  487. oldEditValues = new DataKey (new OrderedDictionary ());
  488. }
  489. return oldEditValues;
  490. }
  491. }
  492. [WebCategoryAttribute ("Misc")]
  493. [DefaultValueAttribute (-1)]
  494. public virtual int EditIndex {
  495. get {
  496. return editIndex;
  497. }
  498. set {
  499. if (value == editIndex)
  500. return;
  501. editIndex = value;
  502. RequireBinding ();
  503. }
  504. }
  505. [WebCategoryAttribute ("Styles")]
  506. [PersistenceMode (PersistenceMode.InnerProperty)]
  507. [NotifyParentProperty (true)]
  508. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  509. public TableItemStyle EditRowStyle {
  510. get {
  511. if (editRowStyle == null) {
  512. editRowStyle = new TableItemStyle ();
  513. if (IsTrackingViewState)
  514. editRowStyle.TrackViewState();
  515. }
  516. return editRowStyle;
  517. }
  518. }
  519. [WebCategoryAttribute ("Styles")]
  520. [PersistenceMode (PersistenceMode.InnerProperty)]
  521. [NotifyParentProperty (true)]
  522. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  523. public TableItemStyle EmptyDataRowStyle {
  524. get {
  525. if (emptyDataRowStyle == null) {
  526. emptyDataRowStyle = new TableItemStyle ();
  527. if (IsTrackingViewState)
  528. emptyDataRowStyle.TrackViewState();
  529. }
  530. return emptyDataRowStyle;
  531. }
  532. }
  533. [DefaultValue (null)]
  534. [TemplateContainer (typeof(GridViewRow), BindingDirection.OneWay)]
  535. [PersistenceMode (PersistenceMode.InnerProperty)]
  536. [Browsable (false)]
  537. public virtual ITemplate EmptyDataTemplate {
  538. get { return emptyDataTemplate; }
  539. set { emptyDataTemplate = value; RequireBinding (); }
  540. }
  541. [LocalizableAttribute (true)]
  542. [WebCategoryAttribute ("Appearance")]
  543. [DefaultValueAttribute ("")]
  544. public virtual string EmptyDataText {
  545. get {
  546. object ob = ViewState ["EmptyDataText"];
  547. if (ob != null) return (string) ob;
  548. return string.Empty;
  549. }
  550. set {
  551. ViewState ["EmptyDataText"] = value;
  552. RequireBinding ();
  553. }
  554. }
  555. [WebCategoryAttribute ("Behavior")]
  556. [DefaultValueAttribute (false)]
  557. public virtual bool EnableSortingAndPagingCallbacks {
  558. get {
  559. object ob = ViewState ["EnableSortingAndPagingCallbacks"];
  560. if (ob != null) return (bool) ob;
  561. return false;
  562. }
  563. set {
  564. ViewState ["EnableSortingAndPagingCallbacks"] = value;
  565. RequireBinding ();
  566. }
  567. }
  568. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  569. [BrowsableAttribute (false)]
  570. public virtual GridViewRow FooterRow {
  571. get {
  572. if (table != null) {
  573. for (int index = table.Rows.Count - 1; index >= 0; index--) {
  574. GridViewRow row = (GridViewRow) table.Rows [index];
  575. switch (row.RowType) {
  576. case DataControlRowType.Separator:
  577. case DataControlRowType.Pager:
  578. continue;
  579. case DataControlRowType.Footer:
  580. return row;
  581. default:
  582. break;
  583. }
  584. }
  585. }
  586. return null;
  587. }
  588. }
  589. [WebCategoryAttribute ("Styles")]
  590. [PersistenceMode (PersistenceMode.InnerProperty)]
  591. [NotifyParentProperty (true)]
  592. [DefaultValue (null)]
  593. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  594. public TableItemStyle FooterStyle {
  595. get {
  596. if (footerStyle == null) {
  597. footerStyle = new TableItemStyle ();
  598. if (IsTrackingViewState)
  599. footerStyle.TrackViewState();
  600. }
  601. return footerStyle;
  602. }
  603. }
  604. [WebCategoryAttribute ("Appearance")]
  605. [DefaultValueAttribute (GridLines.Both)]
  606. public virtual GridLines GridLines {
  607. get {
  608. if (ControlStyleCreated)
  609. return ((TableStyle) ControlStyle).GridLines;
  610. return GridLines.Both;
  611. }
  612. set {
  613. ((TableStyle) ControlStyle).GridLines = value;
  614. }
  615. }
  616. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  617. [BrowsableAttribute (false)]
  618. public virtual GridViewRow HeaderRow {
  619. get {
  620. if (table != null) {
  621. for (int index = 0, total = table.Rows.Count; index < total; index++) {
  622. GridViewRow row = (GridViewRow) table.Rows [index];
  623. switch (row.RowType) {
  624. case DataControlRowType.Separator:
  625. case DataControlRowType.Pager:
  626. continue;
  627. case DataControlRowType.Header:
  628. return row;
  629. default:
  630. break;
  631. }
  632. }
  633. }
  634. return null;
  635. }
  636. }
  637. [WebCategoryAttribute ("Styles")]
  638. [PersistenceMode (PersistenceMode.InnerProperty)]
  639. [NotifyParentProperty (true)]
  640. [DefaultValue (null)]
  641. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  642. public TableItemStyle HeaderStyle {
  643. get {
  644. if (headerStyle == null) {
  645. headerStyle = new TableItemStyle ();
  646. if (IsTrackingViewState)
  647. headerStyle.TrackViewState();
  648. }
  649. return headerStyle;
  650. }
  651. }
  652. [Category ("Layout")]
  653. [DefaultValueAttribute (HorizontalAlign.NotSet)]
  654. public virtual HorizontalAlign HorizontalAlign {
  655. get {
  656. if (ControlStyleCreated)
  657. return ((TableStyle) ControlStyle).HorizontalAlign;
  658. return HorizontalAlign.NotSet;
  659. }
  660. set {
  661. ((TableStyle) ControlStyle).HorizontalAlign = value;
  662. }
  663. }
  664. [BrowsableAttribute (false)]
  665. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  666. public virtual int PageCount {
  667. get {
  668. return ViewState.GetInt ("PageCount", 0);
  669. }
  670. private set {
  671. ViewState ["PageCount"] = value;
  672. }
  673. }
  674. [WebCategoryAttribute ("Paging")]
  675. [BrowsableAttribute (true)]
  676. [DefaultValueAttribute (0)]
  677. public virtual int PageIndex {
  678. get {
  679. return pageIndex;
  680. }
  681. set {
  682. if (value == pageIndex)
  683. return;
  684. pageIndex = value;
  685. RequireBinding ();
  686. }
  687. }
  688. [DefaultValueAttribute (10)]
  689. [WebCategoryAttribute ("Paging")]
  690. public virtual int PageSize {
  691. get {
  692. object ob = ViewState ["PageSize"];
  693. if (ob != null) return (int) ob;
  694. return 10;
  695. }
  696. set {
  697. ViewState ["PageSize"] = value;
  698. RequireBinding ();
  699. }
  700. }
  701. [WebCategoryAttribute ("Paging")]
  702. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  703. [NotifyParentPropertyAttribute (true)]
  704. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  705. public virtual PagerSettings PagerSettings {
  706. get {
  707. if (pagerSettings == null) {
  708. pagerSettings = new PagerSettings (this);
  709. if (IsTrackingViewState)
  710. ((IStateManager)pagerSettings).TrackViewState ();
  711. }
  712. return pagerSettings;
  713. }
  714. }
  715. [WebCategoryAttribute ("Styles")]
  716. [PersistenceMode (PersistenceMode.InnerProperty)]
  717. [NotifyParentProperty (true)]
  718. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  719. public TableItemStyle PagerStyle {
  720. get {
  721. if (pagerStyle == null) {
  722. pagerStyle = new TableItemStyle ();
  723. if (IsTrackingViewState)
  724. pagerStyle.TrackViewState();
  725. }
  726. return pagerStyle;
  727. }
  728. }
  729. [DefaultValue (null)]
  730. /* DataControlPagerCell isnt specified in the docs */
  731. //[TemplateContainer (typeof(DataControlPagerCell), BindingDirection.OneWay)]
  732. [PersistenceMode (PersistenceMode.InnerProperty)]
  733. [Browsable (false)]
  734. public virtual ITemplate PagerTemplate {
  735. get { return pagerTemplate; }
  736. set { pagerTemplate = value; RequireBinding (); }
  737. }
  738. [DefaultValueAttribute ("")]
  739. [WebCategoryAttribute ("Accessibility")]
  740. // [TypeConverterAttribute (typeof(System.Web.UI.Design.DataColumnSelectionConverter)]
  741. public virtual string RowHeaderColumn {
  742. get {
  743. object ob = ViewState ["RowHeaderColumn"];
  744. if (ob != null) return (string) ob;
  745. return string.Empty;
  746. }
  747. set {
  748. ViewState ["RowHeaderColumn"] = value;
  749. RequireBinding ();
  750. }
  751. }
  752. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  753. [BrowsableAttribute (false)]
  754. public virtual GridViewRowCollection Rows {
  755. get {
  756. EnsureChildControls ();
  757. if (rows == null)
  758. rows = new GridViewRowCollection (new ArrayList ());
  759. return rows;
  760. }
  761. }
  762. [WebCategoryAttribute ("Styles")]
  763. [PersistenceMode (PersistenceMode.InnerProperty)]
  764. [NotifyParentProperty (true)]
  765. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  766. public TableItemStyle RowStyle {
  767. get {
  768. if (rowStyle == null) {
  769. rowStyle = new TableItemStyle ();
  770. if (IsTrackingViewState)
  771. rowStyle.TrackViewState();
  772. }
  773. return rowStyle;
  774. }
  775. }
  776. [BrowsableAttribute (false)]
  777. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  778. public virtual DataKey SelectedDataKey {
  779. get {
  780. if (keys == null)
  781. throw new InvalidOperationException ("DataKeys");
  782. if (selectedIndex >= 0 && selectedIndex < DataKeys.Count) {
  783. return DataKeys [selectedIndex];
  784. } else
  785. return null;
  786. }
  787. }
  788. [BindableAttribute (true)]
  789. [DefaultValueAttribute (-1)]
  790. public virtual int SelectedIndex {
  791. get {
  792. return selectedIndex;
  793. }
  794. set {
  795. if (Rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
  796. int oldIndex = selectedIndex;
  797. selectedIndex = -1;
  798. Rows [oldIndex].RowState = GetRowState (oldIndex);
  799. }
  800. selectedIndex = value;
  801. if (Rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
  802. Rows [selectedIndex].RowState = GetRowState (selectedIndex);
  803. }
  804. }
  805. }
  806. [BrowsableAttribute (false)]
  807. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  808. public virtual GridViewRow SelectedRow {
  809. get {
  810. if (selectedIndex >= 0 && selectedIndex < Rows.Count) {
  811. return Rows [selectedIndex];
  812. } else
  813. return null;
  814. }
  815. }
  816. [WebCategoryAttribute ("Styles")]
  817. [PersistenceMode (PersistenceMode.InnerProperty)]
  818. [NotifyParentProperty (true)]
  819. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  820. public TableItemStyle SelectedRowStyle {
  821. get {
  822. if (selectedRowStyle == null) {
  823. selectedRowStyle = new TableItemStyle ();
  824. if (IsTrackingViewState)
  825. selectedRowStyle.TrackViewState();
  826. }
  827. return selectedRowStyle;
  828. }
  829. }
  830. [BrowsableAttribute (false)]
  831. public object SelectedValue {
  832. get {
  833. if (SelectedDataKey != null)
  834. return SelectedDataKey.Value;
  835. else
  836. return null;
  837. }
  838. }
  839. [WebCategoryAttribute ("Appearance")]
  840. [DefaultValueAttribute (false)]
  841. public virtual bool ShowFooter {
  842. get {
  843. object ob = ViewState ["ShowFooter"];
  844. if (ob != null) return (bool) ob;
  845. return false;
  846. }
  847. set {
  848. ViewState ["ShowFooter"] = value;
  849. RequireBinding ();
  850. }
  851. }
  852. [WebCategoryAttribute ("Appearance")]
  853. [DefaultValueAttribute (true)]
  854. public virtual bool ShowHeader {
  855. get {
  856. object ob = ViewState ["ShowHeader"];
  857. if (ob != null) return (bool) ob;
  858. return true;
  859. }
  860. set {
  861. ViewState ["ShowHeader"] = value;
  862. RequireBinding ();
  863. }
  864. }
  865. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  866. [BrowsableAttribute (false)]
  867. [DefaultValueAttribute (SortDirection.Ascending)]
  868. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  869. public virtual SortDirection SortDirection {
  870. get { return sortDirection; }
  871. private set {
  872. if (sortDirection == value)
  873. return;
  874. sortDirection = value;
  875. RequireBinding ();
  876. }
  877. }
  878. [BrowsableAttribute (false)]
  879. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  880. public virtual string SortExpression {
  881. get {
  882. if (sortExpression == null)
  883. return String.Empty;
  884. return sortExpression;
  885. }
  886. private set {
  887. if (sortExpression == value)
  888. return;
  889. sortExpression = value;
  890. RequireBinding ();
  891. }
  892. }
  893. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  894. [BrowsableAttribute (false)]
  895. public virtual GridViewRow TopPagerRow {
  896. get {
  897. EnsureDataBound ();
  898. return topPagerRow;
  899. }
  900. }
  901. [WebCategoryAttribute ("Accessibility")]
  902. [DefaultValueAttribute (true)]
  903. public virtual bool UseAccessibleHeader {
  904. get {
  905. object ob = ViewState ["UseAccessibleHeader"];
  906. if (ob != null) return (bool) ob;
  907. return true;
  908. }
  909. set {
  910. ViewState ["UseAccessibleHeader"] = value;
  911. RequireBinding ();
  912. }
  913. }
  914. public virtual bool IsBindableType (Type type)
  915. {
  916. return type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Guid);
  917. }
  918. // MSDN: The CreateDataSourceSelectArguments method is a helper method called by
  919. // the GridView control to create the DataSourceSelectArguments object that
  920. // contains the arguments passed to the data source. In this implementation,
  921. // the DataSourceSelectArguments object contains the arguments for paging operations.
  922. protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
  923. {
  924. DataSourceSelectArguments arg = DataSourceSelectArguments.Empty;
  925. DataSourceView view= GetData();
  926. if (AllowPaging && view.CanPage) {
  927. arg.StartRowIndex = PageIndex * PageSize;
  928. if (view.CanRetrieveTotalRowCount) {
  929. arg.RetrieveTotalRowCount = true;
  930. arg.MaximumRows = PageSize;
  931. }
  932. else {
  933. arg.MaximumRows = -1;
  934. }
  935. }
  936. if (IsBoundUsingDataSourceID && !String.IsNullOrEmpty (sortExpression)) {
  937. if (sortDirection == SortDirection.Ascending)
  938. arg.SortExpression = sortExpression;
  939. else
  940. arg.SortExpression = sortExpression + " DESC";
  941. }
  942. return arg;
  943. }
  944. protected virtual ICollection CreateColumns (PagedDataSource dataSource, bool useDataSource)
  945. {
  946. ArrayList fields = new ArrayList ();
  947. if (AutoGenerateEditButton || AutoGenerateDeleteButton || AutoGenerateSelectButton) {
  948. CommandField field = new CommandField ();
  949. field.ShowEditButton = AutoGenerateEditButton;
  950. field.ShowDeleteButton = AutoGenerateDeleteButton;
  951. field.ShowSelectButton = AutoGenerateSelectButton;
  952. fields.Add (field);
  953. }
  954. fields.AddRange (Columns);
  955. if (AutoGenerateColumns) {
  956. if (useDataSource)
  957. autoFieldProperties = CreateAutoFieldProperties (dataSource);
  958. if (autoFieldProperties != null) {
  959. foreach (AutoGeneratedFieldProperties props in autoFieldProperties)
  960. fields.Add (CreateAutoGeneratedColumn (props));
  961. }
  962. }
  963. return fields;
  964. }
  965. protected virtual AutoGeneratedField CreateAutoGeneratedColumn (AutoGeneratedFieldProperties fieldProperties)
  966. {
  967. return new AutoGeneratedField (fieldProperties);
  968. }
  969. AutoGeneratedFieldProperties[] CreateAutoFieldProperties (PagedDataSource source)
  970. {
  971. if(source == null) return null;
  972. PropertyDescriptorCollection props = source.GetItemProperties (new PropertyDescriptor[0]);
  973. Type prop_type;
  974. ArrayList retVal = new ArrayList();
  975. if (props == null)
  976. {
  977. object fitem = null;
  978. prop_type = null;
  979. PropertyInfo prop_item = source.DataSource.GetType().GetProperty("Item",
  980. BindingFlags.Instance | BindingFlags.Static |
  981. BindingFlags.Public, null, null,
  982. new Type[] { typeof(int) }, null);
  983. if (prop_item != null) {
  984. prop_type = prop_item.PropertyType;
  985. }
  986. if (prop_type == null || prop_type == typeof(object)) {
  987. IEnumerator en = source.GetEnumerator();
  988. if (en.MoveNext())
  989. fitem = en.Current;
  990. if (fitem != null)
  991. prop_type = fitem.GetType();
  992. }
  993. if (fitem != null && fitem is ICustomTypeDescriptor) {
  994. props = TypeDescriptor.GetProperties(fitem);
  995. } else if (prop_type != null) {
  996. if (IsBindableType (prop_type)) {
  997. AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
  998. ((IStateManager)field).TrackViewState();
  999. field.Name = "Item";
  1000. field.DataField = BoundField.ThisExpression;
  1001. field.Type = prop_type;
  1002. retVal.Add (field);
  1003. } else {
  1004. props = TypeDescriptor.GetProperties (prop_type);
  1005. }
  1006. }
  1007. }
  1008. if (props != null && props.Count > 0)
  1009. {
  1010. foreach (PropertyDescriptor current in props) {
  1011. if (IsBindableType (current.PropertyType)) {
  1012. AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
  1013. ((IStateManager)field).TrackViewState();
  1014. field.Name = current.Name;
  1015. field.DataField = current.Name;
  1016. field.IsReadOnly = current.IsReadOnly;
  1017. field.Type = current.PropertyType;
  1018. retVal.Add (field);
  1019. }
  1020. }
  1021. }
  1022. if (retVal.Count > 0)
  1023. return (AutoGeneratedFieldProperties[]) retVal.ToArray (typeof(AutoGeneratedFieldProperties));
  1024. else
  1025. return new AutoGeneratedFieldProperties [0];
  1026. }
  1027. protected virtual GridViewRow CreateRow (int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState)
  1028. {
  1029. GridViewRow row = new GridViewRow (rowIndex, dataSourceIndex, rowType, rowState);
  1030. return row;
  1031. }
  1032. void RequireBinding ()
  1033. {
  1034. if (Initialized) {
  1035. RequiresDataBinding = true;
  1036. }
  1037. }
  1038. protected virtual Table CreateChildTable ()
  1039. {
  1040. return new ContainedTable (this);
  1041. }
  1042. protected override int CreateChildControls (IEnumerable data, bool dataBinding)
  1043. {
  1044. // clear GridView
  1045. Controls.Clear ();
  1046. table = null;
  1047. rows = null;
  1048. if (data == null) {
  1049. return 0;
  1050. }
  1051. PagedDataSource dataSource;
  1052. if (dataBinding) {
  1053. DataSourceView view = GetData ();
  1054. dataSource = new PagedDataSource ();
  1055. dataSource.DataSource = data;
  1056. if (AllowPaging) {
  1057. dataSource.AllowPaging = true;
  1058. dataSource.PageSize = PageSize;
  1059. dataSource.CurrentPageIndex = PageIndex;
  1060. if (view.CanPage) {
  1061. dataSource.AllowServerPaging = true;
  1062. if (SelectArguments.RetrieveTotalRowCount)
  1063. dataSource.VirtualCount = SelectArguments.TotalRowCount;
  1064. }
  1065. }
  1066. PageCount = dataSource.PageCount;
  1067. }
  1068. else
  1069. {
  1070. dataSource = new PagedDataSource ();
  1071. dataSource.DataSource = data;
  1072. if (AllowPaging) {
  1073. dataSource.AllowPaging = true;
  1074. dataSource.PageSize = PageSize;
  1075. dataSource.CurrentPageIndex = PageIndex;
  1076. }
  1077. }
  1078. if (dataSource.DataSourceCount == 0)
  1079. return 0;
  1080. bool showPager = AllowPaging && (PageCount > 1);
  1081. table = CreateChildTable ();
  1082. Controls.Add (table);
  1083. ArrayList list = new ArrayList ();
  1084. // Creates the set of fields to show
  1085. ICollection fieldCollection = CreateColumns (dataSource, dataBinding);
  1086. DataControlField[] fields = new DataControlField [fieldCollection.Count];
  1087. fieldCollection.CopyTo (fields, 0);
  1088. foreach (DataControlField field in fields) {
  1089. field.Initialize (AllowSorting, this);
  1090. if (EnableSortingAndPagingCallbacks)
  1091. field.ValidateSupportsCallback ();
  1092. }
  1093. // Main table creation
  1094. if (showPager && PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom) {
  1095. topPagerRow = CreatePagerRow (fields.Length, dataSource);
  1096. table.Rows.Add (topPagerRow);
  1097. OnRowCreated (new GridViewRowEventArgs (topPagerRow));
  1098. }
  1099. GridViewRow headerRow = CreateRow (0, 0, DataControlRowType.Header, DataControlRowState.Normal);
  1100. table.Rows.Add (headerRow);
  1101. InitializeRow (headerRow, fields);
  1102. OnRowCreated (new GridViewRowEventArgs (headerRow));
  1103. if (dataBinding) {
  1104. headerRow.DataBind ();
  1105. OnRowDataBound (new GridViewRowEventArgs (headerRow));
  1106. }
  1107. foreach (object obj in dataSource) {
  1108. DataControlRowState rstate = GetRowState (list.Count);
  1109. GridViewRow row = CreateRow (list.Count, list.Count, DataControlRowType.DataRow, rstate);
  1110. row.DataItem = obj;
  1111. list.Add (row);
  1112. table.Rows.Add (row);
  1113. InitializeRow (row, fields);
  1114. OnRowCreated (new GridViewRowEventArgs (row));
  1115. if (dataBinding) {
  1116. row.DataBind ();
  1117. OnRowDataBound (new GridViewRowEventArgs (row));
  1118. if (EditIndex == row.RowIndex)
  1119. oldEditValues = new DataKey (GetRowValues (row, false, true));
  1120. DataKeyArrayList.Add (new DataKey (CreateRowDataKey (row), DataKeyNames));
  1121. }
  1122. if (list.Count >= PageSize)
  1123. break;
  1124. }
  1125. if (list.Count == 0) {
  1126. GridViewRow emptyRow = CreateEmptyrRow (fields.Length);
  1127. table.Rows.Add (emptyRow);
  1128. OnRowCreated (new GridViewRowEventArgs (emptyRow));
  1129. if (dataBinding) {
  1130. emptyRow.DataBind ();
  1131. OnRowDataBound (new GridViewRowEventArgs (emptyRow));
  1132. }
  1133. }
  1134. GridViewRow footerRow = CreateRow (0, 0, DataControlRowType.Footer, DataControlRowState.Normal);
  1135. table.Rows.Add (footerRow);
  1136. InitializeRow (footerRow, fields);
  1137. OnRowCreated (new GridViewRowEventArgs (footerRow));
  1138. if (dataBinding) {
  1139. footerRow.DataBind ();
  1140. OnRowDataBound (new GridViewRowEventArgs (footerRow));
  1141. }
  1142. if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) {
  1143. bottomPagerRow = CreatePagerRow (fields.Length, dataSource);
  1144. table.Rows.Add (bottomPagerRow);
  1145. OnRowCreated (new GridViewRowEventArgs (bottomPagerRow));
  1146. }
  1147. rows = new GridViewRowCollection (list);
  1148. return dataSource.DataSourceCount;
  1149. }
  1150. protected override Style CreateControlStyle ()
  1151. {
  1152. TableStyle style = new TableStyle (ViewState);
  1153. style.GridLines = GridLines.Both;
  1154. style.CellSpacing = 0;
  1155. return style;
  1156. }
  1157. DataControlRowState GetRowState (int index)
  1158. {
  1159. DataControlRowState rstate = (index % 2) == 0 ? DataControlRowState.Normal : DataControlRowState.Alternate;
  1160. if (index == SelectedIndex) rstate |= DataControlRowState.Selected;
  1161. if (index == EditIndex) rstate |= DataControlRowState.Edit;
  1162. return rstate;
  1163. }
  1164. GridViewRow CreatePagerRow (int fieldCount, PagedDataSource dataSource)
  1165. {
  1166. GridViewRow row = CreateRow (-1, -1, DataControlRowType.Pager, DataControlRowState.Normal);
  1167. InitializePager (row, fieldCount, dataSource);
  1168. return row;
  1169. }
  1170. protected virtual void InitializePager (GridViewRow row, int columnSpan, PagedDataSource dataSource)
  1171. {
  1172. TableCell cell = new TableCell ();
  1173. if (columnSpan > 1)
  1174. cell.ColumnSpan = columnSpan;
  1175. if (pagerTemplate != null)
  1176. pagerTemplate.InstantiateIn (cell);
  1177. else
  1178. cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
  1179. row.Cells.Add (cell);
  1180. }
  1181. GridViewRow CreateEmptyrRow (int fieldCount)
  1182. {
  1183. GridViewRow row = CreateRow (-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
  1184. TableCell cell = new TableCell ();
  1185. cell.ColumnSpan = fieldCount;
  1186. if (emptyDataTemplate != null)
  1187. emptyDataTemplate.InstantiateIn (cell);
  1188. else
  1189. cell.Text = EmptyDataText;
  1190. row.Cells.Add (cell);
  1191. return row;
  1192. }
  1193. protected virtual void InitializeRow (GridViewRow row, DataControlField[] fields)
  1194. {
  1195. DataControlCellType ctype;
  1196. bool accessibleHeader = false;
  1197. switch (row.RowType) {
  1198. case DataControlRowType.Header:
  1199. ctype = DataControlCellType.Header;
  1200. accessibleHeader = UseAccessibleHeader;
  1201. break;
  1202. case DataControlRowType.Footer:
  1203. ctype = DataControlCellType.Footer;
  1204. break;
  1205. default:
  1206. ctype = DataControlCellType.DataCell;
  1207. break;
  1208. }
  1209. for (int n=0; n<fields.Length; n++) {
  1210. DataControlField field = fields [n];
  1211. DataControlFieldCell cell;
  1212. if (((field is BoundField) && ((BoundField)field).DataField == RowHeaderColumn) || accessibleHeader)
  1213. cell = new DataControlFieldHeaderCell (field, accessibleHeader ? TableHeaderScope.Column : TableHeaderScope.Row);
  1214. else
  1215. cell = new DataControlFieldCell (field);
  1216. row.Cells.Add (cell);
  1217. field.InitializeCell (cell, ctype, row.RowState, row.RowIndex);
  1218. }
  1219. }
  1220. IOrderedDictionary CreateRowDataKey (GridViewRow row)
  1221. {
  1222. if (cachedKeyProperties == null) {
  1223. PropertyDescriptorCollection props = TypeDescriptor.GetProperties (row.DataItem);
  1224. cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
  1225. for (int n=0; n<DataKeyNames.Length; n++) {
  1226. PropertyDescriptor p = props [DataKeyNames[n]];
  1227. if (p == null)
  1228. new InvalidOperationException ("Property '" + DataKeyNames[n] + "' not found in object of type " + row.DataItem.GetType());
  1229. cachedKeyProperties [n] = p;
  1230. }
  1231. }
  1232. OrderedDictionary dic = new OrderedDictionary ();
  1233. foreach (PropertyDescriptor p in cachedKeyProperties)
  1234. dic [p.Name] = p.GetValue (row.DataItem);
  1235. return dic;
  1236. }
  1237. IOrderedDictionary GetRowValues (GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
  1238. {
  1239. OrderedDictionary dic = new OrderedDictionary ();
  1240. ExtractRowValues (dic, row, includeReadOnlyFields, includePrimaryKey);
  1241. return dic;
  1242. }
  1243. protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
  1244. {
  1245. foreach (TableCell cell in row.Cells) {
  1246. DataControlFieldCell c = cell as DataControlFieldCell;
  1247. if (c != null)
  1248. c.ContainingField.ExtractValuesFromCell (fieldValues, c, row.RowState, includeReadOnlyFields);
  1249. }
  1250. if (!includePrimaryKey && DataKeyNames != null)
  1251. foreach (string key in DataKeyNames)
  1252. fieldValues.Remove (key);
  1253. }
  1254. protected override HtmlTextWriterTag TagKey {
  1255. get {
  1256. if (EnableSortingAndPagingCallbacks)
  1257. return HtmlTextWriterTag.Div;
  1258. else
  1259. return HtmlTextWriterTag.Table;
  1260. }
  1261. }
  1262. public sealed override void DataBind ()
  1263. {
  1264. DataKeyArrayList.Clear ();
  1265. cachedKeyProperties = null;
  1266. base.DataBind ();
  1267. keys = new DataKeyArray (DataKeyArrayList);
  1268. }
  1269. protected internal override void PerformDataBinding (IEnumerable data)
  1270. {
  1271. base.PerformDataBinding (data);
  1272. }
  1273. protected internal virtual void PrepareControlHierarchy ()
  1274. {
  1275. if (table == null)
  1276. return;
  1277. table.Caption = Caption;
  1278. table.CaptionAlign = CaptionAlign;
  1279. foreach (GridViewRow row in table.Rows) {
  1280. switch (row.RowType) {
  1281. case DataControlRowType.Header:
  1282. if (headerStyle != null && !headerStyle.IsEmpty)
  1283. row.ControlStyle.CopyFrom(headerStyle);
  1284. row.Visible = ShowHeader;
  1285. break;
  1286. case DataControlRowType.Footer:
  1287. if (footerStyle != null && !footerStyle.IsEmpty)
  1288. row.ControlStyle.CopyFrom (footerStyle);
  1289. row.Visible = ShowFooter;
  1290. break;
  1291. case DataControlRowType.Pager:
  1292. if (pagerStyle != null && !pagerStyle.IsEmpty)
  1293. row.ControlStyle.CopyFrom (pagerStyle);
  1294. break;
  1295. case DataControlRowType.EmptyDataRow:
  1296. if (emptyDataRowStyle != null && !emptyDataRowStyle.IsEmpty)
  1297. row.ControlStyle.CopyFrom (emptyDataRowStyle);
  1298. break;
  1299. case DataControlRowType.DataRow:
  1300. if (rowStyle != null && !rowStyle.IsEmpty)
  1301. row.ControlStyle.CopyFrom (rowStyle);
  1302. if ((row.RowState & DataControlRowState.Alternate) != 0 && alternatingRowStyle != null && !alternatingRowStyle.IsEmpty)
  1303. row.ControlStyle.CopyFrom (alternatingRowStyle);
  1304. if ((row.RowState & DataControlRowState.Selected) != 0 && selectedRowStyle != null && !selectedRowStyle.IsEmpty)
  1305. row.ControlStyle.CopyFrom (selectedRowStyle);
  1306. if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null && !editRowStyle.IsEmpty)
  1307. row.ControlStyle.CopyFrom (editRowStyle);
  1308. break;
  1309. default:
  1310. break;
  1311. }
  1312. foreach (TableCell cell in row.Cells) {
  1313. DataControlFieldCell fcell = cell as DataControlFieldCell;
  1314. if (fcell != null) {
  1315. DataControlField field = fcell.ContainingField;
  1316. switch (row.RowType) {
  1317. case DataControlRowType.Header:
  1318. if (field.HeaderStyleCreated && !field.HeaderStyle.IsEmpty)
  1319. cell.ControlStyle.CopyFrom (field.HeaderStyle);
  1320. break;
  1321. case DataControlRowType.Footer:
  1322. if (field.FooterStyleCreated && !field.FooterStyle.IsEmpty)
  1323. cell.ControlStyle.CopyFrom (field.FooterStyle);
  1324. break;
  1325. default:
  1326. if (field.ControlStyleCreated && !field.ControlStyle.IsEmpty)
  1327. foreach (Control c in cell.Controls) {
  1328. WebControl wc = c as WebControl;
  1329. if (wc != null)
  1330. wc.ControlStyle.MergeWith (field.ControlStyle);
  1331. }
  1332. if (field.ItemStyleCreated && !field.ItemStyle.IsEmpty)
  1333. cell.ControlStyle.CopyFrom (field.ItemStyle);
  1334. break;
  1335. }
  1336. }
  1337. }
  1338. }
  1339. }
  1340. protected internal override void OnInit (EventArgs e)
  1341. {
  1342. Page.RegisterRequiresControlState (this);
  1343. base.OnInit (e);
  1344. }
  1345. void OnFieldsChanged (object sender, EventArgs args)
  1346. {
  1347. RequireBinding ();
  1348. }
  1349. protected override void OnDataPropertyChanged ()
  1350. {
  1351. base.OnDataPropertyChanged ();
  1352. RequireBinding ();
  1353. }
  1354. protected override void OnDataSourceViewChanged (object sender, EventArgs e)
  1355. {
  1356. base.OnDataSourceViewChanged (sender, e);
  1357. RequireBinding ();
  1358. }
  1359. protected override bool OnBubbleEvent (object source, EventArgs e)
  1360. {
  1361. GridViewCommandEventArgs args = e as GridViewCommandEventArgs;
  1362. if (args != null) {
  1363. OnRowCommand (args);
  1364. string param = args.CommandArgument as string;
  1365. if (param == null || param.Length == 0) {
  1366. GridViewRow row = args.Row;
  1367. if (row != null)
  1368. param = row.RowIndex.ToString();
  1369. }
  1370. ProcessEvent (args.CommandName, param);
  1371. return true;
  1372. }
  1373. return base.OnBubbleEvent (source, e);
  1374. }
  1375. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  1376. {
  1377. RaisePostBackEvent (eventArgument);
  1378. }
  1379. // This is prolly obsolete
  1380. protected virtual void RaisePostBackEvent (string eventArgument)
  1381. {
  1382. int i = eventArgument.IndexOf ('$');
  1383. if (i != -1)
  1384. ProcessEvent (eventArgument.Substring (0, i), eventArgument.Substring (i + 1));
  1385. else
  1386. ProcessEvent (eventArgument, null);
  1387. }
  1388. void ProcessEvent (string eventName, string param)
  1389. {
  1390. switch (eventName)
  1391. {
  1392. case DataControlCommands.PageCommandName:
  1393. int newIndex = -1;
  1394. switch (param) {
  1395. case DataControlCommands.FirstPageCommandArgument:
  1396. newIndex = 0;
  1397. break;
  1398. case DataControlCommands.LastPageCommandArgument:
  1399. newIndex = PageCount - 1;
  1400. break;
  1401. case DataControlCommands.NextPageCommandArgument:
  1402. newIndex = PageIndex + 1;
  1403. break;
  1404. case DataControlCommands.PreviousPageCommandArgument:
  1405. newIndex = PageIndex - 1;
  1406. break;
  1407. default:
  1408. int paramIndex = 0;
  1409. int.TryParse (param, out paramIndex);
  1410. newIndex = paramIndex - 1;
  1411. break;
  1412. }
  1413. ShowPage (newIndex);
  1414. break;
  1415. case DataControlCommands.FirstPageCommandArgument:
  1416. ShowPage (0);
  1417. break;
  1418. case DataControlCommands.LastPageCommandArgument:
  1419. ShowPage (PageCount - 1);
  1420. break;
  1421. case DataControlCommands.NextPageCommandArgument:
  1422. if (PageIndex < PageCount - 1)
  1423. ShowPage (PageIndex + 1);
  1424. break;
  1425. case DataControlCommands.PreviousPageCommandArgument:
  1426. if (PageIndex > 0)
  1427. ShowPage (PageIndex - 1);
  1428. break;
  1429. case DataControlCommands.SelectCommandName:
  1430. SelectRow (int.Parse (param));
  1431. break;
  1432. case DataControlCommands.EditCommandName:
  1433. EditRow (int.Parse (param));
  1434. break;
  1435. case DataControlCommands.UpdateCommandName:
  1436. UpdateRow (EditIndex, true);
  1437. break;
  1438. case DataControlCommands.CancelCommandName:
  1439. CancelEdit ();
  1440. break;
  1441. case DataControlCommands.DeleteCommandName:
  1442. DeleteRow (int.Parse (param));
  1443. break;
  1444. case DataControlCommands.SortCommandName:
  1445. Sort (param);
  1446. break;
  1447. }
  1448. }
  1449. void Sort (string newSortExpression)
  1450. {
  1451. SortDirection newDirection = SortDirection.Ascending;
  1452. if (sortExpression == newSortExpression && sortDirection == SortDirection.Ascending)
  1453. newDirection = SortDirection.Descending;
  1454. Sort (newSortExpression, newDirection);
  1455. }
  1456. public virtual void Sort (string newSortExpression, SortDirection newSortDirection)
  1457. {
  1458. GridViewSortEventArgs args = new GridViewSortEventArgs (newSortExpression, newSortDirection);
  1459. OnSorting (args);
  1460. if (args.Cancel) return;
  1461. if (IsBoundUsingDataSourceID) {
  1462. EditIndex = -1;
  1463. PageIndex = 0;
  1464. SortExpression = args.SortExpression;
  1465. SortDirection = args.SortDirection;
  1466. }
  1467. OnSorted (EventArgs.Empty);
  1468. }
  1469. void SelectRow (int index)
  1470. {
  1471. GridViewSelectEventArgs args = new GridViewSelectEventArgs (index);
  1472. OnSelectedIndexChanging (args);
  1473. if (!args.Cancel) {
  1474. SelectedIndex = args.NewSelectedIndex;
  1475. OnSelectedIndexChanged (EventArgs.Empty);
  1476. }
  1477. }
  1478. void ShowPage (int newIndex)
  1479. {
  1480. GridViewPageEventArgs args = new GridViewPageEventArgs (newIndex);
  1481. OnPageIndexChanging (args);
  1482. if (!args.Cancel) {
  1483. EndRowEdit ();
  1484. PageIndex = args.NewPageIndex;
  1485. OnPageIndexChanged (EventArgs.Empty);
  1486. }
  1487. }
  1488. void EditRow (int index)
  1489. {
  1490. GridViewEditEventArgs args = new GridViewEditEventArgs (index);
  1491. OnRowEditing (args);
  1492. if (!args.Cancel) {
  1493. EditIndex = args.NewEditIndex;
  1494. }
  1495. }
  1496. void CancelEdit ()
  1497. {
  1498. GridViewCancelEditEventArgs args = new GridViewCancelEditEventArgs (EditIndex);
  1499. OnRowCancelingEdit (args);
  1500. if (!args.Cancel) {
  1501. EndRowEdit ();
  1502. }
  1503. }
  1504. [MonoTODO ("Support two-way binding expressions")]
  1505. public virtual void UpdateRow (int rowIndex, bool causesValidation)
  1506. {
  1507. if (causesValidation && Page != null)
  1508. Page.Validate ();
  1509. if (rowIndex != EditIndex) throw new NotSupportedException ();
  1510. currentEditOldValues = OldEditValues.Values;
  1511. GridViewRow row = Rows [rowIndex];
  1512. currentEditRowKeys = DataKeys [rowIndex].Values;
  1513. currentEditNewValues = GetRowValues (row, false, false);
  1514. GridViewUpdateEventArgs args = new GridViewUpdateEventArgs (EditIndex, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
  1515. OnRowUpdating (args);
  1516. if (!args.Cancel) {
  1517. DataSourceView view = GetData ();
  1518. if (view == null)
  1519. throw new HttpException ("The DataSourceView associated to data bound control was null");
  1520. if(view.CanUpdate)
  1521. view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
  1522. } else
  1523. EndRowEdit ();
  1524. }
  1525. bool UpdateCallback (int recordsAffected, Exception exception)
  1526. {
  1527. GridViewUpdatedEventArgs dargs = new GridViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
  1528. OnRowUpdated (dargs);
  1529. if (!dargs.KeepInEditMode)
  1530. EndRowEdit ();
  1531. return dargs.ExceptionHandled;
  1532. }
  1533. public virtual void DeleteRow (int rowIndex)
  1534. {
  1535. GridViewRow row = Rows [rowIndex];
  1536. currentEditRowKeys = DataKeys [rowIndex].Values;
  1537. currentEditNewValues = GetRowValues (row, true, true);
  1538. GridViewDeleteEventArgs args = new GridViewDeleteEventArgs (rowIndex, currentEditRowKeys, currentEditNewValues);
  1539. OnRowDeleting (args);
  1540. if (!args.Cancel) {
  1541. RequireBinding ();
  1542. DataSourceView view = GetData ();
  1543. if (view != null && view.CanDelete)
  1544. view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
  1545. else {
  1546. GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
  1547. OnRowDeleted (dargs);
  1548. }
  1549. }
  1550. }
  1551. bool DeleteCallback (int recordsAffected, Exception exception)
  1552. {
  1553. GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
  1554. OnRowDeleted (dargs);
  1555. return dargs.ExceptionHandled;
  1556. }
  1557. void EndRowEdit ()
  1558. {
  1559. EditIndex = -1;
  1560. oldEditValues = new DataKey (new OrderedDictionary ());
  1561. currentEditRowKeys = null;
  1562. currentEditOldValues = null;
  1563. currentEditNewValues = null;
  1564. }
  1565. protected internal override void LoadControlState (object ob)
  1566. {
  1567. if (ob == null) return;
  1568. object[] state = (object[]) ob;
  1569. base.LoadControlState (state[0]);
  1570. pageIndex = (int) state[1];
  1571. selectedIndex = (int) state[2];
  1572. editIndex = (int) state[3];
  1573. sortExpression = (string) state[4];
  1574. sortDirection = (SortDirection) state[5];
  1575. DataKeyNames = (string []) state [6];
  1576. if (state [7] != null)
  1577. LoadDataKeyArrayState ((object []) state [7]);
  1578. if (state [8] != null)
  1579. ((IStateManager) OldEditValues).LoadViewState (state [8]);
  1580. }
  1581. protected internal override object SaveControlState ()
  1582. {
  1583. if (EnableSortingAndPagingCallbacks) {
  1584. Page.ClientScript.RegisterHiddenField (ClientID + "_Page", PageIndex.ToString ());
  1585. Page.ClientScript.RegisterHiddenField (ClientID + "_SortExpression", SortExpression);
  1586. Page.ClientScript.RegisterHiddenField (ClientID + "_SortDirection", ((int)SortDirection).ToString());
  1587. }
  1588. object bstate = base.SaveControlState ();
  1589. return new object [] {
  1590. bstate,
  1591. pageIndex,
  1592. selectedIndex,
  1593. editIndex,
  1594. sortExpression,
  1595. sortDirection,
  1596. DataKeyNames,
  1597. SaveDataKeyArrayState (),
  1598. (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState ())
  1599. };
  1600. }
  1601. object [] SaveDataKeyArrayState ()
  1602. {
  1603. if (keys == null)
  1604. return null;
  1605. object [] state = new object [keys.Count];
  1606. for (int i = 0; i < keys.Count; i++) {
  1607. state [i] = ((IStateManager) keys [i]).SaveViewState ();
  1608. }
  1609. return state;
  1610. }
  1611. void LoadDataKeyArrayState (object [] state)
  1612. {
  1613. for (int i = 0; i < state.Length; i++) {
  1614. DataKey dataKey = new DataKey (new OrderedDictionary (DataKeyNames.Length), DataKeyNames);
  1615. ((IStateManager) dataKey).LoadViewState (state [i]);
  1616. DataKeyArrayList.Add (dataKey);
  1617. }
  1618. keys = new DataKeyArray (DataKeyArrayList);
  1619. }
  1620. protected override void TrackViewState()
  1621. {
  1622. base.TrackViewState();
  1623. if (columns != null) ((IStateManager)columns).TrackViewState();
  1624. if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
  1625. if (alternatingRowStyle != null) ((IStateManager)alternatingRowStyle).TrackViewState();
  1626. if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
  1627. if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
  1628. if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
  1629. if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
  1630. if (selectedRowStyle != null) ((IStateManager)selectedRowStyle).TrackViewState();
  1631. if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
  1632. if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
  1633. if (keys != null) ((IStateManager)keys).TrackViewState();
  1634. if (autoFieldProperties != null) {
  1635. foreach (IStateManager sm in autoFieldProperties)
  1636. sm.TrackViewState ();
  1637. }
  1638. }
  1639. protected override object SaveViewState()
  1640. {
  1641. object[] states = new object [12];
  1642. states[0] = base.SaveViewState();
  1643. states[1] = (columns == null ? null : ((IStateManager)columns).SaveViewState());
  1644. states[2] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
  1645. states[3] = (alternatingRowStyle == null ? null : ((IStateManager)alternatingRowStyle).SaveViewState());
  1646. states[4] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
  1647. states[5] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
  1648. states[6] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
  1649. states[7] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
  1650. states[8] = (selectedRowStyle == null ? null : ((IStateManager)selectedRowStyle).SaveViewState());
  1651. states[9] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
  1652. states[10] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
  1653. if (autoFieldProperties != null) {
  1654. object[] data = new object [autoFieldProperties.Length];
  1655. bool allNull = true;
  1656. for (int n=0; n<data.Length; n++) {
  1657. data [n] = ((IStateManager)autoFieldProperties [n]).SaveViewState ();
  1658. if (data [n] != null) allNull = false;
  1659. }
  1660. if (!allNull) states [11] = data;
  1661. }
  1662. for (int i = states.Length - 1; i >= 0; i--) {
  1663. if (states [i] != null)
  1664. return states;
  1665. }
  1666. return null;
  1667. }
  1668. protected override void LoadViewState (object savedState)
  1669. {
  1670. if (savedState == null) {
  1671. base.LoadViewState (null);
  1672. return;
  1673. }
  1674. object [] states = (object []) savedState;
  1675. if (states[11] != null) {
  1676. object[] data = (object[]) states [11];
  1677. autoFieldProperties = new AutoGeneratedFieldProperties [data.Length];
  1678. for (int n=0; n<data.Length; n++) {
  1679. IStateManager p = new AutoGeneratedFieldProperties ();
  1680. p.TrackViewState ();
  1681. p.LoadViewState (data [n]);
  1682. autoFieldProperties [n] = (AutoGeneratedFieldProperties) p;
  1683. }
  1684. }
  1685. base.LoadViewState (states[0]);
  1686. if (states[1] != null) ((IStateManager)Columns).LoadViewState (states[1]);
  1687. if (states[2] != null) ((IStateManager)PagerSettings).LoadViewState (states[2]);
  1688. if (states[3] != null) ((IStateManager)AlternatingRowStyle).LoadViewState (states[3]);
  1689. if (states[4] != null) ((IStateManager)FooterStyle).LoadViewState (states[4]);
  1690. if (states[5] != null) ((IStateManager)HeaderStyle).LoadViewState (states[5]);
  1691. if (states[6] != null) ((IStateManager)PagerStyle).LoadViewState (states[6]);
  1692. if (states[7] != null) ((IStateManager)RowStyle).LoadViewState (states[7]);
  1693. if (states[8] != null) ((IStateManager)SelectedRowStyle).LoadViewState (states[8]);
  1694. if (states[9] != null) ((IStateManager)EditRowStyle).LoadViewState (states[9]);
  1695. if (states[10] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
  1696. }
  1697. void ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
  1698. {
  1699. RaiseCallbackEvent (eventArgs);
  1700. }
  1701. protected virtual void RaiseCallbackEvent (string eventArgs)
  1702. {
  1703. string[] clientData = eventArgs.Split ('|');
  1704. PageIndex = int.Parse (clientData[0]);
  1705. SortExpression = HttpUtility.UrlDecode (clientData [1]);
  1706. SortDirection = (SortDirection) int.Parse (clientData [2]);
  1707. RaisePostBackEvent (clientData[3]);
  1708. DataBind ();
  1709. }
  1710. string ICallbackEventHandler.GetCallbackResult ()
  1711. {
  1712. return GetCallbackResult ();
  1713. }
  1714. protected virtual string GetCallbackResult ()
  1715. {
  1716. PrepareControlHierarchy ();
  1717. StringWriter sw = new StringWriter ();
  1718. sw.Write (PageIndex.ToString () + '|' + SortExpression + '|' + (int) SortDirection + '|');
  1719. HtmlTextWriter writer = new HtmlTextWriter (sw);
  1720. RenderGrid (writer);
  1721. return sw.ToString ();
  1722. }
  1723. string ICallbackContainer.GetCallbackScript (IButtonControl control, string argument)
  1724. {
  1725. return GetCallbackScript (control, argument);
  1726. }
  1727. protected virtual string GetCallbackScript (IButtonControl control, string argument)
  1728. {
  1729. if (EnableSortingAndPagingCallbacks)
  1730. return "javascript:GridView_ClientEvent (\"" + ClientID + "\",\"" + control.CommandName + "$" + control.CommandArgument + "\"); return false;";
  1731. else
  1732. return null;
  1733. }
  1734. protected override void OnPagePreLoad (object sender, EventArgs e)
  1735. {
  1736. base.OnPagePreLoad (sender, e);
  1737. if (Page.IsPostBack && EnableSortingAndPagingCallbacks) {
  1738. int page;
  1739. if (int.TryParse (Page.Request.Form [ClientID + "_Page"], out page))
  1740. PageIndex = page;
  1741. int dir;
  1742. if (int.TryParse (Page.Request.Form [ClientID + "_SortDirection"], out dir))
  1743. SortDirection = (SortDirection) dir;
  1744. SortExpression = Page.Request.Form [ClientID + "_SortExpression"];
  1745. }
  1746. }
  1747. protected internal override void OnPreRender (EventArgs e)
  1748. {
  1749. base.OnPreRender (e);
  1750. if (EnableSortingAndPagingCallbacks)
  1751. {
  1752. if (!Page.ClientScript.IsClientScriptIncludeRegistered (typeof(GridView), "GridView.js")) {
  1753. string url = Page.ClientScript.GetWebResourceUrl (typeof(GridView), "GridView.js");
  1754. Page.ClientScript.RegisterClientScriptInclude (typeof(GridView), "GridView.js", url);
  1755. }
  1756. string cgrid = ClientID + "_data";
  1757. string script = string.Format ("var {0} = new Object ();\n", cgrid);
  1758. script += string.Format ("{0}.pageIndex = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (PageIndex));
  1759. script += string.Format ("{0}.sortExp = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (SortExpression == null ? "" : SortExpression));
  1760. script += string.Format ("{0}.sortDir = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral ((int) SortDirection));
  1761. script += string.Format ("{0}.uid = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (UniqueID));
  1762. Page.ClientScript.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
  1763. // Make sure the basic script infrastructure is rendered
  1764. Page.ClientScript.GetCallbackEventReference (this, "null", "", "null");
  1765. Page.ClientScript.GetPostBackClientHyperlink (this, "");
  1766. }
  1767. }
  1768. protected internal override void Render (HtmlTextWriter writer)
  1769. {
  1770. PrepareControlHierarchy ();
  1771. if (EnableSortingAndPagingCallbacks)
  1772. writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
  1773. writer.RenderBeginTag (HtmlTextWriterTag.Div);
  1774. RenderGrid (writer);
  1775. writer.RenderEndTag ();
  1776. }
  1777. void RenderGrid (HtmlTextWriter writer)
  1778. {
  1779. if (table == null)
  1780. return;
  1781. table.Render (writer);
  1782. }
  1783. PostBackOptions IPostBackContainer.GetPostBackOptions (IButtonControl control)
  1784. {
  1785. if (control == null)
  1786. throw new ArgumentNullException ("control");
  1787. if (control.CausesValidation)
  1788. throw new InvalidOperationException ("A button that causes validation in GridView '" + ID + "' is attempting to use the container GridView as the post back target. The button should either turn off validation or use itself as the post back container.");
  1789. PostBackOptions options = new PostBackOptions (this);
  1790. options.Argument = control.CommandName + "$" + control.CommandArgument;
  1791. options.RequiresJavaScriptProtocol = true;
  1792. return options;
  1793. }
  1794. }
  1795. }
  1796. #endif