GridView.cs 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096
  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 (keys == null)
  811. throw new InvalidOperationException ("DataKeys");
  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. if (dataSource.DataSourceCount == 0)
  1114. return 0;
  1115. bool showPager = AllowPaging && (PageCount > 1);
  1116. table = CreateChildTable ();
  1117. Controls.Add (table);
  1118. ArrayList list = new ArrayList ();
  1119. // Creates the set of fields to show
  1120. ICollection fieldCollection = CreateColumns (dataSource, dataBinding);
  1121. DataControlField[] fields = new DataControlField [fieldCollection.Count];
  1122. fieldCollection.CopyTo (fields, 0);
  1123. foreach (DataControlField field in fields) {
  1124. field.Initialize (AllowSorting, this);
  1125. if (EnableSortingAndPagingCallbacks)
  1126. field.ValidateSupportsCallback ();
  1127. }
  1128. // Main table creation
  1129. if (showPager && PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom) {
  1130. topPagerRow = CreatePagerRow (fields.Length, dataSource);
  1131. table.Rows.Add (topPagerRow);
  1132. OnRowCreated (new GridViewRowEventArgs (topPagerRow));
  1133. }
  1134. GridViewRow headerRow = CreateRow (0, 0, DataControlRowType.Header, DataControlRowState.Normal);
  1135. table.Rows.Add (headerRow);
  1136. InitializeRow (headerRow, fields);
  1137. OnRowCreated (new GridViewRowEventArgs (headerRow));
  1138. if (dataBinding) {
  1139. headerRow.DataBind ();
  1140. OnRowDataBound (new GridViewRowEventArgs (headerRow));
  1141. }
  1142. foreach (object obj in dataSource) {
  1143. DataControlRowState rstate = GetRowState (list.Count);
  1144. GridViewRow row = CreateRow (list.Count, list.Count, DataControlRowType.DataRow, rstate);
  1145. row.DataItem = obj;
  1146. list.Add (row);
  1147. table.Rows.Add (row);
  1148. InitializeRow (row, fields);
  1149. OnRowCreated (new GridViewRowEventArgs (row));
  1150. if (dataBinding) {
  1151. row.DataBind ();
  1152. OnRowDataBound (new GridViewRowEventArgs (row));
  1153. if (EditIndex == row.RowIndex)
  1154. oldEditValues = new DataKey (GetRowValues (row, true, true));
  1155. DataKeyArrayList.Add (new DataKey (CreateRowDataKey (row), DataKeyNames));
  1156. }
  1157. if (list.Count >= PageSize)
  1158. break;
  1159. }
  1160. if (list.Count == 0) {
  1161. GridViewRow emptyRow = CreateEmptyrRow (fields.Length);
  1162. table.Rows.Add (emptyRow);
  1163. OnRowCreated (new GridViewRowEventArgs (emptyRow));
  1164. if (dataBinding) {
  1165. emptyRow.DataBind ();
  1166. OnRowDataBound (new GridViewRowEventArgs (emptyRow));
  1167. }
  1168. }
  1169. GridViewRow footerRow = CreateRow (0, 0, DataControlRowType.Footer, DataControlRowState.Normal);
  1170. table.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. table.Rows.Add (bottomPagerRow);
  1180. OnRowCreated (new GridViewRowEventArgs (bottomPagerRow));
  1181. }
  1182. rows = new GridViewRowCollection (list);
  1183. return dataSource.DataSourceCount;
  1184. }
  1185. protected override Style CreateControlStyle ()
  1186. {
  1187. TableStyle style = new TableStyle (ViewState);
  1188. style.GridLines = GridLines.Both;
  1189. style.CellSpacing = 0;
  1190. return style;
  1191. }
  1192. DataControlRowState GetRowState (int index)
  1193. {
  1194. DataControlRowState rstate = (index % 2) == 0 ? DataControlRowState.Normal : DataControlRowState.Alternate;
  1195. if (index == SelectedIndex) rstate |= DataControlRowState.Selected;
  1196. if (index == EditIndex) rstate |= DataControlRowState.Edit;
  1197. return rstate;
  1198. }
  1199. GridViewRow CreatePagerRow (int fieldCount, PagedDataSource dataSource)
  1200. {
  1201. GridViewRow row = CreateRow (-1, -1, DataControlRowType.Pager, DataControlRowState.Normal);
  1202. InitializePager (row, fieldCount, dataSource);
  1203. return row;
  1204. }
  1205. protected virtual void InitializePager (GridViewRow row, int columnSpan, PagedDataSource dataSource)
  1206. {
  1207. TableCell cell = new TableCell ();
  1208. if (columnSpan > 1)
  1209. cell.ColumnSpan = columnSpan;
  1210. if (pagerTemplate != null)
  1211. pagerTemplate.InstantiateIn (cell);
  1212. else
  1213. cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
  1214. row.Cells.Add (cell);
  1215. }
  1216. GridViewRow CreateEmptyrRow (int fieldCount)
  1217. {
  1218. GridViewRow row = CreateRow (-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
  1219. TableCell cell = new TableCell ();
  1220. cell.ColumnSpan = fieldCount;
  1221. if (emptyDataTemplate != null)
  1222. emptyDataTemplate.InstantiateIn (cell);
  1223. else
  1224. cell.Text = EmptyDataText;
  1225. row.Cells.Add (cell);
  1226. return row;
  1227. }
  1228. protected virtual void InitializeRow (GridViewRow row, DataControlField[] fields)
  1229. {
  1230. DataControlCellType ctype;
  1231. bool accessibleHeader = false;
  1232. switch (row.RowType) {
  1233. case DataControlRowType.Header:
  1234. ctype = DataControlCellType.Header;
  1235. accessibleHeader = UseAccessibleHeader;
  1236. break;
  1237. case DataControlRowType.Footer:
  1238. ctype = DataControlCellType.Footer;
  1239. break;
  1240. default:
  1241. ctype = DataControlCellType.DataCell;
  1242. break;
  1243. }
  1244. for (int n=0; n<fields.Length; n++) {
  1245. DataControlField field = fields [n];
  1246. DataControlFieldCell cell;
  1247. if (((field is BoundField) && ((BoundField)field).DataField == RowHeaderColumn) || accessibleHeader)
  1248. cell = new DataControlFieldHeaderCell (field, accessibleHeader ? TableHeaderScope.Column : TableHeaderScope.Row);
  1249. else
  1250. cell = new DataControlFieldCell (field);
  1251. row.Cells.Add (cell);
  1252. field.InitializeCell (cell, ctype, row.RowState, row.RowIndex);
  1253. }
  1254. }
  1255. IOrderedDictionary CreateRowDataKey (GridViewRow row)
  1256. {
  1257. if (cachedKeyProperties == null) {
  1258. PropertyDescriptorCollection props = TypeDescriptor.GetProperties (row.DataItem);
  1259. cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
  1260. for (int n=0; n<DataKeyNames.Length; n++) {
  1261. PropertyDescriptor p = props.Find (DataKeyNames [n], true);
  1262. if (p == null)
  1263. throw new InvalidOperationException ("Property '" + DataKeyNames [n] + "' not found in object of type " + row.DataItem.GetType ());
  1264. cachedKeyProperties [n] = p;
  1265. }
  1266. }
  1267. OrderedDictionary dic = new OrderedDictionary ();
  1268. foreach (PropertyDescriptor p in cachedKeyProperties)
  1269. dic [p.Name] = p.GetValue (row.DataItem);
  1270. return dic;
  1271. }
  1272. IOrderedDictionary GetRowValues (GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
  1273. {
  1274. OrderedDictionary dic = new OrderedDictionary ();
  1275. ExtractRowValues (dic, row, includeReadOnlyFields, includePrimaryKey);
  1276. return dic;
  1277. }
  1278. protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
  1279. {
  1280. foreach (TableCell cell in row.Cells) {
  1281. DataControlFieldCell c = cell as DataControlFieldCell;
  1282. if (c != null)
  1283. c.ContainingField.ExtractValuesFromCell (fieldValues, c, row.RowState, includeReadOnlyFields);
  1284. }
  1285. if (!includePrimaryKey && DataKeyNames != null)
  1286. foreach (string key in DataKeyNames)
  1287. fieldValues.Remove (key);
  1288. }
  1289. protected override HtmlTextWriterTag TagKey {
  1290. get {
  1291. if (EnableSortingAndPagingCallbacks)
  1292. return HtmlTextWriterTag.Div;
  1293. else
  1294. return HtmlTextWriterTag.Table;
  1295. }
  1296. }
  1297. public sealed override void DataBind ()
  1298. {
  1299. DataKeyArrayList.Clear ();
  1300. cachedKeyProperties = null;
  1301. base.DataBind ();
  1302. keys = new DataKeyArray (DataKeyArrayList);
  1303. }
  1304. protected internal override void PerformDataBinding (IEnumerable data)
  1305. {
  1306. base.PerformDataBinding (data);
  1307. }
  1308. protected internal virtual void PrepareControlHierarchy ()
  1309. {
  1310. if (table == null)
  1311. return;
  1312. table.Caption = Caption;
  1313. table.CaptionAlign = CaptionAlign;
  1314. foreach (GridViewRow row in table.Rows) {
  1315. switch (row.RowType) {
  1316. case DataControlRowType.Header:
  1317. if (headerStyle != null && !headerStyle.IsEmpty)
  1318. row.ControlStyle.CopyFrom(headerStyle);
  1319. row.Visible = ShowHeader;
  1320. break;
  1321. case DataControlRowType.Footer:
  1322. if (footerStyle != null && !footerStyle.IsEmpty)
  1323. row.ControlStyle.CopyFrom (footerStyle);
  1324. row.Visible = ShowFooter;
  1325. break;
  1326. case DataControlRowType.Pager:
  1327. if (pagerStyle != null && !pagerStyle.IsEmpty)
  1328. row.ControlStyle.CopyFrom (pagerStyle);
  1329. break;
  1330. case DataControlRowType.EmptyDataRow:
  1331. if (emptyDataRowStyle != null && !emptyDataRowStyle.IsEmpty)
  1332. row.ControlStyle.CopyFrom (emptyDataRowStyle);
  1333. break;
  1334. case DataControlRowType.DataRow:
  1335. if (rowStyle != null && !rowStyle.IsEmpty)
  1336. row.ControlStyle.CopyFrom (rowStyle);
  1337. if ((row.RowState & DataControlRowState.Alternate) != 0 && alternatingRowStyle != null && !alternatingRowStyle.IsEmpty)
  1338. row.ControlStyle.CopyFrom (alternatingRowStyle);
  1339. if ((row.RowState & DataControlRowState.Selected) != 0 && selectedRowStyle != null && !selectedRowStyle.IsEmpty)
  1340. row.ControlStyle.CopyFrom (selectedRowStyle);
  1341. if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null && !editRowStyle.IsEmpty)
  1342. row.ControlStyle.CopyFrom (editRowStyle);
  1343. break;
  1344. default:
  1345. break;
  1346. }
  1347. foreach (TableCell cell in row.Cells) {
  1348. DataControlFieldCell fcell = cell as DataControlFieldCell;
  1349. if (fcell != null) {
  1350. DataControlField field = fcell.ContainingField;
  1351. switch (row.RowType) {
  1352. case DataControlRowType.Header:
  1353. if (field.HeaderStyleCreated && !field.HeaderStyle.IsEmpty)
  1354. cell.ControlStyle.CopyFrom (field.HeaderStyle);
  1355. break;
  1356. case DataControlRowType.Footer:
  1357. if (field.FooterStyleCreated && !field.FooterStyle.IsEmpty)
  1358. cell.ControlStyle.CopyFrom (field.FooterStyle);
  1359. break;
  1360. default:
  1361. if (field.ControlStyleCreated && !field.ControlStyle.IsEmpty)
  1362. foreach (Control c in cell.Controls) {
  1363. WebControl wc = c as WebControl;
  1364. if (wc != null)
  1365. wc.ControlStyle.MergeWith (field.ControlStyle);
  1366. }
  1367. if (field.ItemStyleCreated && !field.ItemStyle.IsEmpty)
  1368. cell.ControlStyle.CopyFrom (field.ItemStyle);
  1369. break;
  1370. }
  1371. }
  1372. }
  1373. }
  1374. }
  1375. protected internal override void OnInit (EventArgs e)
  1376. {
  1377. Page.RegisterRequiresControlState (this);
  1378. base.OnInit (e);
  1379. }
  1380. void OnFieldsChanged (object sender, EventArgs args)
  1381. {
  1382. RequireBinding ();
  1383. }
  1384. protected override void OnDataPropertyChanged ()
  1385. {
  1386. base.OnDataPropertyChanged ();
  1387. RequireBinding ();
  1388. }
  1389. protected override void OnDataSourceViewChanged (object sender, EventArgs e)
  1390. {
  1391. base.OnDataSourceViewChanged (sender, e);
  1392. RequireBinding ();
  1393. }
  1394. protected override bool OnBubbleEvent (object source, EventArgs e)
  1395. {
  1396. GridViewCommandEventArgs args = e as GridViewCommandEventArgs;
  1397. if (args != null) {
  1398. bool causesValidation = false;
  1399. IButtonControl button = args.CommandSource as IButtonControl;
  1400. if (button != null && button.CausesValidation) {
  1401. Page.Validate (button.ValidationGroup);
  1402. causesValidation = true;
  1403. }
  1404. OnRowCommand (args);
  1405. string param = args.CommandArgument as string;
  1406. if (param == null || param.Length == 0) {
  1407. GridViewRow row = args.Row;
  1408. if (row != null)
  1409. param = row.RowIndex.ToString();
  1410. }
  1411. ProcessEvent (args.CommandName, param, causesValidation);
  1412. return true;
  1413. }
  1414. return base.OnBubbleEvent (source, e);
  1415. }
  1416. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  1417. {
  1418. RaisePostBackEvent (eventArgument);
  1419. }
  1420. // This is prolly obsolete
  1421. protected virtual void RaisePostBackEvent (string eventArgument)
  1422. {
  1423. int i = eventArgument.IndexOf ('$');
  1424. if (i != -1)
  1425. ProcessEvent (eventArgument.Substring (0, i), eventArgument.Substring (i + 1), false);
  1426. else
  1427. ProcessEvent (eventArgument, null, false);
  1428. }
  1429. void ProcessEvent (string eventName, string param, bool causesValidation)
  1430. {
  1431. switch (eventName)
  1432. {
  1433. case DataControlCommands.PageCommandName:
  1434. int newIndex = -1;
  1435. switch (param) {
  1436. case DataControlCommands.FirstPageCommandArgument:
  1437. newIndex = 0;
  1438. break;
  1439. case DataControlCommands.LastPageCommandArgument:
  1440. newIndex = PageCount - 1;
  1441. break;
  1442. case DataControlCommands.NextPageCommandArgument:
  1443. newIndex = PageIndex + 1;
  1444. break;
  1445. case DataControlCommands.PreviousPageCommandArgument:
  1446. newIndex = PageIndex - 1;
  1447. break;
  1448. default:
  1449. int paramIndex = 0;
  1450. int.TryParse (param, out paramIndex);
  1451. newIndex = paramIndex - 1;
  1452. break;
  1453. }
  1454. ShowPage (newIndex);
  1455. break;
  1456. case DataControlCommands.FirstPageCommandArgument:
  1457. ShowPage (0);
  1458. break;
  1459. case DataControlCommands.LastPageCommandArgument:
  1460. ShowPage (PageCount - 1);
  1461. break;
  1462. case DataControlCommands.NextPageCommandArgument:
  1463. if (PageIndex < PageCount - 1)
  1464. ShowPage (PageIndex + 1);
  1465. break;
  1466. case DataControlCommands.PreviousPageCommandArgument:
  1467. if (PageIndex > 0)
  1468. ShowPage (PageIndex - 1);
  1469. break;
  1470. case DataControlCommands.SelectCommandName:
  1471. SelectRow (int.Parse (param));
  1472. break;
  1473. case DataControlCommands.EditCommandName:
  1474. EditRow (int.Parse (param));
  1475. break;
  1476. case DataControlCommands.UpdateCommandName:
  1477. int editIndex = int.Parse (param);
  1478. UpdateRow (Rows [editIndex], editIndex, causesValidation);
  1479. break;
  1480. case DataControlCommands.CancelCommandName:
  1481. CancelEdit ();
  1482. break;
  1483. case DataControlCommands.DeleteCommandName:
  1484. DeleteRow (int.Parse (param));
  1485. break;
  1486. case DataControlCommands.SortCommandName:
  1487. Sort (param);
  1488. break;
  1489. }
  1490. }
  1491. void Sort (string newSortExpression)
  1492. {
  1493. SortDirection newDirection = SortDirection.Ascending;
  1494. if (sortExpression == newSortExpression && sortDirection == SortDirection.Ascending)
  1495. newDirection = SortDirection.Descending;
  1496. Sort (newSortExpression, newDirection);
  1497. }
  1498. public virtual void Sort (string newSortExpression, SortDirection newSortDirection)
  1499. {
  1500. GridViewSortEventArgs args = new GridViewSortEventArgs (newSortExpression, newSortDirection);
  1501. OnSorting (args);
  1502. if (args.Cancel) return;
  1503. if (IsBoundUsingDataSourceID) {
  1504. EditIndex = -1;
  1505. PageIndex = 0;
  1506. SortExpression = args.SortExpression;
  1507. SortDirection = args.SortDirection;
  1508. }
  1509. OnSorted (EventArgs.Empty);
  1510. }
  1511. void SelectRow (int index)
  1512. {
  1513. GridViewSelectEventArgs args = new GridViewSelectEventArgs (index);
  1514. OnSelectedIndexChanging (args);
  1515. if (!args.Cancel) {
  1516. SelectedIndex = args.NewSelectedIndex;
  1517. OnSelectedIndexChanged (EventArgs.Empty);
  1518. }
  1519. }
  1520. void ShowPage (int newIndex)
  1521. {
  1522. GridViewPageEventArgs args = new GridViewPageEventArgs (newIndex);
  1523. OnPageIndexChanging (args);
  1524. if (args.Cancel || !IsBoundUsingDataSourceID)
  1525. return;
  1526. EndRowEdit ();
  1527. PageIndex = args.NewPageIndex;
  1528. OnPageIndexChanged (EventArgs.Empty);
  1529. }
  1530. void EditRow (int index)
  1531. {
  1532. GridViewEditEventArgs args = new GridViewEditEventArgs (index);
  1533. OnRowEditing (args);
  1534. if (args.Cancel || !IsBoundUsingDataSourceID)
  1535. return;
  1536. EditIndex = args.NewEditIndex;
  1537. }
  1538. void CancelEdit ()
  1539. {
  1540. GridViewCancelEditEventArgs args = new GridViewCancelEditEventArgs (EditIndex);
  1541. OnRowCancelingEdit (args);
  1542. if (args.Cancel || !IsBoundUsingDataSourceID)
  1543. return;
  1544. EndRowEdit ();
  1545. }
  1546. [MonoTODO ("Support two-way binding expressions")]
  1547. public virtual void UpdateRow (int rowIndex, bool causesValidation)
  1548. {
  1549. if (rowIndex != EditIndex) throw new NotSupportedException ();
  1550. GridViewRow row = Rows [rowIndex];
  1551. UpdateRow (row, rowIndex, causesValidation);
  1552. }
  1553. void UpdateRow (GridViewRow row, int rowIndex, bool causesValidation)
  1554. {
  1555. if (causesValidation && Page != null && !Page.IsValid)
  1556. return;
  1557. currentEditOldValues = OldEditValues.Values;
  1558. currentEditRowKeys = DataKeys [rowIndex].Values;
  1559. currentEditNewValues = GetRowValues (row, false, false);
  1560. GridViewUpdateEventArgs args = new GridViewUpdateEventArgs (rowIndex, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
  1561. OnRowUpdating (args);
  1562. if (args.Cancel || !IsBoundUsingDataSourceID)
  1563. return;
  1564. DataSourceView view = GetData ();
  1565. if (view == null)
  1566. throw new HttpException ("The DataSourceView associated to data bound control was null");
  1567. view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
  1568. }
  1569. bool UpdateCallback (int recordsAffected, Exception exception)
  1570. {
  1571. GridViewUpdatedEventArgs dargs = new GridViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
  1572. OnRowUpdated (dargs);
  1573. if (!dargs.KeepInEditMode)
  1574. EndRowEdit ();
  1575. return dargs.ExceptionHandled;
  1576. }
  1577. public virtual void DeleteRow (int rowIndex)
  1578. {
  1579. GridViewRow row = Rows [rowIndex];
  1580. currentEditRowKeys = DataKeys [rowIndex].Values;
  1581. currentEditNewValues = GetRowValues (row, true, true);
  1582. GridViewDeleteEventArgs args = new GridViewDeleteEventArgs (rowIndex, currentEditRowKeys, currentEditNewValues);
  1583. OnRowDeleting (args);
  1584. if (args.Cancel || !IsBoundUsingDataSourceID)
  1585. return;
  1586. RequireBinding ();
  1587. DataSourceView view = GetData ();
  1588. if (view != null)
  1589. view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
  1590. else {
  1591. GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
  1592. OnRowDeleted (dargs);
  1593. }
  1594. }
  1595. bool DeleteCallback (int recordsAffected, Exception exception)
  1596. {
  1597. GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
  1598. OnRowDeleted (dargs);
  1599. return dargs.ExceptionHandled;
  1600. }
  1601. void EndRowEdit ()
  1602. {
  1603. EditIndex = -1;
  1604. oldEditValues = new DataKey (new OrderedDictionary ());
  1605. currentEditRowKeys = null;
  1606. currentEditOldValues = null;
  1607. currentEditNewValues = null;
  1608. }
  1609. protected internal override void LoadControlState (object ob)
  1610. {
  1611. if (ob == null) return;
  1612. object[] state = (object[]) ob;
  1613. base.LoadControlState (state[0]);
  1614. pageIndex = (int) state[1];
  1615. selectedIndex = (int) state[2];
  1616. editIndex = (int) state[3];
  1617. sortExpression = (string) state[4];
  1618. sortDirection = (SortDirection) state[5];
  1619. DataKeyNames = (string []) state [6];
  1620. if (state [7] != null)
  1621. LoadDataKeyArrayState ((object []) state [7]);
  1622. if (state [8] != null)
  1623. ((IStateManager) OldEditValues).LoadViewState (state [8]);
  1624. }
  1625. protected internal override object SaveControlState ()
  1626. {
  1627. if (EnableSortingAndPagingCallbacks) {
  1628. Page.ClientScript.RegisterHiddenField (ClientID + "_Page", PageIndex.ToString ());
  1629. Page.ClientScript.RegisterHiddenField (ClientID + "_SortExpression", SortExpression);
  1630. Page.ClientScript.RegisterHiddenField (ClientID + "_SortDirection", ((int)SortDirection).ToString());
  1631. }
  1632. object bstate = base.SaveControlState ();
  1633. return new object [] {
  1634. bstate,
  1635. pageIndex,
  1636. selectedIndex,
  1637. editIndex,
  1638. sortExpression,
  1639. sortDirection,
  1640. DataKeyNames,
  1641. SaveDataKeyArrayState (),
  1642. (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState ())
  1643. };
  1644. }
  1645. object [] SaveDataKeyArrayState ()
  1646. {
  1647. if (keys == null)
  1648. return null;
  1649. object [] state = new object [keys.Count];
  1650. for (int i = 0; i < keys.Count; i++) {
  1651. state [i] = ((IStateManager) keys [i]).SaveViewState ();
  1652. }
  1653. return state;
  1654. }
  1655. void LoadDataKeyArrayState (object [] state)
  1656. {
  1657. for (int i = 0; i < state.Length; i++) {
  1658. DataKey dataKey = new DataKey (new OrderedDictionary (DataKeyNames.Length), DataKeyNames);
  1659. ((IStateManager) dataKey).LoadViewState (state [i]);
  1660. DataKeyArrayList.Add (dataKey);
  1661. }
  1662. keys = new DataKeyArray (DataKeyArrayList);
  1663. }
  1664. protected override void TrackViewState()
  1665. {
  1666. base.TrackViewState();
  1667. if (columns != null) ((IStateManager)columns).TrackViewState();
  1668. if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
  1669. if (alternatingRowStyle != null) ((IStateManager)alternatingRowStyle).TrackViewState();
  1670. if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
  1671. if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
  1672. if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
  1673. if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
  1674. if (selectedRowStyle != null) ((IStateManager)selectedRowStyle).TrackViewState();
  1675. if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
  1676. if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
  1677. if (keys != null) ((IStateManager)keys).TrackViewState();
  1678. if (autoFieldProperties != null) {
  1679. foreach (IStateManager sm in autoFieldProperties)
  1680. sm.TrackViewState ();
  1681. }
  1682. }
  1683. protected override object SaveViewState()
  1684. {
  1685. object[] states = new object [12];
  1686. states[0] = base.SaveViewState();
  1687. states[1] = (columns == null ? null : ((IStateManager)columns).SaveViewState());
  1688. states[2] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
  1689. states[3] = (alternatingRowStyle == null ? null : ((IStateManager)alternatingRowStyle).SaveViewState());
  1690. states[4] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
  1691. states[5] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
  1692. states[6] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
  1693. states[7] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
  1694. states[8] = (selectedRowStyle == null ? null : ((IStateManager)selectedRowStyle).SaveViewState());
  1695. states[9] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
  1696. states[10] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
  1697. if (autoFieldProperties != null) {
  1698. object[] data = new object [autoFieldProperties.Length];
  1699. bool allNull = true;
  1700. for (int n=0; n<data.Length; n++) {
  1701. data [n] = ((IStateManager)autoFieldProperties [n]).SaveViewState ();
  1702. if (data [n] != null) allNull = false;
  1703. }
  1704. if (!allNull) states [11] = data;
  1705. }
  1706. for (int i = states.Length - 1; i >= 0; i--) {
  1707. if (states [i] != null)
  1708. return states;
  1709. }
  1710. return null;
  1711. }
  1712. protected override void LoadViewState (object savedState)
  1713. {
  1714. if (savedState == null) {
  1715. base.LoadViewState (null);
  1716. return;
  1717. }
  1718. object [] states = (object []) savedState;
  1719. if (states[11] != null) {
  1720. object[] data = (object[]) states [11];
  1721. autoFieldProperties = new AutoGeneratedFieldProperties [data.Length];
  1722. for (int n=0; n<data.Length; n++) {
  1723. IStateManager p = new AutoGeneratedFieldProperties ();
  1724. p.TrackViewState ();
  1725. p.LoadViewState (data [n]);
  1726. autoFieldProperties [n] = (AutoGeneratedFieldProperties) p;
  1727. }
  1728. }
  1729. base.LoadViewState (states[0]);
  1730. if (states[1] != null) ((IStateManager)Columns).LoadViewState (states[1]);
  1731. if (states[2] != null) ((IStateManager)PagerSettings).LoadViewState (states[2]);
  1732. if (states[3] != null) ((IStateManager)AlternatingRowStyle).LoadViewState (states[3]);
  1733. if (states[4] != null) ((IStateManager)FooterStyle).LoadViewState (states[4]);
  1734. if (states[5] != null) ((IStateManager)HeaderStyle).LoadViewState (states[5]);
  1735. if (states[6] != null) ((IStateManager)PagerStyle).LoadViewState (states[6]);
  1736. if (states[7] != null) ((IStateManager)RowStyle).LoadViewState (states[7]);
  1737. if (states[8] != null) ((IStateManager)SelectedRowStyle).LoadViewState (states[8]);
  1738. if (states[9] != null) ((IStateManager)EditRowStyle).LoadViewState (states[9]);
  1739. if (states[10] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
  1740. }
  1741. void ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
  1742. {
  1743. RaiseCallbackEvent (eventArgs);
  1744. }
  1745. protected virtual void RaiseCallbackEvent (string eventArgs)
  1746. {
  1747. string[] clientData = eventArgs.Split ('|');
  1748. PageIndex = int.Parse (clientData[0]);
  1749. SortExpression = HttpUtility.UrlDecode (clientData [1]);
  1750. SortDirection = (SortDirection) int.Parse (clientData [2]);
  1751. RaisePostBackEvent (clientData[3]);
  1752. DataBind ();
  1753. }
  1754. string ICallbackEventHandler.GetCallbackResult ()
  1755. {
  1756. return GetCallbackResult ();
  1757. }
  1758. protected virtual string GetCallbackResult ()
  1759. {
  1760. PrepareControlHierarchy ();
  1761. StringWriter sw = new StringWriter ();
  1762. sw.Write (PageIndex.ToString () + '|' + SortExpression + '|' + (int) SortDirection + '|');
  1763. HtmlTextWriter writer = new HtmlTextWriter (sw);
  1764. RenderGrid (writer);
  1765. return sw.ToString ();
  1766. }
  1767. string ICallbackContainer.GetCallbackScript (IButtonControl control, string argument)
  1768. {
  1769. return GetCallbackScript (control, argument);
  1770. }
  1771. protected virtual string GetCallbackScript (IButtonControl control, string argument)
  1772. {
  1773. if (EnableSortingAndPagingCallbacks)
  1774. return "javascript:GridView_ClientEvent (\"" + ClientID + "\",\"" + control.CommandName + "$" + control.CommandArgument + "\"); return false;";
  1775. else
  1776. return null;
  1777. }
  1778. protected override void OnPagePreLoad (object sender, EventArgs e)
  1779. {
  1780. base.OnPagePreLoad (sender, e);
  1781. if (Page.IsPostBack && EnableSortingAndPagingCallbacks) {
  1782. int page;
  1783. if (int.TryParse (Page.Request.Form [ClientID + "_Page"], out page))
  1784. PageIndex = page;
  1785. int dir;
  1786. if (int.TryParse (Page.Request.Form [ClientID + "_SortDirection"], out dir))
  1787. SortDirection = (SortDirection) dir;
  1788. SortExpression = Page.Request.Form [ClientID + "_SortExpression"];
  1789. }
  1790. }
  1791. protected internal override void OnPreRender (EventArgs e)
  1792. {
  1793. base.OnPreRender (e);
  1794. if (EnableSortingAndPagingCallbacks)
  1795. {
  1796. if (!Page.ClientScript.IsClientScriptIncludeRegistered (typeof(GridView), "GridView.js")) {
  1797. string url = Page.ClientScript.GetWebResourceUrl (typeof(GridView), "GridView.js");
  1798. Page.ClientScript.RegisterClientScriptInclude (typeof(GridView), "GridView.js", url);
  1799. }
  1800. string cgrid = ClientID + "_data";
  1801. string script = string.Format ("var {0} = new Object ();\n", cgrid);
  1802. script += string.Format ("{0}.pageIndex = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (PageIndex));
  1803. script += string.Format ("{0}.sortExp = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (SortExpression == null ? "" : SortExpression));
  1804. script += string.Format ("{0}.sortDir = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral ((int) SortDirection));
  1805. script += string.Format ("{0}.uid = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (UniqueID));
  1806. Page.ClientScript.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
  1807. // Make sure the basic script infrastructure is rendered
  1808. Page.ClientScript.GetCallbackEventReference (this, "null", "", "null");
  1809. Page.ClientScript.GetPostBackClientHyperlink (this, "");
  1810. }
  1811. }
  1812. protected internal override void Render (HtmlTextWriter writer)
  1813. {
  1814. PrepareControlHierarchy ();
  1815. if (EnableSortingAndPagingCallbacks)
  1816. writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID + "_div");
  1817. writer.RenderBeginTag (HtmlTextWriterTag.Div);
  1818. RenderGrid (writer);
  1819. writer.RenderEndTag ();
  1820. }
  1821. void RenderGrid (HtmlTextWriter writer)
  1822. {
  1823. if (table == null)
  1824. return;
  1825. table.Render (writer);
  1826. }
  1827. PostBackOptions IPostBackContainer.GetPostBackOptions (IButtonControl control)
  1828. {
  1829. if (control == null)
  1830. throw new ArgumentNullException ("control");
  1831. if (control.CausesValidation)
  1832. 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.");
  1833. PostBackOptions options = new PostBackOptions (this);
  1834. options.Argument = control.CommandName + "$" + control.CommandArgument;
  1835. options.RequiresJavaScriptProtocol = true;
  1836. return options;
  1837. }
  1838. }
  1839. }
  1840. #endif