DetailsView.cs 59 KB

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