DetailsView.cs 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963
  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. EnsureChildControls ();
  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. private set {
  731. pageCount = value;
  732. }
  733. }
  734. [WebCategoryAttribute ("Paging")]
  735. [BindableAttribute (true, BindingDirection.OneWay)]
  736. [DefaultValueAttribute (0)]
  737. public virtual int PageIndex {
  738. get {
  739. if (CurrentMode == DetailsViewMode.Insert)
  740. return -1;
  741. return pageIndex;
  742. }
  743. set {
  744. if (value < -1)
  745. throw new ArgumentOutOfRangeException ("PageIndex must be non-negative");
  746. if (pageIndex == value || value == -1)
  747. return;
  748. pageIndex = value;
  749. RequireBinding ();
  750. }
  751. }
  752. [WebCategoryAttribute ("Paging")]
  753. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  754. [NotifyParentPropertyAttribute (true)]
  755. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  756. public virtual PagerSettings PagerSettings {
  757. get {
  758. if (pagerSettings == null) {
  759. pagerSettings = new PagerSettings (this);
  760. if (IsTrackingViewState)
  761. ((IStateManager)pagerSettings).TrackViewState ();
  762. }
  763. return pagerSettings;
  764. }
  765. }
  766. [WebCategoryAttribute ("Styles")]
  767. [PersistenceMode (PersistenceMode.InnerProperty)]
  768. [NotifyParentProperty (true)]
  769. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  770. public TableItemStyle PagerStyle {
  771. get {
  772. if (pagerStyle == null) {
  773. pagerStyle = new TableItemStyle ();
  774. if (IsTrackingViewState)
  775. pagerStyle.TrackViewState();
  776. }
  777. return pagerStyle;
  778. }
  779. }
  780. [DefaultValue (null)]
  781. [TemplateContainer (typeof (DetailsView), BindingDirection.OneWay)]
  782. [PersistenceMode (PersistenceMode.InnerProperty)]
  783. [Browsable (false)]
  784. public virtual ITemplate PagerTemplate {
  785. get { return pagerTemplate; }
  786. set { pagerTemplate = value; RequireBinding (); }
  787. }
  788. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  789. [BrowsableAttribute (false)]
  790. public virtual DetailsViewRowCollection Rows {
  791. get {
  792. EnsureChildControls ();
  793. return rows;
  794. }
  795. }
  796. [WebCategoryAttribute ("Styles")]
  797. [PersistenceMode (PersistenceMode.InnerProperty)]
  798. [NotifyParentProperty (true)]
  799. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  800. [DefaultValue (null)]
  801. public TableItemStyle RowStyle {
  802. get {
  803. if (rowStyle == null) {
  804. rowStyle = new TableItemStyle ();
  805. if (IsTrackingViewState)
  806. rowStyle.TrackViewState();
  807. }
  808. return rowStyle;
  809. }
  810. }
  811. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  812. [BrowsableAttribute (false)]
  813. public object SelectedValue {
  814. get { return DataKey.Value; }
  815. }
  816. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  817. [BrowsableAttribute (false)]
  818. public virtual DetailsViewRow TopPagerRow {
  819. get {
  820. EnsureChildControls ();
  821. return topPagerRow;
  822. }
  823. }
  824. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  825. [BrowsableAttribute (false)]
  826. public virtual object DataItem {
  827. get {
  828. return dataItem;
  829. }
  830. }
  831. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  832. [BrowsableAttribute (false)]
  833. public int DataItemCount {
  834. get { return PageCount; }
  835. }
  836. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  837. [BrowsableAttribute (false)]
  838. public virtual int DataItemIndex {
  839. get { return PageIndex; }
  840. }
  841. int IDataItemContainer.DisplayIndex {
  842. get { return PageIndex; }
  843. }
  844. int IDataItemContainer.DataItemIndex {
  845. get { return DataItemIndex; }
  846. }
  847. public virtual bool IsBindableType (Type type)
  848. {
  849. return type.IsPrimitive || type == typeof (string) || type == typeof (DateTime) || type == typeof (Guid) || type == typeof (Decimal);
  850. }
  851. protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
  852. {
  853. DataSourceSelectArguments arg = new DataSourceSelectArguments ();
  854. DataSourceView view = GetData ();
  855. if (AllowPaging && view.CanPage) {
  856. arg.StartRowIndex = PageIndex;
  857. if (view.CanRetrieveTotalRowCount) {
  858. arg.RetrieveTotalRowCount = true;
  859. arg.MaximumRows = 1;
  860. }
  861. else {
  862. arg.MaximumRows = -1;
  863. }
  864. }
  865. return arg;
  866. }
  867. protected virtual ICollection CreateFieldSet (object dataItem, bool useDataSource)
  868. {
  869. ArrayList fields = new ArrayList ();
  870. if (AutoGenerateRows) {
  871. if (useDataSource) {
  872. if (dataItem != null)
  873. fields.AddRange (CreateAutoGeneratedRows (dataItem));
  874. } else {
  875. if (autoFieldProperties != null) {
  876. foreach (AutoGeneratedFieldProperties props in autoFieldProperties)
  877. fields.Add (CreateAutoGeneratedRow (props));
  878. }
  879. }
  880. }
  881. fields.AddRange (Fields);
  882. if (AutoGenerateEditButton || AutoGenerateDeleteButton || AutoGenerateInsertButton) {
  883. CommandField field = new CommandField ();
  884. field.ShowEditButton = AutoGenerateEditButton;
  885. field.ShowDeleteButton = AutoGenerateDeleteButton;
  886. field.ShowInsertButton = AutoGenerateInsertButton;
  887. fields.Add (field);
  888. }
  889. return fields;
  890. }
  891. protected virtual ICollection CreateAutoGeneratedRows (object dataItem)
  892. {
  893. if (dataItem == null)
  894. return null;
  895. ArrayList list = new ArrayList ();
  896. autoFieldProperties = CreateAutoFieldProperties (dataItem);
  897. foreach (AutoGeneratedFieldProperties props in autoFieldProperties)
  898. list.Add (CreateAutoGeneratedRow (props));
  899. return list;
  900. }
  901. protected virtual AutoGeneratedField CreateAutoGeneratedRow (AutoGeneratedFieldProperties fieldProperties)
  902. {
  903. return new AutoGeneratedField (fieldProperties);
  904. }
  905. AutoGeneratedFieldProperties[] CreateAutoFieldProperties (object dataItem)
  906. {
  907. if (IsBindableType (dataItem.GetType ()))
  908. {
  909. AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
  910. ((IStateManager) field).TrackViewState ();
  911. field.Name = "Item";
  912. field.DataField = BoundField.ThisExpression;
  913. field.Type = dataItem.GetType ();
  914. return new AutoGeneratedFieldProperties [] { field };
  915. }
  916. PropertyDescriptorCollection props = TypeDescriptor.GetProperties (dataItem, false);
  917. if (props != null && props.Count > 0) {
  918. ArrayList retVal = new ArrayList ();
  919. foreach (PropertyDescriptor current in props) {
  920. if (IsBindableType (current.PropertyType)) {
  921. AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
  922. ((IStateManager) field).TrackViewState ();
  923. field.Name = current.Name;
  924. field.DataField = current.Name;
  925. for (int i = 0; i < DataKeyNames.Length; i++) {
  926. if (string.Compare (DataKeyNames [i], current.Name, StringComparison.InvariantCultureIgnoreCase) == 0) {
  927. field.IsReadOnly = true;
  928. break;
  929. }
  930. }
  931. field.Type = current.PropertyType;
  932. retVal.Add (field);
  933. }
  934. }
  935. if (retVal.Count > 0)
  936. return (AutoGeneratedFieldProperties []) retVal.ToArray (typeof (AutoGeneratedFieldProperties));
  937. }
  938. 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));
  939. }
  940. protected virtual DetailsViewRow CreateRow (int rowIndex, DataControlRowType rowType, DataControlRowState rowState)
  941. {
  942. DetailsViewRow row;
  943. if (rowType == DataControlRowType.Pager)
  944. row = new DetailsViewPagerRow (rowIndex, rowType, rowState);
  945. else
  946. row = new DetailsViewRow (rowIndex, rowType, rowState);
  947. return row;
  948. }
  949. void RequireBinding ()
  950. {
  951. if (Initialized) {
  952. RequiresDataBinding = true;
  953. }
  954. }
  955. protected virtual Table CreateTable ()
  956. {
  957. return new ContainedTable (this);
  958. }
  959. protected override Style CreateControlStyle ()
  960. {
  961. TableStyle style = new TableStyle ();
  962. style.GridLines = GridLines.Both;
  963. style.CellSpacing = 0;
  964. return style;
  965. }
  966. protected override int CreateChildControls (IEnumerable data, bool dataBinding)
  967. {
  968. PagedDataSource dataSource = new PagedDataSource ();
  969. dataSource.DataSource = CurrentMode != DetailsViewMode.Insert ? data : null;
  970. dataSource.AllowPaging = AllowPaging;
  971. dataSource.PageSize = 1;
  972. dataSource.CurrentPageIndex = PageIndex;
  973. if (dataBinding && CurrentMode != DetailsViewMode.Insert) {
  974. DataSourceView view = GetData ();
  975. if (view != null && view.CanPage) {
  976. dataSource.AllowServerPaging = true;
  977. if (SelectArguments.RetrieveTotalRowCount)
  978. dataSource.VirtualCount = SelectArguments.TotalRowCount;
  979. }
  980. }
  981. bool showPager = AllowPaging && (dataSource.PageCount > 1);
  982. Controls.Clear ();
  983. table = CreateTable ();
  984. Controls.Add (table);
  985. headerRow = null;
  986. footerRow = null;
  987. topPagerRow = null;
  988. bottomPagerRow = null;
  989. ArrayList list = new ArrayList ();
  990. // Gets the current data item
  991. if (AllowPaging) {
  992. PageCount = dataSource.DataSourceCount;
  993. if (PageIndex >= PageCount && PageCount > 0) {
  994. pageIndex = dataSource.CurrentPageIndex = PageCount - 1;
  995. }
  996. if (dataSource.DataSource != null) {
  997. IEnumerator e = dataSource.GetEnumerator ();
  998. if (e.MoveNext ())
  999. dataItem = e.Current;
  1000. }
  1001. }
  1002. else {
  1003. int page = 0;
  1004. object lastItem = null;
  1005. if (dataSource.DataSource != null) {
  1006. IEnumerator e = dataSource.GetEnumerator ();
  1007. for (; e.MoveNext (); page++) {
  1008. lastItem = e.Current;
  1009. if (page == PageIndex)
  1010. dataItem = e.Current;
  1011. }
  1012. }
  1013. PageCount = page;
  1014. if (PageIndex >= PageCount && PageCount > 0) {
  1015. pageIndex = PageCount - 1;
  1016. dataItem = lastItem;
  1017. }
  1018. }
  1019. if (PageCount == 0 && CurrentMode != DetailsViewMode.Insert) {
  1020. DetailsViewRow row = CreateEmptyRow ();
  1021. if (row != null) {
  1022. table.Rows.Add (row);
  1023. list.Add (row);
  1024. }
  1025. }
  1026. else {
  1027. // Creates the set of fields to show
  1028. ICollection fieldCollection = CreateFieldSet (dataItem, dataBinding && dataItem != null);
  1029. DataControlField [] fields = new DataControlField [fieldCollection.Count];
  1030. fieldCollection.CopyTo (fields, 0);
  1031. foreach (DataControlField field in fields) {
  1032. field.Initialize (false, this);
  1033. if (EnablePagingCallbacks)
  1034. field.ValidateSupportsCallback ();
  1035. }
  1036. // Main table creation
  1037. headerRow = CreateRow (-1, DataControlRowType.Header, DataControlRowState.Normal);
  1038. DataControlFieldCell headerCell = new DataControlFieldCell (null);
  1039. headerCell.ColumnSpan = 2;
  1040. if (headerTemplate != null)
  1041. headerTemplate.InstantiateIn (headerCell);
  1042. else if (!String.IsNullOrEmpty (HeaderText))
  1043. headerCell.Text = HeaderText;
  1044. else
  1045. headerRow.Visible = false;
  1046. headerRow.Cells.Add (headerCell);
  1047. table.Rows.Add (headerRow);
  1048. if (showPager && PagerSettings.Position == PagerPosition.Top ||
  1049. PagerSettings.Position == PagerPosition.TopAndBottom) {
  1050. topPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal);
  1051. InitializePager (topPagerRow, dataSource);
  1052. table.Rows.Add (topPagerRow);
  1053. }
  1054. foreach (DataControlField field in fields) {
  1055. DataControlRowState rstate = GetRowState (list.Count);
  1056. DetailsViewRow row = CreateRow (PageIndex, DataControlRowType.DataRow, rstate);
  1057. InitializeRow (row, field);
  1058. table.Rows.Add (row);
  1059. list.Add (row);
  1060. }
  1061. footerRow = CreateRow (-1, DataControlRowType.Footer, DataControlRowState.Normal);
  1062. DataControlFieldCell footerCell = new DataControlFieldCell (null);
  1063. footerCell.ColumnSpan = 2;
  1064. if (footerTemplate != null)
  1065. footerTemplate.InstantiateIn (footerCell);
  1066. else if (!String.IsNullOrEmpty (FooterText))
  1067. footerCell.Text = FooterText;
  1068. else
  1069. footerRow.Visible = false;
  1070. footerRow.Cells.Add (footerCell);
  1071. table.Rows.Add (footerRow);
  1072. if (showPager && PagerSettings.Position == PagerPosition.Bottom ||
  1073. PagerSettings.Position == PagerPosition.TopAndBottom) {
  1074. bottomPagerRow = CreateRow (-1, DataControlRowType.Pager, DataControlRowState.Normal);
  1075. InitializePager (bottomPagerRow, dataSource);
  1076. table.Rows.Add (bottomPagerRow);
  1077. }
  1078. }
  1079. rows = new DetailsViewRowCollection (list);
  1080. if (dataBinding)
  1081. DataBind (false);
  1082. OnItemCreated (EventArgs.Empty);
  1083. return PageCount;
  1084. }
  1085. protected override void EnsureDataBound ()
  1086. {
  1087. if (CurrentMode == DetailsViewMode.Insert) {
  1088. if (RequiresDataBinding) {
  1089. OnDataBinding (EventArgs.Empty);
  1090. RequiresDataBinding = false;
  1091. PerformDataBinding (null);
  1092. MarkAsDataBound ();
  1093. OnDataBound (EventArgs.Empty);
  1094. }
  1095. }
  1096. else
  1097. base.EnsureDataBound ();
  1098. }
  1099. DataControlRowState GetRowState (int index)
  1100. {
  1101. DataControlRowState rstate = (index % 2) == 0 ? DataControlRowState.Normal : DataControlRowState.Alternate;
  1102. if (CurrentMode == DetailsViewMode.Edit) rstate |= DataControlRowState.Edit;
  1103. else if (CurrentMode == DetailsViewMode.Insert) rstate |= DataControlRowState.Insert;
  1104. return rstate;
  1105. }
  1106. protected virtual void InitializePager (DetailsViewRow row, PagedDataSource dataSource)
  1107. {
  1108. TableCell cell = new TableCell ();
  1109. cell.ColumnSpan = 2;
  1110. if (pagerTemplate != null)
  1111. pagerTemplate.InstantiateIn (cell);
  1112. else
  1113. cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
  1114. row.Cells.Add (cell);
  1115. }
  1116. DetailsViewRow CreateEmptyRow ()
  1117. {
  1118. TableCell cell = new TableCell ();
  1119. if (emptyDataTemplate != null)
  1120. emptyDataTemplate.InstantiateIn (cell);
  1121. else if (!String.IsNullOrEmpty (EmptyDataText))
  1122. cell.Text = EmptyDataText;
  1123. else
  1124. return null;
  1125. DetailsViewRow row = CreateRow (-1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
  1126. row.Cells.Add (cell);
  1127. return row;
  1128. }
  1129. protected virtual void InitializeRow (DetailsViewRow row, DataControlField field)
  1130. {
  1131. row.ContainingField = field;
  1132. DataControlFieldCell cell;
  1133. if (field.ShowHeader) {
  1134. cell = new DataControlFieldCell (field);
  1135. row.Cells.Add (cell);
  1136. field.InitializeCell (cell, DataControlCellType.Header, row.RowState, row.RowIndex);
  1137. }
  1138. cell = new DataControlFieldCell (field);
  1139. if (!field.ShowHeader)
  1140. cell.ColumnSpan = 2;
  1141. row.Cells.Add (cell);
  1142. field.InitializeCell (cell, DataControlCellType.DataCell, row.RowState, row.RowIndex);
  1143. if (CurrentMode == DetailsViewMode.Insert && !field.InsertVisible)
  1144. row.Visible = false;
  1145. }
  1146. void FillRowDataKey (object dataItem)
  1147. {
  1148. KeyTable.Clear ();
  1149. if (cachedKeyProperties == null) {
  1150. PropertyDescriptorCollection props = TypeDescriptor.GetProperties (dataItem);
  1151. cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
  1152. for (int n=0; n<DataKeyNames.Length; n++) {
  1153. PropertyDescriptor p = props.Find (DataKeyNames [n], true);
  1154. if (p == null)
  1155. throw new InvalidOperationException ("Property '" + DataKeyNames[n] + "' not found in object of type " + dataItem.GetType());
  1156. cachedKeyProperties [n] = p;
  1157. }
  1158. }
  1159. foreach (PropertyDescriptor p in cachedKeyProperties)
  1160. KeyTable [p.Name] = p.GetValue (dataItem);
  1161. }
  1162. IOrderedDictionary GetRowValues (bool includeReadOnlyFields, bool includePrimaryKey)
  1163. {
  1164. OrderedDictionary dic = new OrderedDictionary ();
  1165. ExtractRowValues (dic, includeReadOnlyFields, includePrimaryKey);
  1166. return dic;
  1167. }
  1168. protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, bool includeReadOnlyFields, bool includePrimaryKey)
  1169. {
  1170. foreach (DetailsViewRow row in Rows) {
  1171. if (row.Cells.Count < 1) continue;
  1172. DataControlFieldCell c = row.Cells[row.Cells.Count-1] as DataControlFieldCell;
  1173. if (c != null)
  1174. c.ContainingField.ExtractValuesFromCell (fieldValues, c, row.RowState, includeReadOnlyFields);
  1175. }
  1176. if (!includePrimaryKey && DataKeyNames != null)
  1177. foreach (string key in DataKeyNames)
  1178. fieldValues.Remove (key);
  1179. }
  1180. protected override HtmlTextWriterTag TagKey {
  1181. get {
  1182. if (EnablePagingCallbacks)
  1183. return HtmlTextWriterTag.Div;
  1184. else
  1185. return HtmlTextWriterTag.Table;
  1186. }
  1187. }
  1188. public sealed override void DataBind ()
  1189. {
  1190. cachedKeyProperties = null;
  1191. base.DataBind ();
  1192. if (dataItem != null) {
  1193. if (CurrentMode == DetailsViewMode.Edit)
  1194. oldEditValues = new DataKey (GetRowValues (false, true));
  1195. FillRowDataKey (dataItem);
  1196. key = new DataKey (KeyTable);
  1197. }
  1198. }
  1199. protected internal override void PerformDataBinding (IEnumerable data)
  1200. {
  1201. base.PerformDataBinding (data);
  1202. }
  1203. protected internal virtual void PrepareControlHierarchy ()
  1204. {
  1205. if (table == null)
  1206. return;
  1207. table.Caption = Caption;
  1208. table.CaptionAlign = CaptionAlign;
  1209. foreach (DetailsViewRow row in table.Rows) {
  1210. switch (row.RowType) {
  1211. case DataControlRowType.Header:
  1212. if (headerStyle != null && !headerStyle.IsEmpty)
  1213. row.ControlStyle.CopyFrom (headerStyle);
  1214. break;
  1215. case DataControlRowType.Footer:
  1216. if (footerStyle != null && !footerStyle.IsEmpty)
  1217. row.ControlStyle.CopyFrom (footerStyle);
  1218. break;
  1219. case DataControlRowType.Pager:
  1220. if (pagerStyle != null && !pagerStyle.IsEmpty)
  1221. row.ControlStyle.CopyFrom (pagerStyle);
  1222. break;
  1223. case DataControlRowType.EmptyDataRow:
  1224. if (emptyDataRowStyle != null && !emptyDataRowStyle.IsEmpty)
  1225. row.ControlStyle.CopyFrom (emptyDataRowStyle);
  1226. break;
  1227. case DataControlRowType.DataRow:
  1228. if (rowStyle != null && !rowStyle.IsEmpty)
  1229. row.ControlStyle.CopyFrom (rowStyle);
  1230. if ((row.RowState & DataControlRowState.Alternate) != 0 && alternatingRowStyle != null && !alternatingRowStyle.IsEmpty)
  1231. row.ControlStyle.CopyFrom (alternatingRowStyle);
  1232. break;
  1233. default:
  1234. break;
  1235. }
  1236. if (row.ContainingField is CommandField) {
  1237. if (commandRowStyle != null && !commandRowStyle.IsEmpty)
  1238. row.ControlStyle.CopyFrom (commandRowStyle);
  1239. }
  1240. else {
  1241. if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null && !editRowStyle.IsEmpty)
  1242. row.ControlStyle.CopyFrom (editRowStyle);
  1243. if ((row.RowState & DataControlRowState.Insert) != 0) {
  1244. if (insertRowStyle != null && !insertRowStyle.IsEmpty)
  1245. row.ControlStyle.CopyFrom (insertRowStyle);
  1246. else if (editRowStyle != null && !editRowStyle.IsEmpty)
  1247. row.ControlStyle.CopyFrom (editRowStyle);
  1248. }
  1249. }
  1250. for (int n = 0; n < row.Cells.Count; n++) {
  1251. DataControlFieldCell fcell = row.Cells [n] as DataControlFieldCell;
  1252. if (fcell != null && fcell.ContainingField != null) {
  1253. DataControlField field = fcell.ContainingField;
  1254. if (n == 0 && field.ShowHeader) {
  1255. if (fieldHeaderStyle != null && !fieldHeaderStyle.IsEmpty)
  1256. fcell.ControlStyle.CopyFrom (fieldHeaderStyle);
  1257. if (field.HeaderStyleCreated && !field.HeaderStyle.IsEmpty)
  1258. fcell.ControlStyle.CopyFrom (field.HeaderStyle);
  1259. }
  1260. else {
  1261. if (field.ControlStyleCreated && !field.ControlStyle.IsEmpty)
  1262. foreach (Control c in fcell.Controls) {
  1263. WebControl wc = c as WebControl;
  1264. if (wc != null)
  1265. wc.ControlStyle.MergeWith (field.ControlStyle);
  1266. }
  1267. if (field.ItemStyleCreated && !field.ItemStyle.IsEmpty)
  1268. fcell.ControlStyle.CopyFrom (field.ItemStyle);
  1269. }
  1270. }
  1271. }
  1272. }
  1273. }
  1274. protected internal override void OnInit (EventArgs e)
  1275. {
  1276. Page.RegisterRequiresControlState (this);
  1277. base.OnInit (e);
  1278. }
  1279. void OnFieldsChanged (object sender, EventArgs args)
  1280. {
  1281. RequireBinding ();
  1282. }
  1283. protected override void OnDataSourceViewChanged (object sender, EventArgs e)
  1284. {
  1285. base.OnDataSourceViewChanged (sender, e);
  1286. RequireBinding ();
  1287. }
  1288. protected override bool OnBubbleEvent (object source, EventArgs e)
  1289. {
  1290. DetailsViewCommandEventArgs args = e as DetailsViewCommandEventArgs;
  1291. if (args != null) {
  1292. bool causesValidation = false;
  1293. IButtonControl button = args.CommandSource as IButtonControl;
  1294. if (button != null && button.CausesValidation) {
  1295. Page.Validate (button.ValidationGroup);
  1296. causesValidation = true;
  1297. }
  1298. ProcessCommand (args, causesValidation);
  1299. return true;
  1300. }
  1301. return base.OnBubbleEvent (source, e);
  1302. }
  1303. void ProcessCommand (DetailsViewCommandEventArgs args, bool causesValidation) {
  1304. OnItemCommand (args);
  1305. ProcessEvent (args.CommandName, args.CommandArgument as string, causesValidation);
  1306. }
  1307. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  1308. {
  1309. RaisePostBackEvent (eventArgument);
  1310. }
  1311. // TODO: This is prolly obsolete
  1312. protected virtual void RaisePostBackEvent (string eventArgument)
  1313. {
  1314. int i = eventArgument.IndexOf ('$');
  1315. CommandEventArgs arg;
  1316. if (i != -1)
  1317. arg = new CommandEventArgs (eventArgument.Substring (0, i), eventArgument.Substring (i + 1));
  1318. else
  1319. arg = new CommandEventArgs (eventArgument, null);
  1320. ProcessCommand (new DetailsViewCommandEventArgs (this, arg), false);
  1321. }
  1322. void ProcessEvent (string eventName, string param, bool causesValidation)
  1323. {
  1324. switch (eventName)
  1325. {
  1326. case DataControlCommands.PageCommandName:
  1327. int newIndex = -1;
  1328. switch (param) {
  1329. case DataControlCommands.FirstPageCommandArgument:
  1330. newIndex = 0;
  1331. break;
  1332. case DataControlCommands.LastPageCommandArgument:
  1333. newIndex = PageCount - 1;
  1334. break;
  1335. case DataControlCommands.NextPageCommandArgument:
  1336. newIndex = PageIndex + 1;
  1337. break;
  1338. case DataControlCommands.PreviousPageCommandArgument:
  1339. newIndex = PageIndex - 1;
  1340. break;
  1341. default:
  1342. int paramIndex = 0;
  1343. int.TryParse (param, out paramIndex);
  1344. newIndex = paramIndex - 1;
  1345. break;
  1346. }
  1347. ShowPage (newIndex);
  1348. break;
  1349. case DataControlCommands.FirstPageCommandArgument:
  1350. ShowPage (0);
  1351. break;
  1352. case DataControlCommands.LastPageCommandArgument:
  1353. ShowPage (PageCount - 1);
  1354. break;
  1355. case DataControlCommands.NextPageCommandArgument:
  1356. if (PageIndex < PageCount - 1)
  1357. ShowPage (PageIndex + 1);
  1358. break;
  1359. case DataControlCommands.PreviousPageCommandArgument:
  1360. if (PageIndex > 0)
  1361. ShowPage (PageIndex - 1);
  1362. break;
  1363. case DataControlCommands.EditCommandName:
  1364. ProcessChangeMode (DetailsViewMode.Edit);
  1365. break;
  1366. case DataControlCommands.NewCommandName:
  1367. ProcessChangeMode (DetailsViewMode.Insert);
  1368. break;
  1369. case DataControlCommands.UpdateCommandName:
  1370. UpdateItem (param, causesValidation);
  1371. break;
  1372. case DataControlCommands.CancelCommandName:
  1373. CancelEdit ();
  1374. break;
  1375. case DataControlCommands.DeleteCommandName:
  1376. DeleteItem ();
  1377. break;
  1378. case DataControlCommands.InsertCommandName:
  1379. InsertItem (causesValidation);
  1380. break;
  1381. }
  1382. }
  1383. void ShowPage (int newIndex)
  1384. {
  1385. DetailsViewPageEventArgs args = new DetailsViewPageEventArgs (newIndex);
  1386. OnPageIndexChanging (args);
  1387. if (args.Cancel || !IsBoundUsingDataSourceID)
  1388. return;
  1389. if (args.NewPageIndex < 0 || args.NewPageIndex >= PageCount)
  1390. return;
  1391. EndRowEdit (false);
  1392. PageIndex = args.NewPageIndex;
  1393. OnPageIndexChanged (EventArgs.Empty);
  1394. }
  1395. public void ChangeMode (DetailsViewMode newMode)
  1396. {
  1397. CurrentMode = newMode;
  1398. RequireBinding ();
  1399. }
  1400. void ProcessChangeMode (DetailsViewMode newMode)
  1401. {
  1402. DetailsViewModeEventArgs args = new DetailsViewModeEventArgs (newMode, false);
  1403. OnModeChanging (args);
  1404. if (args.Cancel || !IsBoundUsingDataSourceID)
  1405. return;
  1406. ChangeMode (args.NewMode);
  1407. OnModeChanged (EventArgs.Empty);
  1408. }
  1409. void CancelEdit ()
  1410. {
  1411. DetailsViewModeEventArgs args = new DetailsViewModeEventArgs (DetailsViewMode.ReadOnly, true);
  1412. OnModeChanging (args);
  1413. if (args.Cancel || !IsBoundUsingDataSourceID)
  1414. return;
  1415. EndRowEdit ();
  1416. }
  1417. public virtual void UpdateItem (bool causesValidation)
  1418. {
  1419. UpdateItem (null, causesValidation);
  1420. }
  1421. void UpdateItem (string param, bool causesValidation)
  1422. {
  1423. if (causesValidation && Page != null && !Page.IsValid)
  1424. return;
  1425. if (CurrentMode != DetailsViewMode.Edit) throw new HttpException ();
  1426. currentEditOldValues = OldEditValues.Values;
  1427. currentEditRowKeys = DataKey.Values;
  1428. currentEditNewValues = GetRowValues (false, false);
  1429. DetailsViewUpdateEventArgs args = new DetailsViewUpdateEventArgs (param, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
  1430. OnItemUpdating (args);
  1431. if (args.Cancel || !IsBoundUsingDataSourceID)
  1432. return;
  1433. DataSourceView view = GetData ();
  1434. if (view == null)
  1435. throw new HttpException ("The DataSourceView associated to data bound control was null");
  1436. view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
  1437. }
  1438. bool UpdateCallback (int recordsAffected, Exception exception)
  1439. {
  1440. DetailsViewUpdatedEventArgs dargs = new DetailsViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
  1441. OnItemUpdated (dargs);
  1442. if (!dargs.KeepInEditMode)
  1443. EndRowEdit ();
  1444. return dargs.ExceptionHandled;
  1445. }
  1446. public virtual void InsertItem (bool causesValidation)
  1447. {
  1448. InsertItem (null, causesValidation);
  1449. }
  1450. void InsertItem (string param, bool causesValidation)
  1451. {
  1452. if (causesValidation && Page != null && !Page.IsValid)
  1453. return;
  1454. if (CurrentMode != DetailsViewMode.Insert) throw new HttpException ();
  1455. currentEditNewValues = GetRowValues (false, true);
  1456. DetailsViewInsertEventArgs args = new DetailsViewInsertEventArgs (param, currentEditNewValues);
  1457. OnItemInserting (args);
  1458. if (args.Cancel || !IsBoundUsingDataSourceID)
  1459. return;
  1460. DataSourceView view = GetData ();
  1461. if (view == null)
  1462. throw new HttpException ("The DataSourceView associated to data bound control was null");
  1463. view.Insert (currentEditNewValues, new DataSourceViewOperationCallback (InsertCallback));
  1464. }
  1465. bool InsertCallback (int recordsAffected, Exception exception)
  1466. {
  1467. DetailsViewInsertedEventArgs dargs = new DetailsViewInsertedEventArgs (recordsAffected, exception, currentEditNewValues);
  1468. OnItemInserted (dargs);
  1469. if (!dargs.KeepInInsertMode)
  1470. EndRowEdit ();
  1471. return dargs.ExceptionHandled;
  1472. }
  1473. public virtual void DeleteItem ()
  1474. {
  1475. currentEditRowKeys = DataKey.Values;
  1476. currentEditNewValues = GetRowValues (true, false);
  1477. DetailsViewDeleteEventArgs args = new DetailsViewDeleteEventArgs (PageIndex, currentEditRowKeys, currentEditNewValues);
  1478. OnItemDeleting (args);
  1479. if (args.Cancel || !IsBoundUsingDataSourceID)
  1480. return;
  1481. DataSourceView view = GetData ();
  1482. if (view != null)
  1483. view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
  1484. else {
  1485. DetailsViewDeletedEventArgs dargs = new DetailsViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
  1486. OnItemDeleted (dargs);
  1487. }
  1488. if (PageIndex > 0 && PageIndex == PageCount - 1)
  1489. PageIndex --;
  1490. RequireBinding ();
  1491. }
  1492. bool DeleteCallback (int recordsAffected, Exception exception)
  1493. {
  1494. DetailsViewDeletedEventArgs dargs = new DetailsViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
  1495. OnItemDeleted (dargs);
  1496. return dargs.ExceptionHandled;
  1497. }
  1498. void EndRowEdit ()
  1499. {
  1500. EndRowEdit (true);
  1501. }
  1502. void EndRowEdit (bool switchToDefaultMode) {
  1503. if (switchToDefaultMode)
  1504. ChangeMode (DefaultMode);
  1505. oldEditValues = new DataKey (new OrderedDictionary ());
  1506. currentEditRowKeys = null;
  1507. currentEditOldValues = null;
  1508. currentEditNewValues = null;
  1509. RequireBinding ();
  1510. }
  1511. protected internal override void LoadControlState (object ob)
  1512. {
  1513. if (ob == null) return;
  1514. object[] state = (object[]) ob;
  1515. base.LoadControlState (state[0]);
  1516. pageIndex = (int) state[1];
  1517. pageCount = (int) state[2];
  1518. CurrentMode = (DetailsViewMode) state[3];
  1519. dataKeyNames = (string[]) state[4];
  1520. defaultMode = (DetailsViewMode) state[5];
  1521. if (state [6] != null)
  1522. ((IStateManager) DataKey).LoadViewState (state [6]);
  1523. if (state [7] != null)
  1524. ((IStateManager) OldEditValues).LoadViewState (state [7]);
  1525. }
  1526. protected internal override object SaveControlState ()
  1527. {
  1528. object bstate = base.SaveControlState ();
  1529. return new object[] {
  1530. bstate,
  1531. pageIndex,
  1532. pageCount,
  1533. CurrentMode,
  1534. dataKeyNames,
  1535. defaultMode,
  1536. (key == null ? null : ((IStateManager)key).SaveViewState()),
  1537. (oldEditValues == null ? null : ((IStateManager) oldEditValues).SaveViewState ())
  1538. };
  1539. }
  1540. protected override void TrackViewState()
  1541. {
  1542. base.TrackViewState();
  1543. if (columns != null) ((IStateManager)columns).TrackViewState();
  1544. if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
  1545. if (alternatingRowStyle != null) ((IStateManager)alternatingRowStyle).TrackViewState();
  1546. if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
  1547. if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
  1548. if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
  1549. if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
  1550. if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
  1551. if (insertRowStyle != null) ((IStateManager)insertRowStyle).TrackViewState();
  1552. if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
  1553. if (key != null) ((IStateManager)key).TrackViewState();
  1554. if (autoFieldProperties != null) {
  1555. foreach (IStateManager sm in autoFieldProperties)
  1556. sm.TrackViewState ();
  1557. }
  1558. if (ControlStyleCreated)
  1559. ControlStyle.TrackViewState ();
  1560. }
  1561. protected override object SaveViewState()
  1562. {
  1563. object[] states = new object [13];
  1564. states[0] = base.SaveViewState();
  1565. states[1] = (columns == null ? null : ((IStateManager)columns).SaveViewState());
  1566. states[2] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
  1567. states[3] = (alternatingRowStyle == null ? null : ((IStateManager)alternatingRowStyle).SaveViewState());
  1568. states[4] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
  1569. states[5] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
  1570. states[6] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
  1571. states[7] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
  1572. states[8] = (insertRowStyle == null ? null : ((IStateManager)insertRowStyle).SaveViewState());
  1573. states[9] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
  1574. states[10] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
  1575. if (autoFieldProperties != null) {
  1576. object[] data = new object [autoFieldProperties.Length];
  1577. bool allNull = true;
  1578. for (int n=0; n<data.Length; n++) {
  1579. data [n] = ((IStateManager)autoFieldProperties [n]).SaveViewState ();
  1580. if (data [n] != null) allNull = false;
  1581. }
  1582. if (!allNull) states [11] = data;
  1583. }
  1584. if (ControlStyleCreated)
  1585. states [12] = ControlStyle.SaveViewState ();
  1586. for (int i = states.Length - 1; i >= 0; i--) {
  1587. if (states [i] != null)
  1588. return states;
  1589. }
  1590. return null;
  1591. }
  1592. protected override void LoadViewState (object savedState)
  1593. {
  1594. if (savedState == null) {
  1595. base.LoadViewState (null);
  1596. return;
  1597. }
  1598. object [] states = (object []) savedState;
  1599. if (states[11] != null) {
  1600. object[] data = (object[]) states [11];
  1601. autoFieldProperties = new AutoGeneratedFieldProperties [data.Length];
  1602. for (int n=0; n<data.Length; n++) {
  1603. IStateManager p = new AutoGeneratedFieldProperties ();
  1604. p.TrackViewState ();
  1605. p.LoadViewState (data [n]);
  1606. autoFieldProperties [n] = (AutoGeneratedFieldProperties) p;
  1607. }
  1608. }
  1609. base.LoadViewState (states[0]);
  1610. if (states[1] != null) ((IStateManager)Fields).LoadViewState (states[1]);
  1611. if (states[2] != null) ((IStateManager)PagerSettings).LoadViewState (states[2]);
  1612. if (states[3] != null) ((IStateManager)AlternatingRowStyle).LoadViewState (states[3]);
  1613. if (states[4] != null) ((IStateManager)FooterStyle).LoadViewState (states[4]);
  1614. if (states[5] != null) ((IStateManager)HeaderStyle).LoadViewState (states[5]);
  1615. if (states[6] != null) ((IStateManager)PagerStyle).LoadViewState (states[6]);
  1616. if (states[7] != null) ((IStateManager)RowStyle).LoadViewState (states[7]);
  1617. if (states[8] != null) ((IStateManager)InsertRowStyle).LoadViewState (states[8]);
  1618. if (states[9] != null) ((IStateManager)EditRowStyle).LoadViewState (states[9]);
  1619. if (states[10] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
  1620. if (states [12] != null)
  1621. ControlStyle.LoadViewState (states [12]);
  1622. }
  1623. void ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
  1624. {
  1625. RaiseCallbackEvent (eventArgs);
  1626. }
  1627. protected virtual void RaiseCallbackEvent (string eventArgs)
  1628. {
  1629. string[] clientData = eventArgs.Split ('|');
  1630. PageIndex = int.Parse (clientData[0]);
  1631. RaisePostBackEvent (clientData[1]);
  1632. DataBind ();
  1633. }
  1634. string ICallbackEventHandler.GetCallbackResult ()
  1635. {
  1636. return GetCallbackResult ();
  1637. }
  1638. protected virtual string GetCallbackResult ()
  1639. {
  1640. PrepareControlHierarchy ();
  1641. StringWriter sw = new StringWriter ();
  1642. sw.Write (PageIndex.ToString () + '|');
  1643. HtmlTextWriter writer = new HtmlTextWriter (sw);
  1644. RenderGrid (writer);
  1645. return sw.ToString ();
  1646. }
  1647. protected virtual string GetCallbackScript (IButtonControl buttonControl, string argument)
  1648. {
  1649. if (EnablePagingCallbacks)
  1650. return "javascript:DetailsView_ClientEvent (\"" + ClientID + "\",\"" + buttonControl.CommandName + "$" + buttonControl.CommandArgument + "\"); return false;";
  1651. else
  1652. return null;
  1653. }
  1654. string ICallbackContainer.GetCallbackScript (IButtonControl control, string argument)
  1655. {
  1656. return GetCallbackScript (control, argument);
  1657. }
  1658. protected override void OnPagePreLoad (object sender, EventArgs e)
  1659. {
  1660. base.OnPagePreLoad (sender, e);
  1661. if (Page.IsPostBack && EnablePagingCallbacks) {
  1662. int page;
  1663. if (int.TryParse (Page.Request.Form [ClientID + "_Page"], out page))
  1664. PageIndex = page;
  1665. }
  1666. }
  1667. protected internal override void OnPreRender (EventArgs e)
  1668. {
  1669. base.OnPreRender (e);
  1670. if (EnablePagingCallbacks)
  1671. {
  1672. if (!Page.ClientScript.IsClientScriptIncludeRegistered (typeof(DetailsView), "DetailsView.js")) {
  1673. string url = Page.ClientScript.GetWebResourceUrl (typeof(DetailsView), "DetailsView.js");
  1674. Page.ClientScript.RegisterClientScriptInclude (typeof(DetailsView), "DetailsView.js", url);
  1675. }
  1676. Page.ClientScript.RegisterHiddenField (ClientID + "_Page", PageIndex.ToString ());
  1677. string cgrid = ClientID + "_data";
  1678. string script = string.Format ("var {0} = new Object ();\n", cgrid);
  1679. script += string.Format ("{0}.pageIndex = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (PageIndex));
  1680. script += string.Format ("{0}.uid = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (UniqueID));
  1681. Page.ClientScript.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
  1682. // Make sure the basic script infrastructure is rendered
  1683. Page.ClientScript.GetCallbackEventReference (this, "null", "", "null");
  1684. Page.ClientScript.GetPostBackClientHyperlink (this, "");
  1685. }
  1686. }
  1687. protected internal override void Render (HtmlTextWriter writer)
  1688. {
  1689. PrepareControlHierarchy ();
  1690. if (EnablePagingCallbacks)
  1691. writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID + "_div");
  1692. writer.RenderBeginTag (HtmlTextWriterTag.Div);
  1693. RenderGrid (writer);
  1694. writer.RenderEndTag ();
  1695. }
  1696. void RenderGrid (HtmlTextWriter writer)
  1697. {
  1698. if (table == null)
  1699. return;
  1700. table.Render (writer);
  1701. }
  1702. PostBackOptions IPostBackContainer.GetPostBackOptions(IButtonControl control)
  1703. {
  1704. if (control == null)
  1705. throw new ArgumentNullException ("control");
  1706. if (control.CausesValidation)
  1707. 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.");
  1708. PostBackOptions options = new PostBackOptions (this);
  1709. options.Argument = control.CommandName + "$" + control.CommandArgument;
  1710. options.RequiresJavaScriptProtocol = true;
  1711. return options;
  1712. }
  1713. }
  1714. }
  1715. #endif