GridView.cs 63 KB

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