DetailsView.cs 60 KB

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