DetailsView.cs 61 KB

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