DetailsView.cs 58 KB

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