DetailsView.cs 53 KB

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