DetailsView.cs 61 KB

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