GridView.cs 57 KB

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