GridView.cs 62 KB

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