2
0

GridView.cs 56 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 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. DataKeyArray keys;
  70. DataKey oldEditValues;
  71. AutoGeneratedFieldProperties[] autoFieldProperties;
  72. string [] dataKeyNames = null;
  73. readonly string[] emptyKeys = new string[0];
  74. private static readonly object PageIndexChangedEvent = new object();
  75. private static readonly object PageIndexChangingEvent = new object();
  76. private static readonly object RowCancelingEditEvent = new object();
  77. private static readonly object RowCommandEvent = new object();
  78. private static readonly object RowCreatedEvent = new object();
  79. private static readonly object RowDataBoundEvent = new object();
  80. private static readonly object RowDeletedEvent = new object();
  81. private static readonly object RowDeletingEvent = new object();
  82. private static readonly object RowEditingEvent = new object();
  83. private static readonly object RowUpdatedEvent = new object();
  84. private static readonly object RowUpdatingEvent = new object();
  85. private static readonly object SelectedIndexChangedEvent = new object();
  86. private static readonly object SelectedIndexChangingEvent = new object();
  87. private static readonly object SortedEvent = new object();
  88. private static readonly object SortingEvent = new object();
  89. // Control state
  90. int pageIndex;
  91. int pageCount = 0;
  92. int selectedIndex = -1;
  93. int editIndex = -1;
  94. SortDirection sortDirection = SortDirection.Ascending;
  95. string sortExpression = string.Empty;
  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. [BrowsableAttribute (false)]
  465. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  466. public virtual DataKeyArray DataKeys {
  467. get {
  468. EnsureDataBound ();
  469. return keys;
  470. }
  471. }
  472. [WebCategoryAttribute ("Misc")]
  473. [DefaultValueAttribute (-1)]
  474. public virtual int EditIndex {
  475. get {
  476. return editIndex;
  477. }
  478. set {
  479. editIndex = value;
  480. RequireBinding ();
  481. }
  482. }
  483. [WebCategoryAttribute ("Styles")]
  484. [PersistenceMode (PersistenceMode.InnerProperty)]
  485. [NotifyParentProperty (true)]
  486. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  487. public TableItemStyle EditRowStyle {
  488. get {
  489. if (editRowStyle == null) {
  490. editRowStyle = new TableItemStyle ();
  491. if (IsTrackingViewState)
  492. editRowStyle.TrackViewState();
  493. }
  494. return editRowStyle;
  495. }
  496. }
  497. [WebCategoryAttribute ("Styles")]
  498. [PersistenceMode (PersistenceMode.InnerProperty)]
  499. [NotifyParentProperty (true)]
  500. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  501. public TableItemStyle EmptyDataRowStyle {
  502. get {
  503. if (emptyDataRowStyle == null) {
  504. emptyDataRowStyle = new TableItemStyle ();
  505. if (IsTrackingViewState)
  506. emptyDataRowStyle.TrackViewState();
  507. }
  508. return emptyDataRowStyle;
  509. }
  510. }
  511. [DefaultValue (null)]
  512. [TemplateContainer (typeof(GridViewRow), BindingDirection.OneWay)]
  513. [PersistenceMode (PersistenceMode.InnerProperty)]
  514. [Browsable (false)]
  515. public virtual ITemplate EmptyDataTemplate {
  516. get { return emptyDataTemplate; }
  517. set { emptyDataTemplate = value; RequireBinding (); }
  518. }
  519. [LocalizableAttribute (true)]
  520. [WebCategoryAttribute ("Appearance")]
  521. [DefaultValueAttribute ("")]
  522. public virtual string EmptyDataText {
  523. get {
  524. object ob = ViewState ["EmptyDataText"];
  525. if (ob != null) return (string) ob;
  526. return string.Empty;
  527. }
  528. set {
  529. ViewState ["EmptyDataText"] = value;
  530. RequireBinding ();
  531. }
  532. }
  533. [WebCategoryAttribute ("Behavior")]
  534. [DefaultValueAttribute (false)]
  535. public virtual bool EnableSortingAndPagingCallbacks {
  536. get {
  537. object ob = ViewState ["EnableSortingAndPagingCallbacks"];
  538. if (ob != null) return (bool) ob;
  539. return false;
  540. }
  541. set {
  542. ViewState ["EnableSortingAndPagingCallbacks"] = value;
  543. RequireBinding ();
  544. }
  545. }
  546. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  547. [BrowsableAttribute (false)]
  548. public virtual GridViewRow FooterRow {
  549. get {
  550. if (table != null) {
  551. for (int index = table.Rows.Count - 1; index >= 0; index--) {
  552. GridViewRow row = (GridViewRow) table.Rows [index];
  553. switch (row.RowType) {
  554. case DataControlRowType.Separator:
  555. case DataControlRowType.Pager:
  556. continue;
  557. case DataControlRowType.Footer:
  558. return row;
  559. default:
  560. break;
  561. }
  562. }
  563. }
  564. return null;
  565. }
  566. }
  567. [WebCategoryAttribute ("Styles")]
  568. [PersistenceMode (PersistenceMode.InnerProperty)]
  569. [NotifyParentProperty (true)]
  570. [DefaultValue (null)]
  571. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  572. public TableItemStyle FooterStyle {
  573. get {
  574. if (footerStyle == null) {
  575. footerStyle = new TableItemStyle ();
  576. if (IsTrackingViewState)
  577. footerStyle.TrackViewState();
  578. }
  579. return footerStyle;
  580. }
  581. }
  582. [WebCategoryAttribute ("Appearance")]
  583. [DefaultValueAttribute (GridLines.Both)]
  584. public virtual GridLines GridLines {
  585. get {
  586. if (ControlStyleCreated)
  587. return ((TableStyle) ControlStyle).GridLines;
  588. return GridLines.Both;
  589. }
  590. set {
  591. ((TableStyle) ControlStyle).GridLines = value;
  592. }
  593. }
  594. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  595. [BrowsableAttribute (false)]
  596. public virtual GridViewRow HeaderRow {
  597. get {
  598. if (table != null) {
  599. for (int index = 0, total = table.Rows.Count; index < total; index++) {
  600. GridViewRow row = (GridViewRow) table.Rows [index];
  601. switch (row.RowType) {
  602. case DataControlRowType.Separator:
  603. case DataControlRowType.Pager:
  604. continue;
  605. case DataControlRowType.Header:
  606. return row;
  607. default:
  608. break;
  609. }
  610. }
  611. }
  612. return null;
  613. }
  614. }
  615. [WebCategoryAttribute ("Styles")]
  616. [PersistenceMode (PersistenceMode.InnerProperty)]
  617. [NotifyParentProperty (true)]
  618. [DefaultValue (null)]
  619. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  620. public TableItemStyle HeaderStyle {
  621. get {
  622. if (headerStyle == null) {
  623. headerStyle = new TableItemStyle ();
  624. if (IsTrackingViewState)
  625. headerStyle.TrackViewState();
  626. }
  627. return headerStyle;
  628. }
  629. }
  630. [Category ("Layout")]
  631. [DefaultValueAttribute (HorizontalAlign.NotSet)]
  632. public virtual HorizontalAlign HorizontalAlign {
  633. get {
  634. if (ControlStyleCreated)
  635. return ((TableStyle) ControlStyle).HorizontalAlign;
  636. return HorizontalAlign.NotSet;
  637. }
  638. set {
  639. ((TableStyle) ControlStyle).HorizontalAlign = value;
  640. }
  641. }
  642. [BrowsableAttribute (false)]
  643. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  644. public virtual int PageCount {
  645. get {
  646. if (pageCount != 0) return pageCount;
  647. EnsureDataBound ();
  648. return pageCount;
  649. }
  650. }
  651. [WebCategoryAttribute ("Paging")]
  652. [BrowsableAttribute (true)]
  653. [DefaultValueAttribute (0)]
  654. public virtual int PageIndex {
  655. get {
  656. return pageIndex;
  657. }
  658. set {
  659. pageIndex = value;
  660. RequireBinding ();
  661. }
  662. }
  663. [DefaultValueAttribute (10)]
  664. [WebCategoryAttribute ("Paging")]
  665. public virtual int PageSize {
  666. get {
  667. object ob = ViewState ["PageSize"];
  668. if (ob != null) return (int) ob;
  669. return 10;
  670. }
  671. set {
  672. ViewState ["PageSize"] = value;
  673. RequireBinding ();
  674. }
  675. }
  676. [WebCategoryAttribute ("Paging")]
  677. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  678. [NotifyParentPropertyAttribute (true)]
  679. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  680. public virtual PagerSettings PagerSettings {
  681. get {
  682. if (pagerSettings == null) {
  683. pagerSettings = new PagerSettings (this);
  684. if (IsTrackingViewState)
  685. ((IStateManager)pagerSettings).TrackViewState ();
  686. }
  687. return pagerSettings;
  688. }
  689. }
  690. [WebCategoryAttribute ("Styles")]
  691. [PersistenceMode (PersistenceMode.InnerProperty)]
  692. [NotifyParentProperty (true)]
  693. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  694. public TableItemStyle PagerStyle {
  695. get {
  696. if (pagerStyle == null) {
  697. pagerStyle = new TableItemStyle ();
  698. if (IsTrackingViewState)
  699. pagerStyle.TrackViewState();
  700. }
  701. return pagerStyle;
  702. }
  703. }
  704. [DefaultValue (null)]
  705. /* DataControlPagerCell isnt specified in the docs */
  706. //[TemplateContainer (typeof(DataControlPagerCell), BindingDirection.OneWay)]
  707. [PersistenceMode (PersistenceMode.InnerProperty)]
  708. [Browsable (false)]
  709. public virtual ITemplate PagerTemplate {
  710. get { return pagerTemplate; }
  711. set { pagerTemplate = value; RequireBinding (); }
  712. }
  713. [DefaultValueAttribute ("")]
  714. [WebCategoryAttribute ("Accessibility")]
  715. // [TypeConverterAttribute (typeof(System.Web.UI.Design.DataColumnSelectionConverter)]
  716. public virtual string RowHeaderColumn {
  717. get {
  718. object ob = ViewState ["RowHeaderColumn"];
  719. if (ob != null) return (string) ob;
  720. return string.Empty;
  721. }
  722. set {
  723. ViewState ["RowHeaderColumn"] = value;
  724. RequireBinding ();
  725. }
  726. }
  727. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  728. [BrowsableAttribute (false)]
  729. public virtual GridViewRowCollection Rows {
  730. get {
  731. EnsureDataBound ();
  732. return rows;
  733. }
  734. }
  735. [WebCategoryAttribute ("Styles")]
  736. [PersistenceMode (PersistenceMode.InnerProperty)]
  737. [NotifyParentProperty (true)]
  738. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  739. public TableItemStyle RowStyle {
  740. get {
  741. if (rowStyle == null) {
  742. rowStyle = new TableItemStyle ();
  743. if (IsTrackingViewState)
  744. rowStyle.TrackViewState();
  745. }
  746. return rowStyle;
  747. }
  748. }
  749. [BrowsableAttribute (false)]
  750. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  751. public virtual DataKey SelectedDataKey {
  752. get {
  753. if (DataKeys == null)
  754. throw new InvalidOperationException ("DataKeys");
  755. if (selectedIndex >= 0 && selectedIndex < DataKeys.Count) {
  756. return DataKeys [selectedIndex];
  757. } else
  758. return null;
  759. }
  760. }
  761. [BindableAttribute (true)]
  762. [DefaultValueAttribute (-1)]
  763. public virtual int SelectedIndex {
  764. get {
  765. return selectedIndex;
  766. }
  767. set {
  768. if (Rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
  769. int oldIndex = selectedIndex;
  770. selectedIndex = -1;
  771. Rows [oldIndex].RowState = GetRowState (oldIndex);
  772. }
  773. selectedIndex = value;
  774. if (Rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
  775. Rows [selectedIndex].RowState = GetRowState (selectedIndex);
  776. }
  777. }
  778. }
  779. [BrowsableAttribute (false)]
  780. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  781. public virtual GridViewRow SelectedRow {
  782. get {
  783. if (selectedIndex >= 0 && selectedIndex < Rows.Count) {
  784. return Rows [selectedIndex];
  785. } else
  786. return null;
  787. }
  788. }
  789. [WebCategoryAttribute ("Styles")]
  790. [PersistenceMode (PersistenceMode.InnerProperty)]
  791. [NotifyParentProperty (true)]
  792. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  793. public TableItemStyle SelectedRowStyle {
  794. get {
  795. if (selectedRowStyle == null) {
  796. selectedRowStyle = new TableItemStyle ();
  797. if (IsTrackingViewState)
  798. selectedRowStyle.TrackViewState();
  799. }
  800. return selectedRowStyle;
  801. }
  802. }
  803. [BrowsableAttribute (false)]
  804. public object SelectedValue {
  805. get {
  806. if (SelectedDataKey != null)
  807. return SelectedDataKey.Value;
  808. else
  809. return null;
  810. }
  811. }
  812. [WebCategoryAttribute ("Appearance")]
  813. [DefaultValueAttribute (false)]
  814. public virtual bool ShowFooter {
  815. get {
  816. object ob = ViewState ["ShowFooter"];
  817. if (ob != null) return (bool) ob;
  818. return false;
  819. }
  820. set {
  821. ViewState ["ShowFooter"] = value;
  822. RequireBinding ();
  823. }
  824. }
  825. [WebCategoryAttribute ("Appearance")]
  826. [DefaultValueAttribute (true)]
  827. public virtual bool ShowHeader {
  828. get {
  829. object ob = ViewState ["ShowHeader"];
  830. if (ob != null) return (bool) ob;
  831. return true;
  832. }
  833. set {
  834. ViewState ["ShowHeader"] = value;
  835. RequireBinding ();
  836. }
  837. }
  838. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  839. [BrowsableAttribute (false)]
  840. [DefaultValueAttribute (SortDirection.Ascending)]
  841. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  842. public virtual SortDirection SortDirection {
  843. get { return sortDirection; }
  844. }
  845. [BrowsableAttribute (false)]
  846. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  847. public virtual string SortExpression {
  848. get { return sortExpression; }
  849. }
  850. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  851. [BrowsableAttribute (false)]
  852. public virtual GridViewRow TopPagerRow {
  853. get {
  854. EnsureDataBound ();
  855. return topPagerRow;
  856. }
  857. }
  858. [WebCategoryAttribute ("Accessibility")]
  859. [DefaultValueAttribute (true)]
  860. public virtual bool UseAccessibleHeader {
  861. get {
  862. object ob = ViewState ["UseAccessibleHeader"];
  863. if (ob != null) return (bool) ob;
  864. return true;
  865. }
  866. set {
  867. ViewState ["UseAccessibleHeader"] = value;
  868. RequireBinding ();
  869. }
  870. }
  871. public virtual bool IsBindableType (Type type)
  872. {
  873. return type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Guid);
  874. }
  875. protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
  876. {
  877. return base.CreateDataSourceSelectArguments ();
  878. }
  879. protected virtual ICollection CreateColumns (PagedDataSource dataSource, bool useDataSource)
  880. {
  881. ArrayList fields = new ArrayList ();
  882. fields.AddRange (Columns);
  883. if (AutoGenerateEditButton || AutoGenerateDeleteButton || AutoGenerateSelectButton) {
  884. CommandField field = new CommandField ();
  885. field.ShowEditButton = AutoGenerateEditButton;
  886. field.ShowDeleteButton = AutoGenerateDeleteButton;
  887. field.ShowSelectButton = AutoGenerateSelectButton;
  888. fields.Add (field);
  889. }
  890. if (AutoGenerateColumns) {
  891. if (useDataSource)
  892. autoFieldProperties = CreateAutoFieldProperties (dataSource);
  893. if (autoFieldProperties != null) {
  894. foreach (AutoGeneratedFieldProperties props in autoFieldProperties)
  895. fields.Add (CreateAutoGeneratedColumn (props));
  896. }
  897. }
  898. return fields;
  899. }
  900. protected virtual AutoGeneratedField CreateAutoGeneratedColumn (AutoGeneratedFieldProperties fieldProperties)
  901. {
  902. return new AutoGeneratedField (fieldProperties);
  903. }
  904. AutoGeneratedFieldProperties[] CreateAutoFieldProperties (PagedDataSource source)
  905. {
  906. if(source == null) return null;
  907. PropertyDescriptorCollection props = source.GetItemProperties (new PropertyDescriptor[0]);
  908. Type prop_type;
  909. ArrayList retVal = new ArrayList();
  910. if (props == null)
  911. {
  912. object fitem = null;
  913. prop_type = null;
  914. PropertyInfo prop_item = source.DataSource.GetType().GetProperty("Item",
  915. BindingFlags.Instance | BindingFlags.Static |
  916. BindingFlags.Public, null, null,
  917. new Type[] { typeof(int) }, null);
  918. if (prop_item != null) {
  919. prop_type = prop_item.PropertyType;
  920. }
  921. if (prop_type == null || prop_type == typeof(object)) {
  922. IEnumerator en = source.GetEnumerator();
  923. if (en.MoveNext())
  924. fitem = en.Current;
  925. if (fitem != null)
  926. prop_type = fitem.GetType();
  927. }
  928. if (fitem != null && fitem is ICustomTypeDescriptor) {
  929. props = TypeDescriptor.GetProperties(fitem);
  930. } else if (prop_type != null) {
  931. if (IsBindableType (prop_type)) {
  932. AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
  933. ((IStateManager)field).TrackViewState();
  934. field.Name = "Item";
  935. field.DataField = BoundField.ThisExpression;
  936. field.Type = prop_type;
  937. retVal.Add (field);
  938. } else {
  939. props = TypeDescriptor.GetProperties (prop_type);
  940. }
  941. }
  942. }
  943. if (props != null && props.Count > 0)
  944. {
  945. foreach (PropertyDescriptor current in props) {
  946. if (IsBindableType (current.PropertyType)) {
  947. AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
  948. ((IStateManager)field).TrackViewState();
  949. field.Name = current.Name;
  950. field.DataField = current.Name;
  951. field.IsReadOnly = current.IsReadOnly;
  952. field.Type = current.PropertyType;
  953. retVal.Add (field);
  954. }
  955. }
  956. }
  957. if (retVal.Count > 0)
  958. return (AutoGeneratedFieldProperties[]) retVal.ToArray (typeof(AutoGeneratedFieldProperties));
  959. else
  960. return new AutoGeneratedFieldProperties [0];
  961. }
  962. protected virtual GridViewRow CreateRow (int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState)
  963. {
  964. GridViewRow row = new GridViewRow (rowIndex, dataSourceIndex, rowType, rowState);
  965. OnRowCreated (new GridViewRowEventArgs (row));
  966. return row;
  967. }
  968. void RequireBinding ()
  969. {
  970. if (Initialized) {
  971. RequiresDataBinding = true;
  972. pageCount = -1;
  973. }
  974. }
  975. protected virtual Table CreateChildTable ()
  976. {
  977. return new ContainedTable (this);
  978. }
  979. protected override int CreateChildControls (IEnumerable data, bool dataBinding)
  980. {
  981. PagedDataSource dataSource;
  982. if (dataBinding) {
  983. DataSourceView view = GetData ();
  984. dataSource = new PagedDataSource ();
  985. dataSource.DataSource = data;
  986. if (AllowPaging) {
  987. dataSource.AllowPaging = true;
  988. dataSource.PageSize = PageSize;
  989. dataSource.CurrentPageIndex = PageIndex;
  990. if (view.CanPage) {
  991. dataSource.AllowServerPaging = true;
  992. if (view.CanRetrieveTotalRowCount)
  993. dataSource.VirtualCount = SelectArguments.TotalRowCount;
  994. }
  995. }
  996. pageCount = dataSource.PageCount;
  997. }
  998. else
  999. {
  1000. dataSource = new PagedDataSource ();
  1001. dataSource.DataSource = data;
  1002. if (AllowPaging) {
  1003. dataSource.AllowPaging = true;
  1004. dataSource.PageSize = PageSize;
  1005. dataSource.CurrentPageIndex = PageIndex;
  1006. }
  1007. }
  1008. bool showPager = AllowPaging && (PageCount > 1);
  1009. Controls.Clear ();
  1010. table = CreateChildTable ();
  1011. Controls.Add (table);
  1012. ArrayList list = new ArrayList ();
  1013. ArrayList keyList = new ArrayList ();
  1014. // Creates the set of fields to show
  1015. ICollection fieldCollection = CreateColumns (dataSource, dataBinding);
  1016. DataControlField[] fields = new DataControlField [fieldCollection.Count];
  1017. fieldCollection.CopyTo (fields, 0);
  1018. foreach (DataControlField field in fields) {
  1019. field.Initialize (AllowSorting, this);
  1020. if (EnableSortingAndPagingCallbacks)
  1021. field.ValidateSupportsCallback ();
  1022. }
  1023. // Main table creation
  1024. if (showPager && PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom) {
  1025. topPagerRow = CreatePagerRow (fields.Length, dataSource);
  1026. table.Rows.Add (topPagerRow);
  1027. }
  1028. GridViewRow headerRow = CreateRow (0, 0, DataControlRowType.Header, DataControlRowState.Normal);
  1029. table.Rows.Add (headerRow);
  1030. InitializeRow (headerRow, fields);
  1031. foreach (object obj in dataSource) {
  1032. DataControlRowState rstate = GetRowState (list.Count);
  1033. GridViewRow row = CreateRow (list.Count, list.Count, DataControlRowType.DataRow, rstate);
  1034. row.DataItem = obj;
  1035. list.Add (row);
  1036. table.Rows.Add (row);
  1037. InitializeRow (row, fields);
  1038. if (dataBinding) {
  1039. row.DataBind ();
  1040. OnRowDataBound (new GridViewRowEventArgs (row));
  1041. if (EditIndex == row.RowIndex)
  1042. oldEditValues = new DataKey (GetRowValues (row, false, true));
  1043. keyList.Add (new DataKey (CreateRowDataKey (row), DataKeyNames));
  1044. } else {
  1045. if (EditIndex == row.RowIndex)
  1046. oldEditValues = new DataKey (new OrderedDictionary ());
  1047. keyList.Add (new DataKey (new OrderedDictionary (), DataKeyNames));
  1048. }
  1049. if (list.Count >= PageSize)
  1050. break;
  1051. }
  1052. if (list.Count == 0)
  1053. table.Rows.Add (CreateEmptyrRow (fields.Length));
  1054. GridViewRow footerRow = CreateRow (0, 0, DataControlRowType.Footer, DataControlRowState.Normal);
  1055. table.Rows.Add (footerRow);
  1056. InitializeRow (footerRow, fields);
  1057. if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) {
  1058. bottomPagerRow = CreatePagerRow (fields.Length, dataSource);
  1059. table.Rows.Add (bottomPagerRow);
  1060. }
  1061. rows = new GridViewRowCollection (list);
  1062. keys = new DataKeyArray (keyList);
  1063. return dataSource.DataSourceCount;
  1064. }
  1065. protected override Style CreateControlStyle ()
  1066. {
  1067. TableStyle style = new TableStyle (ViewState);
  1068. style.GridLines = GridLines.Both;
  1069. style.CellSpacing = 0;
  1070. return style;
  1071. }
  1072. DataControlRowState GetRowState (int index)
  1073. {
  1074. DataControlRowState rstate = (index % 2) == 0 ? DataControlRowState.Normal : DataControlRowState.Alternate;
  1075. if (index == SelectedIndex) rstate |= DataControlRowState.Selected;
  1076. if (index == EditIndex) rstate |= DataControlRowState.Edit;
  1077. return rstate;
  1078. }
  1079. GridViewRow CreatePagerRow (int fieldCount, PagedDataSource dataSource)
  1080. {
  1081. GridViewRow row = CreateRow (-1, -1, DataControlRowType.Pager, DataControlRowState.Normal);
  1082. InitializePager (row, fieldCount, dataSource);
  1083. return row;
  1084. }
  1085. protected virtual void InitializePager (GridViewRow row, int columnSpan, PagedDataSource dataSource)
  1086. {
  1087. TableCell cell = new TableCell ();
  1088. if (columnSpan > 1)
  1089. cell.ColumnSpan = columnSpan;
  1090. if (pagerTemplate != null)
  1091. pagerTemplate.InstantiateIn (cell);
  1092. else
  1093. cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount, pagerStyle));
  1094. row.Cells.Add (cell);
  1095. }
  1096. GridViewRow CreateEmptyrRow (int fieldCount)
  1097. {
  1098. GridViewRow row = CreateRow (-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
  1099. TableCell cell = new TableCell ();
  1100. cell.ColumnSpan = fieldCount;
  1101. if (emptyDataTemplate != null)
  1102. emptyDataTemplate.InstantiateIn (cell);
  1103. else
  1104. cell.Text = EmptyDataText;
  1105. row.Cells.Add (cell);
  1106. return row;
  1107. }
  1108. protected virtual void InitializeRow (GridViewRow row, DataControlField[] fields)
  1109. {
  1110. DataControlCellType ctype;
  1111. bool accessibleHeader = false;
  1112. switch (row.RowType) {
  1113. case DataControlRowType.Header:
  1114. ctype = DataControlCellType.Header;
  1115. accessibleHeader = UseAccessibleHeader;
  1116. break;
  1117. case DataControlRowType.Footer:
  1118. ctype = DataControlCellType.Footer;
  1119. break;
  1120. default:
  1121. ctype = DataControlCellType.DataCell;
  1122. break;
  1123. }
  1124. for (int n=0; n<fields.Length; n++) {
  1125. DataControlField field = fields [n];
  1126. DataControlFieldCell cell;
  1127. if (((field is BoundField) && ((BoundField)field).DataField == RowHeaderColumn) || accessibleHeader)
  1128. cell = new DataControlFieldHeaderCell (field, accessibleHeader ? TableHeaderScope.Column : TableHeaderScope.Row);
  1129. else
  1130. cell = new DataControlFieldCell (field);
  1131. row.Cells.Add (cell);
  1132. field.InitializeCell (cell, ctype, row.RowState, row.RowIndex);
  1133. }
  1134. }
  1135. IOrderedDictionary CreateRowDataKey (GridViewRow row)
  1136. {
  1137. if (cachedKeyProperties == null) {
  1138. PropertyDescriptorCollection props = TypeDescriptor.GetProperties (row.DataItem);
  1139. cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
  1140. for (int n=0; n<DataKeyNames.Length; n++) {
  1141. PropertyDescriptor p = props [DataKeyNames[n]];
  1142. if (p == null)
  1143. new InvalidOperationException ("Property '" + DataKeyNames[n] + "' not found in object of type " + row.DataItem.GetType());
  1144. cachedKeyProperties [n] = p;
  1145. }
  1146. }
  1147. OrderedDictionary dic = new OrderedDictionary ();
  1148. foreach (PropertyDescriptor p in cachedKeyProperties)
  1149. dic [p.Name] = p.GetValue (row.DataItem);
  1150. return dic;
  1151. }
  1152. IOrderedDictionary GetRowValues (GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
  1153. {
  1154. OrderedDictionary dic = new OrderedDictionary ();
  1155. ExtractRowValues (dic, row, includeReadOnlyFields, includePrimaryKey);
  1156. return dic;
  1157. }
  1158. protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
  1159. {
  1160. foreach (TableCell cell in row.Cells) {
  1161. DataControlFieldCell c = cell as DataControlFieldCell;
  1162. if (c != null)
  1163. c.ContainingField.ExtractValuesFromCell (fieldValues, c, row.RowState, includeReadOnlyFields);
  1164. }
  1165. if (!includePrimaryKey && DataKeyNames != null)
  1166. foreach (string key in DataKeyNames)
  1167. fieldValues.Remove (key);
  1168. }
  1169. protected override HtmlTextWriterTag TagKey {
  1170. get {
  1171. if (EnableSortingAndPagingCallbacks)
  1172. return HtmlTextWriterTag.Div;
  1173. else
  1174. return HtmlTextWriterTag.Table;
  1175. }
  1176. }
  1177. public sealed override void DataBind ()
  1178. {
  1179. DataSourceView view = GetData ();
  1180. if (AllowPaging && view.CanPage) {
  1181. SelectArguments.StartRowIndex = PageIndex * PageSize;
  1182. SelectArguments.MaximumRows = PageSize;
  1183. if (view.CanRetrieveTotalRowCount)
  1184. SelectArguments.RetrieveTotalRowCount = true;
  1185. }
  1186. if (sortExpression != "") {
  1187. if (sortDirection == SortDirection.Ascending)
  1188. SelectArguments.SortExpression = sortExpression;
  1189. else
  1190. SelectArguments.SortExpression = sortExpression + " DESC";
  1191. }
  1192. cachedKeyProperties = null;
  1193. base.DataBind ();
  1194. }
  1195. protected internal override void PerformDataBinding (IEnumerable data)
  1196. {
  1197. base.PerformDataBinding (data);
  1198. }
  1199. [MonoTODO]
  1200. protected internal virtual void PrepareControlHierarchy ()
  1201. {
  1202. throw new NotImplementedException ();
  1203. }
  1204. protected internal override void OnInit (EventArgs e)
  1205. {
  1206. Page.RegisterRequiresControlState (this);
  1207. base.OnInit (e);
  1208. }
  1209. void OnFieldsChanged (object sender, EventArgs args)
  1210. {
  1211. RequireBinding ();
  1212. }
  1213. protected override void OnDataPropertyChanged ()
  1214. {
  1215. base.OnDataPropertyChanged ();
  1216. RequireBinding ();
  1217. }
  1218. protected override void OnDataSourceViewChanged (object sender, EventArgs e)
  1219. {
  1220. base.OnDataSourceViewChanged (sender, e);
  1221. RequireBinding ();
  1222. }
  1223. protected override bool OnBubbleEvent (object source, EventArgs e)
  1224. {
  1225. GridViewCommandEventArgs args = e as GridViewCommandEventArgs;
  1226. if (args != null) {
  1227. OnRowCommand (args);
  1228. string param = args.CommandArgument as string;
  1229. if (param == null || param.Length == 0) {
  1230. GridViewRow row = args.Row;
  1231. if (row != null)
  1232. param = row.RowIndex.ToString();
  1233. }
  1234. ProcessEvent (args.CommandName, param);
  1235. }
  1236. return base.OnBubbleEvent (source, e);
  1237. }
  1238. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  1239. {
  1240. RaisePostBackEvent (eventArgument);
  1241. }
  1242. // This is prolly obsolete
  1243. protected virtual void RaisePostBackEvent (string eventArgument)
  1244. {
  1245. int i = eventArgument.IndexOf ('$');
  1246. if (i != -1)
  1247. ProcessEvent (eventArgument.Substring (0, i), eventArgument.Substring (i + 1));
  1248. else
  1249. ProcessEvent (eventArgument, null);
  1250. }
  1251. void ProcessEvent (string eventName, string param)
  1252. {
  1253. switch (eventName)
  1254. {
  1255. case DataControlCommands.PageCommandName:
  1256. int newIndex = -1;
  1257. switch (param) {
  1258. case DataControlCommands.FirstPageCommandArgument:
  1259. newIndex = 0;
  1260. break;
  1261. case DataControlCommands.LastPageCommandArgument:
  1262. newIndex = PageCount - 1;
  1263. break;
  1264. case DataControlCommands.NextPageCommandArgument:
  1265. if (PageIndex < PageCount - 1) newIndex = PageIndex + 1;
  1266. break;
  1267. case DataControlCommands.PreviousPageCommandArgument:
  1268. if (PageIndex > 0) newIndex = PageIndex - 1;
  1269. break;
  1270. default:
  1271. newIndex = int.Parse (param) - 1;
  1272. break;
  1273. }
  1274. ShowPage (newIndex);
  1275. break;
  1276. case DataControlCommands.FirstPageCommandArgument:
  1277. ShowPage (0);
  1278. break;
  1279. case DataControlCommands.LastPageCommandArgument:
  1280. ShowPage (PageCount - 1);
  1281. break;
  1282. case DataControlCommands.NextPageCommandArgument:
  1283. if (PageIndex < PageCount - 1)
  1284. ShowPage (PageIndex + 1);
  1285. break;
  1286. case DataControlCommands.PreviousPageCommandArgument:
  1287. if (PageIndex > 0)
  1288. ShowPage (PageIndex - 1);
  1289. break;
  1290. case DataControlCommands.SelectCommandName:
  1291. SelectRow (int.Parse (param));
  1292. break;
  1293. case DataControlCommands.EditCommandName:
  1294. EditRow (int.Parse (param));
  1295. break;
  1296. case DataControlCommands.UpdateCommandName:
  1297. UpdateRow (EditIndex, true);
  1298. break;
  1299. case DataControlCommands.CancelCommandName:
  1300. CancelEdit ();
  1301. break;
  1302. case DataControlCommands.DeleteCommandName:
  1303. DeleteRow (int.Parse (param));
  1304. break;
  1305. case DataControlCommands.SortCommandName:
  1306. Sort (param);
  1307. break;
  1308. }
  1309. }
  1310. void Sort (string newSortExpression)
  1311. {
  1312. SortDirection newDirection;
  1313. if (sortExpression == newSortExpression) {
  1314. if (sortDirection == SortDirection.Ascending)
  1315. newDirection = SortDirection.Descending;
  1316. else
  1317. newDirection = SortDirection.Ascending;
  1318. } else
  1319. newDirection = sortDirection;
  1320. Sort (newSortExpression, newDirection);
  1321. }
  1322. public virtual void Sort (string newSortExpression, SortDirection newSortDirection)
  1323. {
  1324. GridViewSortEventArgs args = new GridViewSortEventArgs (newSortExpression, newSortDirection);
  1325. OnSorting (args);
  1326. if (args.Cancel) return;
  1327. sortExpression = args.SortExpression;
  1328. sortDirection = args.SortDirection;
  1329. RequireBinding ();
  1330. OnSorted (EventArgs.Empty);
  1331. }
  1332. void SelectRow (int index)
  1333. {
  1334. GridViewSelectEventArgs args = new GridViewSelectEventArgs (index);
  1335. OnSelectedIndexChanging (args);
  1336. if (!args.Cancel) {
  1337. SelectedIndex = args.NewSelectedIndex;
  1338. OnSelectedIndexChanged (EventArgs.Empty);
  1339. }
  1340. }
  1341. void ShowPage (int newIndex)
  1342. {
  1343. GridViewPageEventArgs args = new GridViewPageEventArgs (newIndex);
  1344. OnPageIndexChanging (args);
  1345. if (!args.Cancel) {
  1346. EndRowEdit ();
  1347. PageIndex = args.NewPageIndex;
  1348. OnPageIndexChanged (EventArgs.Empty);
  1349. }
  1350. }
  1351. void EditRow (int index)
  1352. {
  1353. GridViewEditEventArgs args = new GridViewEditEventArgs (index);
  1354. OnRowEditing (args);
  1355. if (!args.Cancel) {
  1356. EditIndex = args.NewEditIndex;
  1357. }
  1358. }
  1359. void CancelEdit ()
  1360. {
  1361. GridViewCancelEditEventArgs args = new GridViewCancelEditEventArgs (EditIndex);
  1362. OnRowCancelingEdit (args);
  1363. if (!args.Cancel) {
  1364. EndRowEdit ();
  1365. }
  1366. }
  1367. [MonoTODO ("Support two-way binding expressions")]
  1368. public virtual void UpdateRow (int rowIndex, bool causesValidation)
  1369. {
  1370. if (causesValidation)
  1371. Page.Validate ();
  1372. if (rowIndex != EditIndex) throw new NotSupportedException ();
  1373. currentEditOldValues = oldEditValues.Values;
  1374. GridViewRow row = Rows [rowIndex];
  1375. currentEditRowKeys = DataKeys [rowIndex].Values;
  1376. currentEditNewValues = GetRowValues (row, false, false);
  1377. GridViewUpdateEventArgs args = new GridViewUpdateEventArgs (EditIndex, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
  1378. OnRowUpdating (args);
  1379. if (!args.Cancel) {
  1380. DataSourceView view = GetData ();
  1381. if (view == null) throw new HttpException ("The DataSourceView associated to data bound control was null");
  1382. view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
  1383. } else
  1384. EndRowEdit ();
  1385. }
  1386. bool UpdateCallback (int recordsAffected, Exception exception)
  1387. {
  1388. GridViewUpdatedEventArgs dargs = new GridViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
  1389. OnRowUpdated (dargs);
  1390. if (!dargs.KeepInEditMode)
  1391. EndRowEdit ();
  1392. return dargs.ExceptionHandled;
  1393. }
  1394. public virtual void DeleteRow (int rowIndex)
  1395. {
  1396. GridViewRow row = Rows [rowIndex];
  1397. currentEditRowKeys = DataKeys [rowIndex].Values;
  1398. currentEditNewValues = GetRowValues (row, true, true);
  1399. GridViewDeleteEventArgs args = new GridViewDeleteEventArgs (rowIndex, currentEditRowKeys, currentEditNewValues);
  1400. OnRowDeleting (args);
  1401. if (!args.Cancel) {
  1402. RequireBinding ();
  1403. DataSourceView view = GetData ();
  1404. if (view != null)
  1405. view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
  1406. else {
  1407. GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
  1408. OnRowDeleted (dargs);
  1409. }
  1410. }
  1411. }
  1412. bool DeleteCallback (int recordsAffected, Exception exception)
  1413. {
  1414. GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
  1415. OnRowDeleted (dargs);
  1416. return dargs.ExceptionHandled;
  1417. }
  1418. void EndRowEdit ()
  1419. {
  1420. EditIndex = -1;
  1421. oldEditValues = new DataKey (new OrderedDictionary ());
  1422. currentEditRowKeys = null;
  1423. currentEditOldValues = null;
  1424. currentEditNewValues = null;
  1425. }
  1426. protected internal override void LoadControlState (object ob)
  1427. {
  1428. if (ob == null) return;
  1429. object[] state = (object[]) ob;
  1430. base.LoadControlState (state[0]);
  1431. pageIndex = (int) state[1];
  1432. pageCount = (int) state[2];
  1433. selectedIndex = (int) state[3];
  1434. editIndex = (int) state[4];
  1435. sortExpression = (string) state[5];
  1436. sortDirection = (SortDirection) state[6];
  1437. DataKeyNames = (string []) state [7];
  1438. }
  1439. protected internal override object SaveControlState ()
  1440. {
  1441. object bstate = base.SaveControlState ();
  1442. return new object[] {
  1443. bstate, pageIndex, pageCount, selectedIndex, editIndex, sortExpression, sortDirection, DataKeyNames
  1444. };
  1445. }
  1446. protected override void TrackViewState()
  1447. {
  1448. base.TrackViewState();
  1449. if (columns != null) ((IStateManager)columns).TrackViewState();
  1450. if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
  1451. if (alternatingRowStyle != null) ((IStateManager)alternatingRowStyle).TrackViewState();
  1452. if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
  1453. if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
  1454. if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
  1455. if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
  1456. if (selectedRowStyle != null) ((IStateManager)selectedRowStyle).TrackViewState();
  1457. if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
  1458. if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
  1459. if (keys != null) ((IStateManager)keys).TrackViewState();
  1460. if (autoFieldProperties != null) {
  1461. foreach (IStateManager sm in autoFieldProperties)
  1462. sm.TrackViewState ();
  1463. }
  1464. }
  1465. protected override object SaveViewState()
  1466. {
  1467. object[] states = new object [14];
  1468. states[0] = base.SaveViewState();
  1469. states[1] = (columns == null ? null : ((IStateManager)columns).SaveViewState());
  1470. states[2] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
  1471. states[3] = (alternatingRowStyle == null ? null : ((IStateManager)alternatingRowStyle).SaveViewState());
  1472. states[4] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
  1473. states[5] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
  1474. states[6] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
  1475. states[7] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
  1476. states[8] = (selectedRowStyle == null ? null : ((IStateManager)selectedRowStyle).SaveViewState());
  1477. states[9] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
  1478. states[10] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
  1479. states[11] = (keys == null ? null : ((IStateManager)keys).SaveViewState());
  1480. states[12] = (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState());
  1481. if (autoFieldProperties != null) {
  1482. object[] data = new object [autoFieldProperties.Length];
  1483. bool allNull = true;
  1484. for (int n=0; n<data.Length; n++) {
  1485. data [n] = ((IStateManager)autoFieldProperties [n]).SaveViewState ();
  1486. if (data [n] != null) allNull = false;
  1487. }
  1488. if (!allNull) states [13] = data;
  1489. }
  1490. for (int i = states.Length - 1; i >= 0; i--) {
  1491. if (states [i] != null)
  1492. return states;
  1493. }
  1494. return null;
  1495. }
  1496. protected override void LoadViewState (object savedState)
  1497. {
  1498. if (savedState == null) {
  1499. base.LoadViewState (null);
  1500. return;
  1501. }
  1502. object [] states = (object []) savedState;
  1503. if (states[13] != null) {
  1504. object[] data = (object[]) states [13];
  1505. autoFieldProperties = new AutoGeneratedFieldProperties [data.Length];
  1506. for (int n=0; n<data.Length; n++) {
  1507. IStateManager p = new AutoGeneratedFieldProperties ();
  1508. p.TrackViewState ();
  1509. p.LoadViewState (data [n]);
  1510. autoFieldProperties [n] = (AutoGeneratedFieldProperties) p;
  1511. }
  1512. }
  1513. base.LoadViewState (states[0]);
  1514. EnsureChildControls ();
  1515. if (states[1] != null) ((IStateManager)Columns).LoadViewState (states[1]);
  1516. if (states[2] != null) ((IStateManager)PagerSettings).LoadViewState (states[2]);
  1517. if (states[3] != null) ((IStateManager)AlternatingRowStyle).LoadViewState (states[3]);
  1518. if (states[4] != null) ((IStateManager)FooterStyle).LoadViewState (states[4]);
  1519. if (states[5] != null) ((IStateManager)HeaderStyle).LoadViewState (states[5]);
  1520. if (states[6] != null) ((IStateManager)PagerStyle).LoadViewState (states[6]);
  1521. if (states[7] != null) ((IStateManager)RowStyle).LoadViewState (states[7]);
  1522. if (states[8] != null) ((IStateManager)SelectedRowStyle).LoadViewState (states[8]);
  1523. if (states[9] != null) ((IStateManager)EditRowStyle).LoadViewState (states[9]);
  1524. if (states[10] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
  1525. if (states[11] != null) ((IStateManager)DataKeys).LoadViewState (states[11]);
  1526. if (states[12] != null && oldEditValues != null) ((IStateManager)oldEditValues).LoadViewState (states[12]);
  1527. }
  1528. void ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
  1529. {
  1530. RaiseCallbackEvent (eventArgs);
  1531. }
  1532. string callbackResult;
  1533. protected virtual void RaiseCallbackEvent (string eventArgs)
  1534. {
  1535. string[] clientData = eventArgs.Split ('|');
  1536. pageIndex = int.Parse (clientData[0]);
  1537. sortExpression = HttpUtility.UrlDecode (clientData[1]);
  1538. if (sortExpression == "") sortExpression = null;
  1539. RequireBinding ();
  1540. RaisePostBackEvent (clientData[2]);
  1541. EnsureDataBound ();
  1542. StringWriter sw = new StringWriter ();
  1543. sw.Write (PageIndex.ToString() + '|' + SortExpression + '|');
  1544. HtmlTextWriter writer = new HtmlTextWriter (sw);
  1545. RenderGrid (writer);
  1546. callbackResult = sw.ToString ();
  1547. }
  1548. string ICallbackEventHandler.GetCallbackResult ()
  1549. {
  1550. return GetCallbackResult ();
  1551. }
  1552. protected virtual string GetCallbackResult ()
  1553. {
  1554. return callbackResult;
  1555. }
  1556. string ICallbackContainer.GetCallbackScript (IButtonControl control, string argument)
  1557. {
  1558. return GetCallbackScript (control, argument);
  1559. }
  1560. protected virtual string GetCallbackScript (IButtonControl control, string argument)
  1561. {
  1562. if (EnableSortingAndPagingCallbacks)
  1563. return "javascript:GridView_ClientEvent (\"" + ClientID + "\",\"" + control.CommandName + "$" + control.CommandArgument + "\"); return false;";
  1564. else
  1565. return null;
  1566. }
  1567. [MonoTODO]
  1568. protected override void OnPagePreLoad (object sender, EventArgs e)
  1569. {
  1570. base.OnPagePreLoad (sender, e);
  1571. }
  1572. protected internal override void OnPreRender (EventArgs e)
  1573. {
  1574. base.OnPreRender (e);
  1575. if (EnableSortingAndPagingCallbacks)
  1576. {
  1577. if (!Page.ClientScript.IsClientScriptIncludeRegistered (typeof(GridView), "GridView.js")) {
  1578. string url = Page.ClientScript.GetWebResourceUrl (typeof(GridView), "GridView.js");
  1579. Page.ClientScript.RegisterClientScriptInclude (typeof(GridView), "GridView.js", url);
  1580. }
  1581. string cgrid = ClientID + "_data";
  1582. string script = string.Format ("var {0} = new Object ();\n", cgrid);
  1583. script += string.Format ("{0}.pageIndex = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (PageIndex));
  1584. script += string.Format ("{0}.sortExp = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (SortExpression == null ? "" : SortExpression));
  1585. script += string.Format ("{0}.uid = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (UniqueID));
  1586. Page.ClientScript.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
  1587. // Make sure the basic script infrastructure is rendered
  1588. Page.ClientScript.GetCallbackEventReference (this, "null", "", "null");
  1589. Page.ClientScript.GetPostBackClientHyperlink (this, "");
  1590. }
  1591. }
  1592. protected internal override void Render (HtmlTextWriter writer)
  1593. {
  1594. if (EnableSortingAndPagingCallbacks)
  1595. writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
  1596. writer.RenderBeginTag (HtmlTextWriterTag.Div);
  1597. RenderGrid (writer);
  1598. writer.RenderEndTag ();
  1599. }
  1600. void RenderGrid (HtmlTextWriter writer)
  1601. {
  1602. if (table == null)
  1603. return;
  1604. table.Caption = Caption;
  1605. table.CaptionAlign = CaptionAlign;
  1606. table.RenderBeginTag (writer);
  1607. foreach (GridViewRow row in table.Rows)
  1608. {
  1609. switch (row.RowType) {
  1610. case DataControlRowType.Header:
  1611. if (!ShowHeader) continue;
  1612. if (headerStyle != null)headerStyle.AddAttributesToRender (writer, row);
  1613. break;
  1614. case DataControlRowType.Footer:
  1615. if (!ShowFooter) continue;
  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. PostBackOptions pbo = new PostBackOptions (this, control.CommandName + "$" + control.CommandArgument);
  1658. return pbo;
  1659. }
  1660. }
  1661. }
  1662. #endif