DetailsView.cs 54 KB

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