GridView.cs 64 KB

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