DetailsView.cs 60 KB

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