DetailsView.cs 59 KB

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